You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. I had run into an issue when switching models.
Basically, I have implemented an API endpoint where I can change models.
Where is what happened:
I started with gemini-1.5-flash, asking it what time is now, which would call my now() tool.
It runs without any problem returning the current datetime
Then I switched to gpt-4o-mini and asked the same question again, passing the message history I got after using Gemini
This causes the following exception: AssertionError: OpenAI requires tool_call_id to be set: ToolCallPart(tool_name='now', args={}, tool_call_id=None, part_kind='tool-call')
[Edit] Minimal working example
frompydantic_aiimportAgentfrompydantic_ai.models.openaiimportOpenAIModelfrompydantic_ai.models.geminiimportGeminiModelfromdatetimeimportdatetimeopen_ai_api_key= ...
gemini_api_key= ...
openai_model=OpenAIModel(
model_name='gpt-4o-mini',
api_key=open_ai_api_key,
)
gemini_model=GeminiModel(
model_name='gemini-2.0-flash-exp', # could be gemini-1.5-flash alsoapi_key=gemini_api_key,
)
agent=Agent(gemini_model)
@agent.tool_plaindefnow():
returndatetime.now().isoformat()
r1=agent.run_sync('what is the current date time?')
print(r1.all_messages_json())
r2=agent.run_sync( # this will fail'what time is now?',
model=openai_model,
message_history=r1.all_messages(),
)
print(r2.all_messages_json())
Message history (stored until call gpt-4o-mini)
[ModelRequest(parts=[SystemPromptPart(content='\nYou are a test agent.\n\nYou must do what the user asks.\n', dynamic_ref=None, part_kind='system-prompt'), UserPromptPart(content='call now', timestamp=datetime.datetime(2025, 2, 11, 15, 55, 5, 628330, tzinfo=TzInfo(UTC)), part_kind='user-prompt')], kind='request'),
ModelResponse(parts=[TextPart(content='I am sorry, I cannot fulfill this request. The available tools do not provide the functionality to make calls.\n', part_kind='text')], model_name='gemini-1.5-flash', timestamp=datetime.datetime(2025, 2, 11, 15, 55, 6, 59052, tzinfo=TzInfo(UTC)), kind='response'),
ModelRequest(parts=[UserPromptPart(content='call the tool now', timestamp=datetime.datetime(2025, 2, 11, 15, 55, 14, 394461, tzinfo=TzInfo(UTC)), part_kind='user-prompt')], kind='request'),
ModelResponse(parts=[TextPart(content='I cannot call a tool. The available tools are functions that I can execute, not entities that I can call in a telephone sense. Is there something specific you would like me to do with one of the available tools?\n', part_kind='text')], model_name='gemini-1.5-flash', timestamp=datetime.datetime(2025, 2, 11, 15, 55, 15, 449295, tzinfo=TzInfo(UTC)), kind='response'),
ModelRequest(parts=[UserPromptPart(content='what time is now?', timestamp=datetime.datetime(2025, 2, 11, 15, 55, 23, 502937, tzinfo=TzInfo(UTC)), part_kind='user-prompt')], kind='request'),
ModelResponse(parts=[ToolCallPart(tool_name='now', args={}, tool_call_id=None, part_kind='tool-call')], model_name='gemini-1.5-flash', timestamp=datetime.datetime(2025, 2, 11, 15, 55, 24, 151395, tzinfo=TzInfo(UTC)), kind='response'),
ModelRequest(parts=[ToolReturnPart(tool_name='now', content='2025-02-11T12:55:24.153651-03:00', tool_call_id=None, timestamp=datetime.datetime(2025, 2, 11, 15, 55, 24, 153796, tzinfo=TzInfo(UTC)), part_kind='tool-return')], kind='request'),
ModelResponse(parts=[TextPart(content='The current time is 2025-02-11 12:55:24 -03:00.\n', part_kind='text')], model_name='gemini-1.5-flash', timestamp=datetime.datetime(2025, 2, 11, 15, 55, 24, 560881, tzinfo=TzInfo(UTC)), kind='response')]
Traceback
Traceback (most recent call last):
File "/app/agents/_agents/_wrapper.py", line 125, in run_stream
async with self._agent.run_stream(
~~~~~~~~~~~~~~~~~~~~~~^
user_prompt=user_prompt,
^^^^^^^^^^^^^^^^^^^^^^^^
...<2 lines>...
deps=self.deps,
^^^^^^^^^^^^^^^
) as result:
^
File "/usr/local/lib/python3.13/contextlib.py", line 214, in __aenter__
return await anext(self.gen)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/pydantic_ai/agent.py", line 595, in run_stream
async with node.run_to_result(GraphRunContext(graph_state, graph_deps)) as r:
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/contextlib.py", line 214, in __aenter__
return await anext(self.gen)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/pydantic_ai/_agent_graph.py", line 415, in run_to_result
async with ctx.deps.model.request_stream(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
ctx.state.message_history, model_settings, model_request_parameters
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
) as streamed_response:
^
File "/usr/local/lib/python3.13/contextlib.py", line 214, in __aenter__
return await anext(self.gen)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/pydantic_ai/models/openai.py", line 160, in request_stream
response = await self._completions_create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
messages, True, cast(OpenAIModelSettings, model_settings or {}), model_request_parameters
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/usr/local/lib/python3.13/site-packages/pydantic_ai/models/openai.py", line 203, in _completions_create
openai_messages = list(chain(*(self._map_message(m) for m in messages)))
File "/usr/local/lib/python3.13/site-packages/pydantic_ai/models/openai.py", line 267, in _map_message
tool_calls.append(self._map_tool_call(item))
~~~~~~~~~~~~~~~~~~~^^^^^^
File "/usr/local/lib/python3.13/site-packages/pydantic_ai/models/openai.py", line 284, in _map_tool_call
id=_guard_tool_call_id(t=t, model_source='OpenAI'),
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/pydantic_ai/_utils.py", line 200, in guard_tool_call_id
assert t.tool_call_id is not None, f'{model_source} requires `tool_call_id` to be set: {t}'
^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: OpenAI requires `tool_call_id` to be set: ToolCallPart(tool_name='now', args={}, tool_call_id=None, part_kind='tool-call')
The text was updated successfully, but these errors were encountered:
Also, I was wondering if exceptions raised by a tool (not ModelRetry) deserve a type for it and should go into the ModelMessage, to be in the message history
Since we have models that expectstool_call_id to be not None, I would suggest to change the ToolCallPart to have non-nullable tool_call_id, and generate dummy ids in all of these places.
Description
Hi. I had run into an issue when switching models.
Basically, I have implemented an API endpoint where I can change models.
Where is what happened:
gemini-1.5-flash
, asking it what time is now, which would call mynow()
tool.gpt-4o-mini
and asked the same question again, passing the message history I got after using GeminiAssertionError: OpenAI requires
tool_call_idto be set: ToolCallPart(tool_name='now', args={}, tool_call_id=None, part_kind='tool-call')
[Edit] Minimal working example
Message history (stored until call gpt-4o-mini)
Traceback
The text was updated successfully, but these errors were encountered: