Skip to content

Close #2021: correctly raise UsageLimitExceeded for google #2022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pydantic_ai_slim/pydantic_ai/models/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing_extensions import assert_never

from .. import UnexpectedModelBehavior, _utils, usage
from ..exceptions import UserError
from ..exceptions import UsageLimitExceeded, UserError
from ..messages import (
BinaryContent,
FileUrl,
Expand Down Expand Up @@ -289,7 +289,9 @@ def _process_response(self, response: GenerateContentResponse) -> ModelResponse:
if not response.candidates or len(response.candidates) != 1:
raise UnexpectedModelBehavior('Expected exactly one candidate in Gemini response') # pragma: no cover
if response.candidates[0].content is None or response.candidates[0].content.parts is None:
if response.candidates[0].finish_reason == 'SAFETY':
if response.candidates[0].finish_reason == 'MAX_TOKENS':
raise UsageLimitExceeded(str(response))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UsageLimitExceeded was made specifically for limits specified using UsageLimits: https://ai.pydantic.dev/agents/#usage-limits

What do you think about continuing to raise UnexpectedModelBehavior here, but setting a custom message indicating the specified max_tokens limit was hit?

PS. I agree UnexpectedModelBehavior isn't really the best name in this case, perhaps just ModelError would be better, but that's a change we can make separately.

elif response.candidates[0].finish_reason == 'SAFETY':
raise UnexpectedModelBehavior('Safety settings triggered', str(response))
else:
raise UnexpectedModelBehavior(
Expand Down
13 changes: 12 additions & 1 deletion tests/models/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing_extensions import TypedDict

from pydantic_ai.agent import Agent
from pydantic_ai.exceptions import ModelRetry, UnexpectedModelBehavior, UserError
from pydantic_ai.exceptions import ModelRetry, UnexpectedModelBehavior, UsageLimitExceeded, UserError
from pydantic_ai.messages import (
AudioUrl,
BinaryContent,
Expand Down Expand Up @@ -585,6 +585,17 @@ async def test_google_model_safety_settings(allow_model_requests: None, google_p
await agent.run('Tell me a joke about a Brazilians.')


async def test_google_model_usage_limit_exceeded(allow_model_requests: None, google_provider: GoogleProvider):
# this passes with 'gemini-1.5-flash' but fails with 'google-gla:gemini-2.5-pro-preview-05-06'
m = GoogleModel('google-gla:gemini-2.5-pro-preview-05-06', provider=google_provider)
agent = Agent(m, model_settings=dict(max_tokens=1))

with pytest.raises(UsageLimitExceeded):
await agent.run(
'Write me a two paragraph essay about the history of the internet.',
)


async def test_google_model_empty_user_prompt(allow_model_requests: None, google_provider: GoogleProvider):
m = GoogleModel('gemini-1.5-flash', provider=google_provider)
agent = Agent(m, instructions='You are a helpful assistant.')
Expand Down
Loading