Repository navigation
fix(gemini): keep Google's reason on streamed chunks and the assembled response - #52242
Conversation
…and record finishes one way
[Medium impact] The reviewed changes appear safe to merge, with both previous findings addressed.
|
|
bugbot run |
Merging this PR will not alter performance1 warning
Comparing |
|
bugbot run |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 25d3e9c. Configure here.
Handwritten 1-3 sentence description
googles response says why it stopped: a safety block, a copyright block, a blocked prompt, or a normal finish.
a non-streamed gemini answer already had a field with that reason, and this PR adds the same field to the last chunk of every streamed answer.
Details
TLDR
Intentional product change: every streamed Gemini answer gains a
provider_specific_fieldsobject on its last chunk. Non-streamed answers are unchanged.User Flow
Before: a developer streaming Gemini through the proxy can't tell a safety block from a recitation block or a blocked prompt
"stream": trueto a Gemini model, and Google filters the answer"finish_reason": "content_filter"with nothing else on the choicelitellm.stream_chunk_builderand the choice still has no reasonAfter: the same stream tells them exactly what Google said
"finish_reason": "content_filter"and"provider_specific_fields": {"native_finish_reason": "SAFETY"}, the same as the non-streamed answer{"block_reason": "SAFETY"}instead, and a normal answer shows{"native_finish_reason": "STOP"}litellm.stream_chunk_buildercarries the same fields on its choiceRelevant issues
Related to #51613. The non-streamed half was closed by #52208. This also covers #47577, where a streamed MALFORMED_FUNCTION_CALL showed up as a plain "stop".
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Live against real Vertex AI
gemini-3.8-flash. Before is the merge baseea96ea3db59, After is the tip25d3e9cd429. Each proxy ran with one worker and no Redis, because this change only touches how a provider's stream is translated and shares no router or spend state between workers. Both proxies ran withPYTHON_DOTENV_DISABLED=1so neither picked up an unrelated.envfrom a parent folder.Google never blocked a harmless prompt at the prompt level during testing, so the blocked-prompt case (
block_reason) is proven by the scripted-upstream integration tests below, not live. Google also sent no free-text note in these runs, so the tests that check a note never reaches the caller use scripted upstreams too.Setup, the same for both commits. Env vars
VERTEXAI_PROJECTandVERTEXAI_LOCATIONcome from the shell, and Vertex auth is gcloud application default credentials.config.yaml and proxy command
Before (ea96ea3)
Streamed answer that Google filters
content_filterand gives no reason.Streamed normal answer
"messages": [{"role": "user", "content": "Say hello in one word."}]and nosafety_settings.stopand gives no reason. The non-streamed answer to the same prompt does carry{"native_finish_reason": "STOP"}.After (25d3e9c)
Streamed answer that Google filters
Streamed normal answer
STOP, matching the non-streamed answer. The thought-signature chunk before it is unchanged and doesn't get a copy of the reason.Also checked live at the tip, all as expected. A streamed tool call ends
tool_callswith{"native_finish_reason": "STOP"}. A filtered stream withstream_options.include_usagestill sends its usage chunk. Streamed/v1/messagesand/v1/responseswith the filtered prompt are unchanged event for event.Red/green tests
The tests were written before the fix and run by Devin with scripted Gemini and Vertex upstreams, using the tip's test files against both the base code and the tip. Each regression failed on base on the missing reason, then passed three runs in a row at the tip. Each guard passed at both.
ea96ea3db5925d3e9cd429, 3 runstest_gemini_content_filter_reason_wire.py::test_regression_streamed_finish_chunk_and_assembled_choice_carry_google_reasontest_gemini_content_filter_reason_wire.py::test_regression_streamed_and_non_streamed_google_reason_agreetest_vertex_and_google_ai_studio_gemini.py::test_streamed_finish_chunk_and_rebuilt_choice_carry_google_reason(sync and async)test_streaming_chunk_builder_utils.py::test_stream_chunk_builder_keeps_the_final_choices_provider_specific_fields/v1/messages,/v1/responses, plainopenai/stream)Unit tests for the changed files pass locally, and each new unit test fails when its piece of the fix is reverted.
Type
🐛 Bug Fix
Caveats (if any)
Medium
provider_specific_fieldssent on a finish chunk now move to the final chunkstream_chunk_buildernow keeps them on its choiceLow
Final Attestation