Skip to content

Commit 1f3b100

Browse files
authored
Retry instead of error when Google response fails with MALFORMED_FUNCTION_CALL or other recoverable finish reason (#3300)
1 parent 674b465 commit 1f3b100

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

pydantic_ai_slim/pydantic_ai/models/google.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,9 @@ def _process_response(self, response: GenerateContentResponse) -> ModelResponse:
471471
raise UnexpectedModelBehavior(
472472
f'Content filter {raw_finish_reason.value!r} triggered', response.model_dump_json()
473473
)
474-
else:
475-
raise UnexpectedModelBehavior(
476-
'Content field missing from Gemini response', response.model_dump_json()
477-
) # pragma: no cover
478-
parts = candidate.content.parts or []
474+
parts = [] # pragma: no cover
475+
else:
476+
parts = candidate.content.parts or []
479477

480478
usage = _metadata_as_usage(response)
481479
return _process_response_from_parts(
@@ -649,17 +647,12 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
649647
# )
650648

651649
if candidate.content is None or candidate.content.parts is None:
652-
if self.finish_reason == 'stop': # pragma: no cover
653-
# Normal completion - skip this chunk
654-
continue
655-
elif self.finish_reason == 'content_filter' and raw_finish_reason: # pragma: no cover
650+
if self.finish_reason == 'content_filter' and raw_finish_reason: # pragma: no cover
656651
raise UnexpectedModelBehavior(
657652
f'Content filter {raw_finish_reason.value!r} triggered', chunk.model_dump_json()
658653
)
659654
else: # pragma: no cover
660-
raise UnexpectedModelBehavior(
661-
'Content field missing from streaming Gemini response', chunk.model_dump_json()
662-
)
655+
continue
663656

664657
parts = candidate.content.parts
665658
if not parts:

0 commit comments

Comments
 (0)