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
2 changes: 2 additions & 0 deletions pydantic_ai_slim/pydantic_ai/models/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
if candidate.finish_reason == 'STOP': # pragma: no cover
# Normal completion - skip this chunk
continue
elif candidate.finish_reason == 'MALFORMED_FUNCTION_CALL': # pragma: no cover
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a test for this, based on a real-world response with this finish reason?

Is skipping the chunk the right thing to do? Would any more chunks follow or would this be the last one in the response? Is it valid to just move on and ignore the error?

If the response before this failure is empty, we retry the request, which I suppose is the goal:

# Go back to the model request node with an empty request, which means we'll essentially
# resubmit the most recent request that resulted in an empty response,
# as the empty response and request will not create any items in the API payload,
# in the hope the model will return a non-empty response this time.
ctx.state.increment_retries(ctx.deps.max_result_retries)
self._next_node = ModelRequestNode[DepsT, NodeRunEndT](_messages.ModelRequest(parts=[]))

continue
elif candidate.finish_reason == 'SAFETY': # pragma: no cover
raise UnexpectedModelBehavior('Safety settings triggered', str(chunk))
else: # pragma: no cover
Expand Down