Skip to content

fix(gemini): end a stream with an error when Gemini sends an error chunk - #1062

Open
skybleak333 wants to merge 1 commit into
prism-php:mainfrom
skybleak333:fix/gemini-stream-error-chunk
Open

skybleak333 wants to merge 1 commit into
prism-php:mainfrom
skybleak333:fix/gemini-stream-error-chunk

Conversation

@skybleak333

Copy link
Copy Markdown

Description

When Gemini fails after it has started streaming (the model gets overloaded, an internal error), it sends one more chunk with an error object and closes the stream:

data: {"error": {"code": 503, "message": "The model is overloaded. Please try again later.", "status": "UNAVAILABLE"}}

The Gemini stream handler never looked for error, so it skipped that chunk and ended with StepFinishEvent and StreamEndEvent with FinishReason::Stop. The text cut off mid-sentence looked like a complete answer, and nothing said why.

It now handles the chunk the way the Anthropic stream handles its error event: an ErrorEvent (the status as errorType, the message, the HTTP code in metadata, not recoverable), and the step and the stream end with FinishReason::Error.

Google's SDKs treat this chunk as an error too: python-genai raises on a chunk that starts with {"error": (_api_client.py), js-genai has throwIfStreamErrorChunk.

New test with a text chunk followed by the error chunk; it fails without the change. The full suite, PHPStan, Pint and Rector pass.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant