Skip to content
Open
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
11 changes: 8 additions & 3 deletions pydantic_ai_slim/pydantic_ai/models/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .._output import OutputObjectDefinition
from .._run_context import RunContext
from ..builtin_tools import CodeExecutionTool, ImageGenerationTool, UrlContextTool, WebSearchTool
from ..exceptions import UserError
from ..exceptions import ModelHTTPError, UserError
from ..messages import (
BinaryContent,
BuiltinToolCallPart,
Expand Down Expand Up @@ -50,7 +50,7 @@
)

try:
from google.genai import Client
from google.genai import Client, errors
from google.genai.types import (
BlobDict,
CodeExecutionResult,
Expand Down Expand Up @@ -385,7 +385,12 @@ async def _generate_content(
) -> GenerateContentResponse | Awaitable[AsyncIterator[GenerateContentResponse]]:
contents, config = await self._build_content_and_config(messages, model_settings, model_request_parameters)
func = self.client.aio.models.generate_content_stream if stream else self.client.aio.models.generate_content
return await func(model=self._model_name, contents=contents, config=config) # type: ignore
try:
return await func(model=self._model_name, contents=contents, config=config) # type: ignore
except errors.APIError as e:
if (status_code := e.code) >= 400:
Copy link

Choose a reason for hiding this comment

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

Bug: Unhandled Exception in Error Handling

The try...except block can raise a TypeError if e.code is None for an APIError. The comparison (status_code := e.code) >= 400 attempts to compare None with an integer, causing an unhandled exception when an APIError lacks a status code.

Fix in Cursor Fix in Web

raise ModelHTTPError(status_code=status_code, model_name=self._model_name, body=e.details) from e
raise # pragma: lax no cover

async def _build_content_and_config(
self,
Expand Down
Loading