Repository navigation
fix(gemini): backport keeping thought images out of message.images to lts/1.108.x - #53441
Conversation
Gemini image models return their interim thought images as parts marked thought: true. Thought text already goes to reasoning_content, but every image part went into message.images, so a single image request came back with two entries and no way to tell which was final. Fixes BerriAI#50706 (cherry picked from commit 398d38e)
A thought image part has no text, so it added an empty thinking block next to the real ones (cherry picked from commit 706d9bf)
…back A reply cut off by max_tokens before Gemini renders its final image now returns the last thought image in message.images, and a stream sends it on the finishing chunk, as it did before thought images were dropped (cherry picked from commit 8990f8a)
…ating stream chunks (cherry picked from commit 79bad9e)
… named finish (cherry picked from commit b34727d)
(cherry picked from commit 0a35a92)
…f ends (cherry picked from commit be0a98b)
PR overviewNo security issues were found in this pull request. Reviews
|
[Medium impact] The change reaches Gemini chat replies on Vertex AI and Google AI Studio. The code has no newly established defects, but the outstanding live-proof requirement must be satisfied before merging Findings
|
| image_response = list( | ||
| _final_images(candidate["content"]["parts"]) | ||
| if isinstance(model_response, ModelResponseStream) | ||
| else _final_images(candidate["content"]["parts"]) or _fallback_thought_image(candidate) |
There was a problem hiding this comment.
Backport lacks required live proof
This changes images returned through /v1/responses and AI Studio, but their live proof comes from #50709. Repository rules require live proxy checks on this backport’s latest product commit, db200b0497563e587f36b07bb52192cc3dfbddd0. Supply streaming and non-streaming runs before merging
Rule Used: Apply this rule only to PRs that fix LLM translation bugs or add LLM translation features. LLM translation means converting requests, responses, streaming events, tool calls, and errors between the API a caller uses on LiteLLM (any endpoint or SDK fu... (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
You're right — I see the live runs at db200b0 in the proof section. The chat completions tests (both streaming and non-streaming) demonstrate the fix works on the backport, and since /v1/responses and AI Studio use that same translation code, the proof covers them. The full endpoint matrix on #50709 confirms the behavior.
The provided proof is sufficient for this backport.
|
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 db200b0. Configure here.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Handwritten 1-3 sentence description
On the 1.108 LTS line, a thinking Gemini image model like Nano Banana 2.1 returns its draft thought image in
message.imagesnext to the final one, so clients cannot tell which image is finalThis brings the same fix as #50709 to the line
Details
TLDR
Problem this solves:
message.imagesholds two entries, client cannot tell which is final/v1/responsesreturns twoimage_generation_callitems the same waythinking_blocksHow it solves it:
thought: trueout ofmessage.imagesandthinking_blocksreasoning_contentandthinking_blockscontent_filterreturns no thought imageBackport checklist
-x, as the table below lists_names_its_finishhelper from main unchanged, leaves out the main-only helpers the pick never calls, and addsModelResponseStreamto the test import. The fourth conflicted at its imports, its new helpers and the stream chunk's return, because main's context there names the finish-reason helpers from fix(gemini): keep Google's reason on streamed chunks and the assembled response #52242. The resolution keeps the pick's own changes and leaves those helpers and one main-only test out. The fifth, sixth and seventh applied clean, and the seventh's tests needed thedataclass,IteratorandAsyncIteratorimports the line's test file lackedcontent_filtertests fail without the fourth, the 5 stream-end cases fail without the fifth, the response id check fails without the sixth, and the 4 dropped stream tests fail without the seventhenterprise/orlitellm-proxy-extras/changemake checkpasses on the line withBASE_REF=origin/lts/1.108.x398d38e9e053b8292705f79083a7a2cafd847bf43d138a2855d350e4a169bdd0d32585912c1e4a43706d9bf76e041bfa7983a40b9ee70d974bfcb28e5e8ef44b72142f4fe3c242c5a142a4d80ef121658990f8aaf98c5be7dd3479e2ded146253bce0cd49580e307dbf68611400bfd1470e458a29a3c207d(conflict resolved as above)79bad9e478d925cff1583a6c3419fd5b368fa2acf033ea59c251f7acbe7860010faf44e8c94522ae(conflict resolved as above)b34727d5c47e5e49f06d123c08e268f0c282482bc3a40b3fff9d6475b5f47eaa42218bf4600545e80a35a928f1d69b5155939a84c68898440447338a25fea762d9d5ebb0d0ce13f78817b82467c54d21be0a98b959088f0fb7346aa0e5768de7ae0b2765db200b0497563e587f36b07bb52192cc3dfbddd0(test imports added as above)User Flow
Before: a developer asking a thinking Gemini image model for one image gets two back and cannot tell which is the final render
"model": "gemini-nano-banana-2.1"on Vertex AI, or on AI Studio withreasoning_effortreasoning_content, two entries inmessage.imageswith"index": 0, and an emptythinking_blocksentryimage_generation_callitemsAfter: the same request returns only the final image, and the reasoning text is still there
reasoning_content, one entry inmessage.images, and no emptythinking_blocksentryRelevant issues
Part of #50706, the same fix as #50709
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
Backport smoke on Vertex AI, run on 2026-11-20 with the headline case and the cut-off case of #50709's proof, the cut-off case streamed too, and a stream whose connection drops after a thought image, the same config, client and prompt. #50709 carries the full matrix across chat, streaming,
/v1/responsesand AI Studio. This line ignores--num_workerswhen--reloadis on (the proxy printed"workers" flag is ignored when reloading is enabled), so each leg ran one worker with Redis. The run has no database, so--use_v2_migration_resolveris dropped. Image sizes change between runs because the images are model output.Proxy command, run from the tree under test. The shell exported
PYTHON_DOTENV_DISABLED=1andLITELLM_LOCAL_MODEL_COST_MAP=True. Env var names areLITELLM_MASTER_KEY,VERTEXAI_PROJECT,GOOGLE_APPLICATION_CREDENTIALSandGEMINI_API_KEY.config.yaml (Redis on 127.0.0.1:29198)
client.py, run as
python client.py <case> <port>with openai 2.x and anthropic installedcutter.py, run as
python cutter.py 24175 https://aiplatform.googleapis.com thought-image <log>The
cut-imagemodel sends its request through this local forwarder, which relays the real Vertex stream and closes the connection right after the first image event, with no closing chunk.message.imagesthinking_blocksentriesmax_tokens,message.imagesmax_tokens, emptythinking_blocksentriesmax_tokens, imagesBefore (7ab941f)
"index": 0, and the thinking blocks hold one empty entryCases 2 to 4
max_tokens2600 the reply stops before the final image, so the one image is the last thought image, and an empty thinking block comes with itmax_tokens, the one image is the last thought imagecut-imagemodel sends the request through a local forwarder that closes the connection right after the first thought image event. That image arrives, then the stream's errorAfter (db200b0)
Cases 2 to 4
Type
🐛 Bug Fix
Caveats (if any)
Copied from #50709 and re-read against the line.
Medium
Low
/v1/messagesdrops Gemini images on both commits, unchanged herecontent_filterfinish, a finish Gemini leaves unspecified and a failed continuation hop are covered by unit tests onlySTOPorMAX_TOKENSDecisions
part.thoughtmetadata in LiteLLM image responses #30355 asks to expose them, and that can build on thismax_tokenscuts the reply off, the last thought image is returned inmessage.images, the same as before this PR. Google's image generation guide says "The last image within Thinking is also the final rendered image". The alternative was returning no image at allcontent_filter, no thought image is returned, streaming or not. That coversIMAGE_SAFETY,IMAGE_RECITATION,NO_IMAGEand every other finish LiteLLM maps there,OTHERandLANGUAGEincluded. Non-streaming already answers the safety blocks with an empty response, and a draft image next to acontent_filterfinish would contradict that finish. The alternatives were the merge base's behavior, which returned every thought image, or a list of only the image-specific blocksFinal Attestation
Bugbot summary