Skip to content

Tool calling with gemini-2.0-flash fail. #268

Closed
@kzmszk

Description

@kzmszk

Problem description

I ran pydantic-ai-examples/weather_agent.py with gemini-2.0-flash-exp and got the following error.
It works fine with gpt-4o or gemini-1.5-pro.

Env

python: 3.12.6
pydantic-ai: 0.0.12

How to reproduce

pydantic-ai-examples/weather_agent.py with the following change:

weather_agent = Agent(
-     'openai:gpt-4o',
+     'gemini-2.0-flash-exp',
    system_prompt='Be concise, reply with one sentence.',
    deps_type=Deps,
    retries=2,
)

Error log

$ uv run pydantic-ai/pydantic_ai_examples/weather_agent.py
06:42:59.371 weather_agent run prompt=What is the weather like in London and in Wiltshire?
06:42:59.372   preparing model and tools run_step=1
06:42:59.373   model request
06:43:00.377   handle model response
06:43:00.379     running tools=['get_lat_lng', 'get_lat_lng']
06:43:00.381   preparing model and tools run_step=2
06:43:00.381   model request
Traceback (most recent call last):
  File "/Users/kazu/Work/Fuchikoma/pd/pydantic-ai/pydantic_ai_examples/weather_agent.py", line 154, in <module>
    asyncio.run(main())
  File "/Users/kazu/.local/share/uv/python/cpython-3.12.6-macos-aarch64-none/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Users/kazu/.local/share/uv/python/cpython-3.12.6-macos-aarch64-none/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kazu/.local/share/uv/python/cpython-3.12.6-macos-aarch64-none/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/kazu/Work/Fuchikoma/pd/pydantic-ai/pydantic_ai_examples/weather_agent.py", line 146, in main
    result = await weather_agent.run(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kazu/Work/Fuchikoma/pd/.venv/lib/python3.12/site-packages/pydantic_ai/agent.py", line 209, in run
    model_response, request_cost = await agent_model.request(messages)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kazu/Work/Fuchikoma/pd/.venv/lib/python3.12/site-packages/pydantic_ai/models/gemini.py", line 168, in request
    async with self._make_request(messages, False) as http_response:
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kazu/.local/share/uv/python/cpython-3.12.6-macos-aarch64-none/lib/python3.12/contextlib.py", line 210, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kazu/Work/Fuchikoma/pd/.venv/lib/python3.12/site-packages/pydantic_ai/models/gemini.py", line 209, in _make_request
    raise exceptions.UnexpectedModelBehavior(f'Unexpected response from gemini {r.status_code}', r.text)
pydantic_ai.exceptions.UnexpectedModelBehavior: Unexpected response from gemini 400, body:
{
  "error": {
    "code": 400,
    "message": "Please ensure that the number of function response parts should be equal to number of function call parts of the function call turn.",
    "status": "INVALID_ARGUMENT"
  }
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions