-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
- I confirm that I searched for my issue in https://github.com/pydantic/pydantic-ai/issues before opening this issue
Description
When using Gemini, RECITATION errors may occur. Pydantic AI currently does not handle them gracefully.
I do not know how to provide a code snippet to reproduce that specific problem, as it's elusive.
See discussion of the Gemini RECITATION problem here: https://issuetracker.google.com/issues/331677495
Similar issues in pydantic-ai have been rased for other untexpected Finish Reasons:
#509
#631
See traceback:
File "/my_folder/.venv/lib/python3.13/site-packages/opentelemetry/trace/__init__.py", line 589, in use_span
yield span
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_graph/graph.py", line 246, in iter
yield GraphRun[StateT, DepsT, RunEndT](
...<6 lines>...
)
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_ai/agent/__init__.py", line 674, in iter
yield agent_run
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_ai/agent/abstract.py", line 235, in run
async for node in agent_run:
...<4 lines>...
await event_stream_handler(_agent_graph.build_run_context(agent_run.ctx), stream)
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_ai/run.py", line 150, in __anext__
next_node = await self._graph_run.__anext__()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_graph/graph.py", line 758, in __anext__
return await self.next(self._next_node)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_graph/graph.py", line 731, in next
self._next_node = await node.run(ctx)
^^^^^^^^^^^^^^^^^^^
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_ai/_agent_graph.py", line 397, in run
return await self._make_request(ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_ai/_agent_graph.py", line 439, in _make_request
model_response = await ctx.deps.model.request(message_history, model_settings, model_request_parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_ai/models/instrumented.py", line 362, in request
response = await self.wrapped.request(messages, model_settings, model_request_parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_ai/models/google.py", line 240, in request
return self._process_response(response)
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "/my_folder/.venv/lib/python3.13/site-packages/pydantic_ai/models/google.py", line 463, in _process_response
raise UnexpectedModelBehavior(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'Content field missing from Gemini response', str(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
) # pragma: no cover
^
pydantic_ai.exceptions.UnexpectedModelBehavior: Content field missing from Gemini response, body:
sdk_http_response=HttpResponse(
headers=<dict len=11>
) candidates=[Candidate(
finish_reason=<FinishReason.RECITATION: 'RECITATION'>,
index=0
)] create_time=None model_version='gemini-2.5-pro' prompt_feedback=None response_id='<my_response_id' usage_metadata=GenerateContentResponseUsageMetadata(
cache_tokens_details=[
ModalityTokenCount(
modality=<MediaModality.TEXT: 'TEXT'>,
token_count=6800
),
],
cached_content_token_count=6800,
prompt_token_count=9939,
prompt_tokens_details=[
ModalityTokenCount(
modality=<MediaModality.TEXT: 'TEXT'>,
token_count=9939
),
],
thoughts_token_count=468,
total_token_count=10407
) automatic_function_calling_history=[] parsed=None```
### Example Code
```Python
# This is the basic framework with which I ran my code, I do not know how to produce a fully reproducible code, as the issue appears at random and I'm not able to share the exact Prompt used.
provider = GoogleProvider(api_key=self.api_key)
agent = Agent(
GoogleModel("gemini-2.5-pro", provider=provider),
model_settings=ModelSettings(max_tokens=1000000),
output_type=SomePydanticModel,
deps_type=AnalysisDeps,
system_prompt=system_prompt,
toolsets=mcp_servers,
)
deps = AnalysisDeps(...)
async with agent:
agent_result = await agent.run(
"my_prompt", deps=deps
)
Python, Pydantic AI & LLM client version
Python 3.13.7 (v3.13.7:bcee1c32211, Aug 14 2025, 19:10:51) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
pydantic-ai==1.1.0
Using gemini-2.5-pro.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working