Skip to content

Incorrect exception type returned for gemini when token limit is exceeded #2021

Open
@metaember

Description

@metaember

Initial Checks

Description

When manually passing a token limit to the agent that's too restrictive and causes the model run to fail, the error we raise is of the wrong type

from pydantic_ai import Agent

Agent("google-gla:gemini-2.5-pro-preview-05-06", model_settings=dict(max_tokens=5)).run_sync("write a haiku")

yields

UnexpectedModelBehavior: Content field missing from Gemini response, body: (...)

instead of UsageLimitExceeded

Example Code

from pydantic_ai import Agent
Agent("google-gla:gemini-2.5-pro-preview-05-06", model_settings=dict(max_tokens=5)).run_sync("write a haiku")

Python, Pydantic AI & LLM client version

Python 3.12.4
Pydantic 0.3.1
google-gla:gemini-2.5-pro-preview-05-06

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