Repository navigation
fix(gemini): replay each assistant image's thought signature on its own part - #53435
Merged
mateo-berri merged 13 commits intoNov 22, 2026
Merged
mateo-berri merged 13 commits into
mateo-berri merged 13 commits into
Conversation
…wn part Gemini image models sign every final image part and only accept a signature replayed inside its original part. Each generated image now carries its own signature in images[].provider_specific_fields.thought_signature, and the history converter sends it back on that image part. History from before this change falls back to the message-level list, right-aligned onto the images, and the text part keeps the first signature no image or tool call claimed
…pt unsigned image fields A streamed reply saved before per-image signatures lists only the text's signature, and the history fallback moved it onto the image. The text now keeps the first unused message signature as before, and saved images take only what is left, matched from the end thought_signature is optional inside an image's provider_specific_fields, so a response whose image carries other provider fields, or none, parses again
The shared response type no longer declares an image's provider_specific_fields, so an upstream response whose image carries other keys there, or a non-string signature, validates exactly as it did before. Gemini's own image items still type the signature
…history_thought_signature # Conflicts: # tests/unit/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py
…history_thought_signature # Conflicts: # tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
Contributor
4 of 7 tasks
Contributor
4 of 6 tasks
Contributor
[Medium impact] The PR appears safe to merge, with the previous numbered finding addressed and no new actionable issues found.
|
…history_thought_signature # Conflicts: # litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
…p list text signatures off images
Contributor
Contributor
…_params in batch Text models reject an image model's signature on a replayed image with a 400 that the placeholder resend does not catch, so images go unsigned unless the target outputs images. An empty assistant reply keeps its text signature, Vertex batch lines honor drop_params, and the malformed history error names the right provider.
…rop_params for malformed images
…history_thought_signature # Conflicts: # litellm/llms/vertex_ai/files/transformation.py
Contributor
Contributor
Contributor
|
bugbot run |
Contributor
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 670cad9. Configure here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Handwritten 1-3 sentence description
Gemini image models on Vertex now keep the picture when a chat asks for an edit in the next turn. LiteLLM used to drop the signature Google puts on each generated image when the chat history came back, so Vertex ignored the old picture and drew a new one, and a third turn failed with a 400
Details
TLDR
Problem this solves:
How it solves it:
thought_signatureDesign decisions
message.imagesgetsprovider_specific_fields.thought_signature, and the message-levelprovider_specific_fields.thought_signatureslist stays as it is. Moving signatures off the list was the alternative, but clients already read it. Matching the list to images by position alone was the other, and saved history already works that way (1492 prompt tokens). The per-image copy is still needed for streamed replies, which list only the text's signature, and for clients that drop or reorder images. The cost is that an image's signature (1.5 to 2.7 MB in these runs) appears twice in the response, so a text plus image reply grew from 4.32 MB to 6.05 MBcontentis a list. A saved reply with images whosecontentis""or[]counts as image only, so its image takes the signature instead of an empty text part. Without images, the empty text part keeps its signature as before. Dropping the list was the alternative, which keeps the bug for every saved chatdrop_paramsrule, on any assistant message, with or without images. An image entry orimage_urlthat is not an object, an image URL that is not a string, an image's own signature that is not a string, or athought_signaturesentry that is not a string, answers 400 "Invalid assistant message", and withdrop_paramsthe bad entry is dropped and the rest is sent. A stringimage_urlis read as the URL, where base skipped the image. Base ignored a bad signature entry and answered 500 for a number as the URL. An empty URL is skipped as before. Vertex batch lines honor the line's or the deployment'sdrop_params. Silently dropping both was the alternative, which hides a broken clientmessage.imagesand merged first, so its test now expects the final image with its own signatureprovider_specific_fields.thought_signature. The shared response type leaves it undeclared, so a response whose image carries other keys there, or a non-string signature, validates exactly as before. Declaring it on the shared type was the alternative, and it made those responses fail validationvertex_ai/andgemini/deployments, a Vertex image signature replayed to AI Studio gets a 400, and the resend with Google's placeholder signature from fix(gemini): resend once with the placeholder signature when the other backend rejects a replayed thought signature #49697 turns that into a 200 after one more call. Sending each image's signature to both backends keeps every same-backend edit working, which is the case this fixes. Leaving images unsigned forgemini/was the alternative, and Google's Gemini 3 docs say image signatures are validated strictly there too. The lts/1.108.x twin ports only that chat completions resend, since the line does not have fix(gemini): resend once with the placeholder signature when the other backend rejects a replayed thought signature #49697modeimage_generationorimageinsupported_output_modalities). Text models such asgemini-3.8-flashanswer 400 "Request contains an invalid argument" to an image model's signature, which the fix(gemini): resend once with the placeholder signature when the other backend rejects a replayed thought signature #49697 resend does not catch. Models the cost map lacks get unsigned images, as on base, andmodel_info: {mode: image_generation}on the deployment turns signing on. Signing for every target was the alternative, and it broke asking a text model about a drawn pictureprovider_specific_fields, the same way the message-level field was already stripped for them. Keeping it was the alternative, and it sent them a 1.9 MB signature they cannot useUser Flow
Before: a developer edits a Vertex Gemini picture over chat, and every edit comes back as an unrelated new picture
gemini-3.1-flash-imageasking for a cartoon cat on a chair, and get text plus one imageAfter: the same steps edit the same picture
provider_specific_fields.thought_signatureRelevant issues
Fixes #52543
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
Each leg boots one 2-worker proxy (
--num_workers 2, one instance) from the commit under test and calls real Vertex AI and Google AI Studio. Each cell isusage.prompt_tokenson the edit turn, which shows whether the model read the earlier picture. "Direct to Google" sends the same two turns straight togenerateContent, replaying the model's turn as Google returned it. "History saved before this fix" sends turn 1 through the before proxy and the edit through the after proxy. Commands are the lines the shell printed, with each proxy's throwaway master key shown as<master key>Vertex flash, text + image. Before draws a new room, and after only paints the chair blue
Setup: proxy start, config.yaml, case script, SDK client, direct script
Env var names:
VERTEXAI_PROJECT,GOOGLE_APPLICATION_CREDENTIALS,GEMINI_API_KEY,LITELLM_MASTER_KEY,LITELLM_LOCAL_MODEL_COST_MAP=True,PYTHON_DOTENV_DISABLED=1,LITELLM_DANGEROUSLY_PERMIT_WEAK_OR_UNSET_MASTER_KEY=truePorts: before 22045, after 30962, plus 25954, a second before proxy at bec8b46 that made the saved-history first turn
cell.sh <case> <port> <out dir> [<turn 1 port>]sdk_client.py <port>, run with openai 3.57.0direct.py <vertex|studio> <model> <text|image> <out dir>Last updated: 670cad9. QA ran on d7574b3, before leg on bec8b46. Later commits change only malformed history, strict providers and main's video_url fix, which no case below sends. Unit tests and 17 head Live PR risk cells matched ba001d436e3 at a4dac89, and the 12 malformed history and SDK cells matched again at 670cad9
Before (bec8b46, main merge base)
Vertex flash, text + image
Vertex flash, image only
Vertex flash, streamed first turn
Vertex flash, three turns
OpenAI Python SDK
Vertex Nano Banana, run 1
Vertex Nano Banana, run 2
Vertex Nano Banana, run 3
AI Studio flash, text + image
Vertex text-only model
After (d7574b3)
Vertex flash, text + image
Vertex flash, image only
Vertex flash, streamed first turn
Vertex flash, three turns
OpenAI Python SDK
Vertex Nano Banana, run 1
Vertex Nano Banana, run 2
Vertex Nano Banana, run 3
AI Studio flash, text + image
Vertex text-only model
History saved before this fix
Direct to Google
Vertex flash text + image, Vertex flash image only, Vertex Nano Banana, AI Studio flash text + image
Surprises
Type
🐛 Bug Fix
Caveats (if any)
Medium
vertex_ai/andgemini/deployments pay the extra upload and round trip (design decision 10)model_info: {mode: image_generation}on the deployment turns signing onLow
thought_signaturesentry now answers 400 on text-only Gemini chats too, where base sent the valid signature.drop_paramsdrops it insteadbody.modelis an alias send unsigned images. Proxy uploads rewrite rows to the deployment model, so mostly direct SDK batch users hit this. Filed as a follow-upLive PR risk
Blast radius, base vs head, run at 9a2db67, acdf68a, d7574b3 and ba001d4, head side at a4dac89 and 670cad9
No dependent path breaks for valid history. Each side ran from its own worktree (base bec8b46, head 9a2db67) with 2 workers, the same config, its own Postgres database and Redis, and a recording forwarder in front of Vertex AI and AI Studio, so the requests litellm sent to Google were compared too. Cells covered text plus image, saved histories from before this fix (text signature only, list content, a streamed reply, a Nano Banana draft and final), odd per-image signatures (empty, number, list, 5 KB string, one image listed twice), malformed history (a number as the image URL, an object in
thought_signatures, with and withoutdrop_params), Vertex to AI Studio and back, the response cache,/v1/responses,/v1/messages, text streaming, key create and delete, and spend logsAt acdf68a these cells ran again on both sides (base bec8b46), each replaying a saved first turn with one change. Each number is the edit turn's prompt tokens on a 200. Commands and outputs
thought_signaturesdrop_paramsFor the saved reply with empty content, base sent the signature on an empty text part and head sends it on the image. For a Vertex image-only reply sent to AI Studio, head made two calls (a 400, then the placeholder resend) and base made one, in both runs
At d7574b3 these cells ran on both sides (base bec8b46). Each number is the last turn's prompt tokens on a 200. Commands and outputs
thought_signaturesdrop_paramsWith a text model as the target, both sides send the image unsigned, where acdf68a answered 400. In the tools chat, base ignored the picture on turn 2 and drew a new one instead of calling the tool, so its turn 3 could not be built.
At ba001d4 these cells ran on both sides (base bec8b46), with recording forwarders in front of Fireworks and Mistral too. Each number is the edit turn's prompt tokens, or the status for Fireworks and Mistral. Commands and outputs
image_urlimage_urldrop_paramsdrop_paramsdrop_paramsmode: chatdeploymentthought_signaturesdrop_paramsFireworks got 4,597,164 bytes with the image's signature on base and 2,708,785 bytes without it on head. Mistral answers 422
extra_forbiddenonimageson both sides, a pre-existing bug filed separately. Themode: chatdeployment registersgemini-3.1-flash-imagewithmodel_info: {mode: chat}, and head still signs the imagelitellm SDK leg
Base
Head
Breaking
None. Every valid-history cell answered the same status on both sides. The requests to Google differ only where this PR says they should, since a replayed image part now carries its signature. Saved histories from before this fix send the same parts as base, apart from the Nano Banana final image, which now gets its own signature. List content sends no signature on the text or the image, on both sides
Backward incompatible
Malformed assistant history now answers 400 unless
drop_paramsis set, on any assistant message with or without images, per litellm'sdrop_paramsrule (design decision 5). An object insideprovider_specific_fields.thought_signaturesanswered 200 on base, which ignored it, and now answers 400 "Invalid assistant message: provider_specific_fields.thought_signatures entries must be strings". Withdrop_params: truethe entry is dropped and the edit keeps the picture (1490 prompt tokens, base 53). A number asimages[].image_url.urlanswered 500 "argument of type 'int' is not iterable" on base and now answers 400. An image entry orimage_urlthat is not an object, or an image's own signature that is not a string, was skipped on base (53 prompt tokens) and now answers 400 unlessdrop_paramsis setEach image in a response gains
provider_specific_fields.thought_signature, and the same signature stays in the message'sthought_signatureslist, so a text plus image response grew from about 4.8 MB to 6.5 MB. This is design decision 2, kept on purpose and open for the reviewerImage
provider_specific_fieldsvalidates exactly as on base, since only Gemini's own image type declares the signature. An OpenAI-compatible upstream that sends other keys there, or a non-string signature, still passes throughRegression risk
A replayed text plus image turn sent to Google grew from 2.71 MB to 4.60 MB, because the image's signature now goes with it. That is the fix. An empty per-image signature counts as missing, so the image falls back to the message's list and keeps the picture (1490 prompt tokens, base 53). A number or a list there answers 400, and with
drop_paramsit counts as missing too. A wrong string is sent as is, and Vertex then ignores the picture, which is what base does for every replayed image (53 on both sides). A Vertex image-only reply replayed to AI Studio now sends the image's Vertex signature, which AI Studio rejects, so litellm resends once with the placeholder (#49697) and answers 200 (283 prompt tokens on both sides). Vertex batch and the OpenCode provider use the same history converter and were not runDependency graph
drop_paramsmode: chat/v1/responsesimage output/v1/messagesadapterNot verified
Vertex batch (
/v1/batches) and the OpenCode provider were not run. Vertex context caching could not run, since Vertex answers 400 "does not support explicit context caching" forgemini-3.1-flash-image(seen in the earlier run at cad8b63)Final Attestation
Bugbot summary