Skip to content

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

@metaember

Description

@metaember

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 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

Activity

added a commit that references this issue on Jun 18, 2025
d6792d0
metaember

metaember commented on Jun 18, 2025

@metaember
Author

I guess there's a deeper question of why this is not an issue with e.g. 'gemini-1.5-flash' whereas it is with "google-gla:gemini-2.5-pro-preview-05-06", at least this pr fixes the error though

added
bugSomething isn't working
and removed on Jun 18, 2025
self-assigned this
on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @DouweM@metaember

    Issue actions

      Incorrect exception type returned for gemini when token limit is exceeded · Issue #2021 · pydantic/pydantic-ai