Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dummy tool call id for models that doesn't return it #933

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
14 changes: 14 additions & 0 deletions pydantic_ai_slim/pydantic_ai/_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations as _annotations

import asyncio
import random
import time
from collections.abc import AsyncIterable, AsyncIterator, Iterator
from contextlib import asynccontextmanager, suppress
from dataclasses import dataclass, is_dataclass
from datetime import datetime, timezone
from functools import partial
from string import ascii_letters, digits
from types import GenericAlias
from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar, Union

Expand Down Expand Up @@ -201,6 +203,18 @@ def guard_tool_call_id(
return t.tool_call_id


def dummy_tool_call_id(*, length: int = 15) -> str:
"""Generate a random tool call ID.

Args:
length: The length of the tool call ID to be appended to `fake_call_`. (default: 15)

Returns:
str: A random tool call ID. e.g. `fake_call_pgWoMcQp9F4V0qS`
"""
return 'fake_call_' + ''.join(random.choices(ascii_letters + digits, k=length))


class PeekableAsyncStream(Generic[T]):
"""Wraps an async iterable of type T and allows peeking at the *next* item without consuming it.

Expand Down
1 change: 1 addition & 0 deletions pydantic_ai_slim/pydantic_ai/models/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ def _process_response_from_parts(
elif 'function_call' in part:
items.append(
ToolCallPart(
tool_call_id=_utils.dummy_tool_call_id(),
tool_name=part['function_call']['name'],
args=part['function_call']['args'],
)
Expand Down
2 changes: 1 addition & 1 deletion pydantic_ai_slim/pydantic_ai/models/mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ async def _process_streamed_response(
@staticmethod
def _map_mistral_to_pydantic_tool_call(tool_call: MistralToolCall) -> ToolCallPart:
"""Maps a MistralToolCall to a ToolCall."""
tool_call_id = tool_call.id or None
tool_call_id = tool_call.id or _utils.dummy_tool_call_id()
func_call = tool_call.function

return ToolCallPart(func_call.name, func_call.arguments, tool_call_id)
Expand Down
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from _pytest.assertion.rewrite import AssertionRewritingHook
from typing_extensions import TypeAlias

import pydantic_ai._utils
import pydantic_ai.models

__all__ = 'IsNow', 'IsFloat', 'TestEnv', 'ClientWithHandler', 'try_import'
Expand Down Expand Up @@ -179,3 +180,19 @@ def set_event_loop() -> Iterator[None]:
asyncio.set_event_loop(new_loop)
yield
new_loop.close()


@pytest.fixture
def dummy_tool_call_id_mock(monkeypatch: pytest.MonkeyPatch) -> Callable[[], str]:
"""Mock the dummy_tool_call_id function to return a fixed string."""

def _mock(*, length: int = 15) -> str:
return 'fake_call_123456789012345'

monkeypatch.setattr(
pydantic_ai._utils,
'dummy_tool_call_id',
_mock,
)

return _mock
26 changes: 21 additions & 5 deletions tests/models/test_gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,9 @@ async def test_text_success(get_gemini_client: GetGeminiClient):
)


async def test_request_structured_response(get_gemini_client: GetGeminiClient):
async def test_request_structured_response(
get_gemini_client: GetGeminiClient, dummy_tool_call_id_mock: Callable[[], str]
):
response = gemini_response(
_content_model_response(ModelResponse(parts=[ToolCallPart('final_result', {'response': [1, 2, 123]})]))
)
Expand All @@ -505,6 +507,7 @@ async def test_request_structured_response(get_gemini_client: GetGeminiClient):
ToolCallPart(
tool_name='final_result',
args={'response': [1, 2, 123]},
tool_call_id=dummy_tool_call_id_mock(),
)
],
model_name='gemini-1.5-flash-123',
Expand All @@ -513,15 +516,18 @@ async def test_request_structured_response(get_gemini_client: GetGeminiClient):
ModelRequest(
parts=[
ToolReturnPart(
tool_name='final_result', content='Final result processed.', timestamp=IsNow(tz=timezone.utc)
tool_name='final_result',
content='Final result processed.',
timestamp=IsNow(tz=timezone.utc),
tool_call_id=dummy_tool_call_id_mock(),
)
]
),
]
)


async def test_request_tool_call(get_gemini_client: GetGeminiClient):
async def test_request_tool_call(get_gemini_client: GetGeminiClient, dummy_tool_call_id_mock: Callable[[], str]):
responses = [
gemini_response(
_content_model_response(ModelResponse(parts=[ToolCallPart('get_location', {'loc_name': 'San Fransisco'})]))
Expand Down Expand Up @@ -566,6 +572,7 @@ async def get_location(loc_name: str) -> str:
ToolCallPart(
tool_name='get_location',
args={'loc_name': 'San Fransisco'},
tool_call_id=dummy_tool_call_id_mock(),
)
],
model_name='gemini-1.5-flash-123',
Expand All @@ -577,6 +584,7 @@ async def get_location(loc_name: str) -> str:
content='Wrong location, please try again',
tool_name='get_location',
timestamp=IsNow(tz=timezone.utc),
tool_call_id=dummy_tool_call_id_mock(),
)
]
),
Expand All @@ -585,10 +593,12 @@ async def get_location(loc_name: str) -> str:
ToolCallPart(
tool_name='get_location',
args={'loc_name': 'London'},
tool_call_id=dummy_tool_call_id_mock(),
),
ToolCallPart(
tool_name='get_location',
args={'loc_name': 'New York'},
tool_call_id=dummy_tool_call_id_mock(),
),
],
model_name='gemini-1.5-flash-123',
Expand All @@ -597,10 +607,16 @@ async def get_location(loc_name: str) -> str:
ModelRequest(
parts=[
ToolReturnPart(
tool_name='get_location', content='{"lat": 51, "lng": 0}', timestamp=IsNow(tz=timezone.utc)
tool_name='get_location',
content='{"lat": 51, "lng": 0}',
timestamp=IsNow(tz=timezone.utc),
tool_call_id=dummy_tool_call_id_mock(),
),
ToolReturnPart(
tool_name='get_location', content='{"lat": 41, "lng": -74}', timestamp=IsNow(tz=timezone.utc)
tool_name='get_location',
content='{"lat": 41, "lng": -74}',
timestamp=IsNow(tz=timezone.utc),
tool_call_id=dummy_tool_call_id_mock(),
),
]
),
Expand Down
114 changes: 113 additions & 1 deletion tests/models/test_mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dataclasses import dataclass
from datetime import datetime, timezone
from functools import cached_property
from typing import Any, cast
from typing import Any, Callable, cast

import pytest
from inline_snapshot import snapshot
Expand Down Expand Up @@ -1303,6 +1303,118 @@ async def get_location(loc_name: str) -> str:
)


async def test_request_tool_call_with_no_id_from_model(
allow_model_requests: None, dummy_tool_call_id_mock: Callable[[], str]
):
"""Test that a tool call with no id uses a dummy tool call id.

The role of this test is to check that the tool call id is a dummy id,
when the tool call id is not provided by the model.
"""

# Given
completion = [
completion_message(
MistralAssistantMessage(
content=None,
role='assistant',
tool_calls=[
MistralToolCall(
id=None, # no id
function=MistralFunctionCall(arguments='{"loc_name": "San Fransisco"}', name='get_location'),
type='function',
)
],
),
),
completion_message(
MistralAssistantMessage(
content=None,
role='assistant',
tool_calls=[
MistralToolCall(
id=None, # no id
function=MistralFunctionCall(arguments='{"loc_name": "London"}', name='get_location'),
type='function',
)
],
),
),
completion_message(MistralAssistantMessage(content='final response', role='assistant')),
]
mock_client = MockMistralAI.create_mock(completion)
model = MistralModel('mistral-large-latest', client=mock_client)
agent = Agent(model)

@agent.tool_plain
async def get_location(loc_name: str) -> str:
if loc_name == 'London':
return json.dumps({'lat': 51, 'lng': 0})
else:
raise ModelRetry('Wrong location, please try again')

# When
result = await agent.run('Hello')

# Then
assert result.all_messages() == snapshot(
[
ModelRequest(
parts=[
UserPromptPart(content='Hello', timestamp=IsNow(tz=timezone.utc)),
]
),
ModelResponse(
parts=[
ToolCallPart(
tool_name='get_location',
args='{"loc_name": "San Fransisco"}',
tool_call_id=dummy_tool_call_id_mock(),
)
],
model_name='mistral-large-123',
timestamp=datetime(2024, 1, 1, 0, 0, tzinfo=timezone.utc),
),
ModelRequest(
parts=[
RetryPromptPart(
content='Wrong location, please try again',
tool_name='get_location',
tool_call_id=dummy_tool_call_id_mock(),
timestamp=IsNow(tz=timezone.utc),
)
]
),
ModelResponse(
parts=[
ToolCallPart(
tool_name='get_location',
args='{"loc_name": "London"}',
tool_call_id=dummy_tool_call_id_mock(),
)
],
model_name='mistral-large-123',
timestamp=datetime(2024, 1, 1, 0, 0, tzinfo=timezone.utc),
),
ModelRequest(
parts=[
ToolReturnPart(
tool_name='get_location',
content='{"lat": 51, "lng": 0}',
tool_call_id=dummy_tool_call_id_mock(),
timestamp=IsNow(tz=timezone.utc),
)
]
),
ModelResponse(
parts=[TextPart(content='final response')],
model_name='mistral-large-123',
timestamp=datetime(2024, 1, 1, 0, 0, tzinfo=timezone.utc),
),
]
)


#####################
## Completion Function call Stream
#####################
Expand Down
13 changes: 12 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
from inline_snapshot import snapshot

from pydantic_ai import UserError
from pydantic_ai._utils import UNSET, PeekableAsyncStream, check_object_json_schema, group_by_temporal
from pydantic_ai._utils import (
UNSET,
PeekableAsyncStream,
check_object_json_schema,
dummy_tool_call_id,
group_by_temporal,
)

from .models.mock_async_stream import MockAsyncStream

Expand Down Expand Up @@ -80,3 +86,8 @@ def test_package_versions(capsys: pytest.CaptureFixture[str]):
packages = sorted((package.metadata['Name'], package.version) for package in distributions())
for name, version in packages:
print(f'{name:30} {version}')


def test_dummy_tool_call_id():
assert len(dummy_tool_call_id().replace('fake_call_', '')) > 0
assert len(dummy_tool_call_id(length=10).replace('fake_call_', '')) == 10