Skip to content

fix(gemini): replay each assistant image's thought signature on its own part - #53435

Merged
mateo-berri merged 13 commits into
BerriAI:mainfrom
chaznm:litellm_gemini_image_history_thought_signature
Nov 22, 2026
Merged

mateo-berri merged 13 commits into
BerriAI:mainfrom
chaznm:litellm_gemini_image_history_thought_signature

Conversation

@chaznm

@chaznm chaznm commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Vertex image edits draw a brand new picture every turn
  • The third image turn on Vertex fails with a 400
  • Streamed images never carried their signature at all

How it solves it:

  • Each returned image now carries its own thought_signature
  • A replayed image sends that signature on its own part
  • Chats saved before this fix fall back to the message's signature list
Design decisions
  1. Built fresh instead of on fix(vertex_ai): keep the thought signature on a replayed assistant image #52554. That PR fixes only image-only replies. For text plus image it sends the text's signature on the text and drops the image's own, and for Nano Banana it gives the final image's signature to the unsigned draft. It closes as superseded once this opens
  2. Each image in message.images gets provider_specific_fields.thought_signature, and the message-level provider_specific_fields.thought_signatures list 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 MB
  3. The text part gets the first message signature that no image's own signature and no tool call uses. For a message without per-image signatures that is exactly today's rule, so the text replays as before apart from the malformed case in item 5
  4. History saved before this fix has no per-image signature. The images then take the message signatures the text and tool calls did not use, matched from the end. Matching from the end gives the signature to the final image and leaves an earlier draft unsigned. A streamed reply saved before this fix lists only the text's signature, so its image stays unsigned as before, also when content is a list. A saved reply with images whose content is "" 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 chat
  5. Malformed history follows litellm's drop_params rule, on any assistant message, with or without images. An image entry or image_url that is not an object, an image URL that is not a string, an image's own signature that is not a string, or a thought_signatures entry that is not a string, answers 400 "Invalid assistant message", and with drop_params the bad entry is dropped and the rest is sent. A string image_url is 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's drop_params. Silently dropping both was the alternative, which hides a broken client
  6. Some older Nano Banana replies list a signature for every image and none for the text. Their text still gets the first one, as it does today, and the final image gets its own
  7. Out of scope are the Responses API and Anthropic Messages bridges, the text signature for list content, and the message-level list on streamed image chunks. The per-image signature already fixes replay for streamed images
  8. fix(gemini): keep thought images out of message.images #50709 keeps Nano Banana's thought draft out of message.images and merged first, so its test now expects the final image with its own signature
  9. Only Gemini's own image type declares provider_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 validation
  10. A signature only validates on the backend that made it, and saved history cannot say which one did. When a model group mixes vertex_ai/ and gemini/ 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 for gemini/ 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 #49697
  11. An image's signature goes back only when the target model outputs images, per the cost map (mode image_generation or image in supported_output_modalities). Text models such as gemini-3.8-flash answer 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, and model_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 picture
  12. Providers that reject unknown message fields (Groq, Fireworks, FriendliAI, Databricks, Mistral, Together) get the replayed image without provider_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 use

User Flow

Before: a developer edits a Vertex Gemini picture over chat, and every edit comes back as an unrelated new picture

  1. They send POST /v1/chat/completions to gemini-3.1-flash-image asking for a cartoon cat on a chair, and get text plus one image
  2. They send that assistant message back unchanged with "Make the chair blue. Keep everything else the same."
  3. The reply is a different cat in a different room, at about 60 prompt tokens, so the old picture was never read
  4. A third turn answers 400 "Image part is missing a thought_signature"

After: the same steps edit the same picture

  1. They send the same first request, and each returned image now has provider_specific_fields.thought_signature
  2. They send that assistant message back unchanged with the same edit request
  3. The reply is the same cat and room with a blue chair, at about 1,500 prompt tokens, like a direct Vertex call
  4. The third turn succeeds

Relevant issues

Fixes #52543

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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 more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays 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 is usage.prompt_tokens on the edit turn, which shows whether the model read the earlier picture. "Direct to Google" sends the same two turns straight to generateContent, 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>

Case Direct to Google Before After
Vertex flash, text + image 1469 53 1528
Vertex flash, image only 1523 22 1471
Vertex flash, streamed first turn 1469 55 1506
Vertex flash, third turn n/a 400 error 2904
OpenAI Python SDK 1469 56 1544
History saved before this fix 1469 n/a 1492
Vertex Nano Banana, run 1 2373 130 2759
Vertex Nano Banana, run 2 2373 91 2438
Vertex Nano Banana, run 3 2373 119 2556
AI Studio flash, text + image 315 325 318
Vertex text-only model n/a answer 22 answer 22

Vertex flash, text + image. Before draws a new room, and after only paints the chair blue

Before and after

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=true

python litellm/proxy/proxy_cli.py --config config.yaml --port <port> --num_workers 2 --timeout_worker_healthcheck 300

Ports: before 22045, after 30962, plus 25954, a second before proxy at bec8b46 that made the saved-history first turn

model_list:
  - model_name: gemini-3.1-flash-image
    litellm_params:
      model: vertex_ai/gemini-3.1-flash-image
      vertex_project: os.environ/VERTEXAI_PROJECT
      vertex_location: global
  - model_name: gemini-nano-banana-2.1
    litellm_params:
      model: vertex_ai/gemini-nano-banana-2.1
      vertex_project: os.environ/VERTEXAI_PROJECT
      vertex_location: global
  - model_name: studio-gemini-3.1-flash-image
    litellm_params:
      model: gemini/gemini-3.1-flash-image
      api_key: os.environ/GEMINI_API_KEY
  - model_name: gemini-3.8-flash
    litellm_params:
      model: vertex_ai/gemini-3.8-flash
      vertex_project: os.environ/VERTEXAI_PROJECT
      vertex_location: global

cell.sh <case> <port> <out dir> [<turn 1 port>]

#!/bin/zsh
# usage: cell.sh <cell> <port> <outdir> [<turn1 port>]
set -e
C=$1; P=$2; O=$3/$1; T1P=${4:-$2}; mkdir -p $O
run() { printf '$ %s\n' "$1"; eval "$1"; }
DRAW_TEXT="First write one sentence describing what you will draw, then generate an image of a cartoon cat on a chair."
DRAW_ONLY="Generate an image of a cartoon cat on a chair."
REV="Make the chair blue. Keep everything else the same."
REV2="Now make the cat orange. Keep everything else the same."
STREAM=false
case $C in
  flash-text-image) M=gemini-3.1-flash-image; FIRST=$DRAW_TEXT ;;
  flash-image-only) M=gemini-3.1-flash-image; FIRST=$DRAW_ONLY ;;
  nano-draft-final) M=gemini-nano-banana-2.1; FIRST=$DRAW_ONLY ;;
  studio-text-image) M=studio-gemini-3.1-flash-image; FIRST=$DRAW_TEXT ;;
  flash-stream) M=gemini-3.1-flash-image; FIRST=$DRAW_TEXT; STREAM=true ;;
  flash-three-turns) M=gemini-3.1-flash-image; FIRST=$DRAW_TEXT ;;
  legacy-history) M=gemini-3.1-flash-image; FIRST=$DRAW_TEXT ;;
  text-only) M=gemini-3.8-flash; FIRST="Name one prime number between 10 and 20. Answer with the number only."; REV="Double it. Answer with the number only." ;;
esac
K1=$LITELLM_MASTER_KEY; K=$LITELLM_MASTER_KEY
jq -n --arg m "$M" --arg p "$FIRST" --argjson s $STREAM '{model: $m, stream: $s, messages: [{role: "user", content: $p}]}' > $O/turn1_request.json
if [[ $STREAM == true ]]; then
  run "curl -sS http://127.0.0.1:$T1P/v1/chat/completions -H 'Authorization: Bearer $K1' -H 'Content-Type: application/json' -d @$O/turn1_request.json > $O/turn1.sse"
  sed -n 's/^data: //p' $O/turn1.sse | grep -v '^\[DONE\]' | jq -s '{choices: [{message: {role: "assistant", content: ([.[].choices[0].delta.content // empty] | join("") | if . == "" then null else . end), images: [.[].choices[0].delta.images // empty | .[]], provider_specific_fields: {thought_signatures: [.[].choices[0].delta.provider_specific_fields.thought_signatures // empty | .[]]}}}], usage: (map(select(.usage)) | last | .usage)}' > $O/turn1.json
else
  run "curl -sS http://127.0.0.1:$T1P/v1/chat/completions -H 'Authorization: Bearer $K1' -H 'Content-Type: application/json' -d @$O/turn1_request.json > $O/turn1.json"
fi
run "jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // \"\" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' $O/turn1.json"
jq --arg m "$M" --arg p "$FIRST" --arg r "$REV" '{model: $m, messages: [{role: "user", content: $p}, .choices[0].message, {role: "user", content: $r}]}' $O/turn1.json > $O/turn2_request.json
run "curl -sS http://127.0.0.1:$P/v1/chat/completions -H 'Authorization: Bearer $K' -H 'Content-Type: application/json' -d @$O/turn2_request.json > $O/turn2.json"
run "jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' $O/turn2.json"
if [[ $C == flash-three-turns ]]; then
  jq --arg r "$REV2" '.messages += [input.choices[0].message, {role: "user", content: $r}]' $O/turn2_request.json $O/turn2.json > $O/turn3_request.json
  run "curl -sS http://127.0.0.1:$P/v1/chat/completions -H 'Authorization: Bearer $K' -H 'Content-Type: application/json' -d @$O/turn3_request.json > $O/turn3.json"
  run "jq -c '{third_turn_prompt_tokens: .usage.prompt_tokens, images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' $O/turn3.json"
fi
for t in turn1 turn2 turn3; do [[ -f $O/$t.json ]] && jq -r '.choices[0].message.images[-1].image_url.url // empty' $O/$t.json | sed 's/^data:image\/[a-z]*;base64,//' | base64 -d > $O/$t.png 2>/dev/null || true; done

sdk_client.py <port>, run with openai 3.57.0

import os
import sys

from openai import OpenAI

port = sys.argv[1]
client = OpenAI(base_url=f"http://127.0.0.1:{port}/v1", api_key=os.environ["LITELLM_MASTER_KEY"])
messages = [{"role": "user", "content": "First write one sentence describing what you will draw, then generate an image of a cartoon cat on a chair."}]
first = client.chat.completions.create(model="gemini-3.1-flash-image", messages=messages)
print("first turn images:", len(first.choices[0].message.images or []))
messages.append(first.choices[0].message)
messages.append({"role": "user", "content": "Make the chair blue. Keep everything else the same."})
revision = client.chat.completions.create(model="gemini-3.1-flash-image", messages=messages)
print("revision prompt tokens:", revision.usage.prompt_tokens)
print("revision images:", len(revision.choices[0].message.images or []))

direct.py <vertex|studio> <model> <text|image> <out dir>

import json, os, sys
import google.auth, google.auth.transport.requests, requests

backend, model, kind, out = sys.argv[1:5]
prompts = {"text": "First write one sentence describing what you will draw, then generate an image of a cartoon cat on a chair.",
           "image": "Generate an image of a cartoon cat on a chair."}
if backend == "vertex":
    creds, _ = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
    creds.refresh(google.auth.transport.requests.Request())
    url = f"https://aiplatform.googleapis.com/v1/projects/{os.environ['VERTEXAI_PROJECT']}/locations/global/publishers/google/models/{model}:generateContent"
    headers = {"Authorization": "Bearer " + creds.token}
else:
    url = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent"
    headers = {"x-goog-api-key": os.environ["GEMINI_API_KEY"]}
config = {"responseModalities": ["TEXT", "IMAGE"]}
user1 = {"role": "user", "parts": [{"text": prompts[kind]}]}
first = requests.post(url, headers=headers, json={"contents": [user1], "generationConfig": config}, timeout=300).json()
model_turn = first["candidates"][0]["content"]
shape = [("thought-" if p.get("thought") else "") + ("image" if "inlineData" in p else "text") + ("+sig" if "thoughtSignature" in p else "") for p in model_turn["parts"]]
user2 = {"role": "user", "parts": [{"text": "Make the chair blue. Keep everything else the same."}]}
second = requests.post(url, headers=headers, json={"contents": [user1, model_turn, user2], "generationConfig": config}, timeout=300).json()
os.makedirs(out, exist_ok=True)
json.dump({"first": first, "second": second}, open(os.path.join(out, f"direct_{backend}_{model}_{kind}.json"), "w"))
print(json.dumps({"backend": backend, "model": model, "first_turn_parts": shape, "revision_prompt_tokens": second.get("usageMetadata", {}).get("promptTokenCount"), "error": second.get("error", {}).get("message")}))

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
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/flash-text-image/turn1_request.json > base7/flash-text-image/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' base7/flash-text-image/turn1.json
{"content":"I will generate a cheerful cartoon image of an orange tabby ","images":[{"url_chars":2469874,"own_signature_chars":0}],"message_signature_chars":[172,1705648],"error":null}
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/flash-text-image/turn2_request.json > base7/flash-text-image/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' base7/flash-text-image/turn2.json
{"revision_prompt_tokens":53,"content":null,"images":[2397086],"error":null}
Vertex flash, image only
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/flash-image-only/turn1_request.json > base7/flash-image-only/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' base7/flash-image-only/turn1.json
{"content":null,"images":[{"url_chars":2394550,"own_signature_chars":0}],"message_signature_chars":[1609068],"error":null}
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/flash-image-only/turn2_request.json > base7/flash-image-only/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' base7/flash-image-only/turn2.json
{"revision_prompt_tokens":22,"content":null,"images":[2613222],"error":null}
Vertex flash, streamed first turn
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/flash-stream/turn1_request.json > base7/flash-stream/turn1.sse
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' base7/flash-stream/turn1.json
{"content":"I will create a cute cartoon style illustration of a happy, ","images":[{"url_chars":2800038,"own_signature_chars":0}],"message_signature_chars":[172],"error":null}
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/flash-stream/turn2_request.json > base7/flash-stream/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' base7/flash-stream/turn2.json
{"revision_prompt_tokens":55,"content":null,"images":[2639446],"error":null}
Vertex flash, three turns
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/flash-three-turns/turn1_request.json > base7/flash-three-turns/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' base7/flash-three-turns/turn1.json
{"content":"I will draw a charming, orange tabby cartoon cat happily cur","images":[{"url_chars":2537530,"own_signature_chars":0}],"message_signature_chars":[172,1728232],"error":null}
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/flash-three-turns/turn2_request.json > base7/flash-three-turns/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' base7/flash-three-turns/turn2.json
{"revision_prompt_tokens":55,"content":null,"images":[2365022],"error":null}
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/flash-three-turns/turn3_request.json > base7/flash-three-turns/turn3.json
$ jq -c '{third_turn_prompt_tokens: .usage.prompt_tokens, images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' base7/flash-three-turns/turn3.json
{"third_turn_prompt_tokens":null,"images":[],"error":"litellm.BadRequestError: Vertex_aiException BadRequestError - {\n  \"error\": {\n    \"code\": 400,\n    \"message\": \"Image part is missing a thought_signature in content position 2, part position 1. Please refer to https://aistudio.google.com/docs/thought-signatures?generate-content=true#model-behavior for more details.\",\n    \"status\": \"INVALID_ARGUMENT\"\n  }\n}\n\n\nLiteLLM: model group 'gemini-3.1-flash-image' failed with the error above. No fallback was attempted."}
OpenAI Python SDK
$ sdkvenv/bin/python sdk_client.py 22045
first turn images: 1
revision prompt tokens: 56
revision images: 1
Vertex Nano Banana, run 1
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/nano-draft-final/turn1_request.json > base7/nano-draft-final/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' base7/nano-draft-final/turn1.json
{"content":null,"images":[{"url_chars":2668814,"own_signature_chars":0}],"message_signature_chars":[3165528],"error":null}
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/nano-draft-final/turn2_request.json > base7/nano-draft-final/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' base7/nano-draft-final/turn2.json
{"revision_prompt_tokens":130,"content":null,"images":[1981094],"error":null}
Vertex Nano Banana, run 2
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7-nano2/nano-draft-final/turn1_request.json > base7-nano2/nano-draft-final/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' base7-nano2/nano-draft-final/turn1.json
{"content":null,"images":[{"url_chars":2280830,"own_signature_chars":0}],"message_signature_chars":[2694496],"error":null}
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7-nano2/nano-draft-final/turn2_request.json > base7-nano2/nano-draft-final/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' base7-nano2/nano-draft-final/turn2.json
{"revision_prompt_tokens":91,"content":null,"images":[2547522],"error":null}
Vertex Nano Banana, run 3
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7-nano3/nano-draft-final/turn1_request.json > base7-nano3/nano-draft-final/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' base7-nano3/nano-draft-final/turn1.json
{"content":null,"images":[{"url_chars":2484606,"own_signature_chars":0}],"message_signature_chars":[2909496],"error":null}
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7-nano3/nano-draft-final/turn2_request.json > base7-nano3/nano-draft-final/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' base7-nano3/nano-draft-final/turn2.json
{"revision_prompt_tokens":119,"content":" Here is the image with the blue chair, while keeping everyt","images":[2324666],"error":null}
AI Studio flash, text + image
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/studio-text-image/turn1_request.json > base7/studio-text-image/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' base7/studio-text-image/turn1.json
{"content":"I will draw a charming, orange tabby cat comfortably seated ","images":[{"url_chars":1312239,"own_signature_chars":0}],"message_signature_chars":[212,1734800],"error":null}
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/studio-text-image/turn2_request.json > base7/studio-text-image/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' base7/studio-text-image/turn2.json
{"revision_prompt_tokens":325,"content":null,"images":[1264223],"error":null}
Vertex text-only model
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/text-only/turn1_request.json > base7/text-only/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' base7/text-only/turn1.json
{"content":"11","images":[],"message_signature_chars":[704],"error":null}
$ curl -sS http://127.0.0.1:22045/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @base7/text-only/turn2_request.json > base7/text-only/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' base7/text-only/turn2.json
{"revision_prompt_tokens":181,"content":"22","images":[],"error":null}

After (d7574b3)

Vertex flash, text + image
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/flash-text-image/turn1_request.json > head9/flash-text-image/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' head9/flash-text-image/turn1.json
{"content":"I will generate a cheerful cartoon image of a ginger tabby c","images":[{"url_chars":2349898,"own_signature_chars":1565132}],"message_signature_chars":[172,1565132],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/flash-text-image/turn2_request.json > head9/flash-text-image/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9/flash-text-image/turn2.json
{"revision_prompt_tokens":1528,"content":null,"images":[2360942],"error":null}
Vertex flash, image only
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/flash-image-only/turn1_request.json > head9/flash-image-only/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' head9/flash-image-only/turn1.json
{"content":null,"images":[{"url_chars":2561994,"own_signature_chars":1779728}],"message_signature_chars":[1779728],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/flash-image-only/turn2_request.json > head9/flash-image-only/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9/flash-image-only/turn2.json
{"revision_prompt_tokens":1471,"content":null,"images":[2548198],"error":null}
Vertex flash, streamed first turn
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/flash-stream/turn1_request.json > head9/flash-stream/turn1.sse
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' head9/flash-stream/turn1.json
{"content":"I will draw a charming cartoon illustration of a happy, oran","images":[{"url_chars":2451646,"own_signature_chars":1657216}],"message_signature_chars":[172],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/flash-stream/turn2_request.json > head9/flash-stream/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9/flash-stream/turn2.json
{"revision_prompt_tokens":1506,"content":null,"images":[2467086],"error":null}
Vertex flash, three turns
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/flash-three-turns/turn1_request.json > head9/flash-three-turns/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' head9/flash-three-turns/turn1.json
{"content":"I will draw a whimsical cartoon orange tabby cat curled up a","images":[{"url_chars":2770086,"own_signature_chars":1948492}],"message_signature_chars":[172,1948492],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/flash-three-turns/turn2_request.json > head9/flash-three-turns/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9/flash-three-turns/turn2.json
{"revision_prompt_tokens":1513,"content":null,"images":[2724046],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/flash-three-turns/turn3_request.json > head9/flash-three-turns/turn3.json
$ jq -c '{third_turn_prompt_tokens: .usage.prompt_tokens, images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9/flash-three-turns/turn3.json
{"third_turn_prompt_tokens":2904,"images":[2728666],"error":null}
OpenAI Python SDK
$ sdkvenv/bin/python sdk_client.py 30962
first turn images: 1
revision prompt tokens: 1544
revision images: 1
Vertex Nano Banana, run 1
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/nano-draft-final/turn1_request.json > head9/nano-draft-final/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' head9/nano-draft-final/turn1.json
{"content":null,"images":[{"url_chars":2648906,"own_signature_chars":3145552}],"message_signature_chars":[3145552],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/nano-draft-final/turn2_request.json > head9/nano-draft-final/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9/nano-draft-final/turn2.json
{"revision_prompt_tokens":2759,"content":"Here is the updated illustration with the chair changed to a","images":[2643886],"error":null}
Vertex Nano Banana, run 2
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9-nano2/nano-draft-final/turn1_request.json > head9-nano2/nano-draft-final/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' head9-nano2/nano-draft-final/turn1.json
{"content":null,"images":[{"url_chars":2241310,"own_signature_chars":2595144}],"message_signature_chars":[2595144],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9-nano2/nano-draft-final/turn2_request.json > head9-nano2/nano-draft-final/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9-nano2/nano-draft-final/turn2.json
{"revision_prompt_tokens":2438,"content":"Here is the updated image with the armchair changed to blue ","images":[2230818],"error":null}
Vertex Nano Banana, run 3
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9-nano3/nano-draft-final/turn1_request.json > head9-nano3/nano-draft-final/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' head9-nano3/nano-draft-final/turn1.json
{"content":"Here is the cartoon cat on a chair you asked for! Let me kno","images":[{"url_chars":2241814,"own_signature_chars":1032028}],"message_signature_chars":[1459500,1032028],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9-nano3/nano-draft-final/turn2_request.json > head9-nano3/nano-draft-final/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9-nano3/nano-draft-final/turn2.json
{"revision_prompt_tokens":2556,"content":"I have updated the image by changing the armchair to blue wh","images":[2256842],"error":null}
AI Studio flash, text + image
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/studio-text-image/turn1_request.json > head9/studio-text-image/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' head9/studio-text-image/turn1.json
{"content":"A full-color, playful cartoon illustration of a cheerful ora","images":[{"url_chars":1276839,"own_signature_chars":1706992}],"message_signature_chars":[212,1706992],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/studio-text-image/turn2_request.json > head9/studio-text-image/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9/studio-text-image/turn2.json
{"revision_prompt_tokens":318,"content":"I have updated the image to make the chair blue while keepin","images":[1288647],"error":null}
Vertex text-only model
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/text-only/turn1_request.json > head9/text-only/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' head9/text-only/turn1.json
{"content":"11","images":[],"message_signature_chars":[632],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/text-only/turn2_request.json > head9/text-only/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9/text-only/turn2.json
{"revision_prompt_tokens":158,"content":"22","images":[],"error":null}
History saved before this fix
$ curl -sS http://127.0.0.1:25954/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/legacy-history/turn1_request.json > head9/legacy-history/turn1.json
$ jq -c '{content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | {url_chars: (.image_url.url | length), own_signature_chars: (.provider_specific_fields.thought_signature // "" | length)}], message_signature_chars: [.choices[0].message.provider_specific_fields.thought_signatures[]? | length], error: .error.message}' head9/legacy-history/turn1.json
{"content":"I will generate a cheerful image of a friendly cartoon orang","images":[{"url_chars":2354382,"own_signature_chars":0}],"message_signature_chars":[172,1602744],"error":null}
$ curl -sS http://127.0.0.1:30962/v1/chat/completions -H 'Authorization: Bearer <master key>' -H 'Content-Type: application/json' -d @head9/legacy-history/turn2_request.json > head9/legacy-history/turn2.json
$ jq -c '{revision_prompt_tokens: .usage.prompt_tokens, content: (.choices[0].message.content // null | if . == null then null else .[0:60] end), images: [.choices[0].message.images[]? | .image_url.url | length], error: .error.message}' head9/legacy-history/turn2.json
{"revision_prompt_tokens":1492,"content":null,"images":[2366550],"error":null}

Direct to Google

Vertex flash text + image, Vertex flash image only, Vertex Nano Banana, AI Studio flash text + image
$ python direct.py vertex gemini-3.1-flash-image text direct2
{"backend": "vertex", "model": "gemini-3.1-flash-image", "first_turn_parts": ["text+sig", "image+sig"], "revision_prompt_tokens": 1469, "error": null}
$ python direct.py vertex gemini-3.1-flash-image image direct2
{"backend": "vertex", "model": "gemini-3.1-flash-image", "first_turn_parts": ["image+sig"], "revision_prompt_tokens": 1523, "error": null}
$ python direct.py vertex gemini-nano-banana-2.1 image direct2
{"backend": "vertex", "model": "gemini-nano-banana-2.1", "first_turn_parts": ["thought-text", "thought-image", "thought-text", "image+sig"], "revision_prompt_tokens": 2373, "error": null}
$ python direct.py studio gemini-3.1-flash-image text direct2
{"backend": "studio", "model": "gemini-3.1-flash-image", "first_turn_parts": ["text+sig", "image+sig"], "revision_prompt_tokens": 315, "error": null}

Surprises

  • Unfixed Nano Banana still kept the picture in 3 of 9 runs across main and LTS
  • AI Studio reads unsigned images, so it never showed this bug

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • A Vertex image-only reply sent to AI Studio now makes two upstream calls
    • AI Studio rejects the Vertex signature with a 400. litellm then resends with the placeholder signature and answers 200. Base made one call
    • Only model groups mixing vertex_ai/ and gemini/ deployments pay the extra upload and round trip (design decision 10)
  • Every image reply now carries its multi-MB signature twice
    • A text plus image reply is 6.05 MB, and 4.32 MB without the per-image copy. Clients, logging callbacks and response caches get the extra 1.7 MB, and clients send it back on later turns
    • OpenAI, Azure, OpenRouter and hosted vLLM routes get the same signature twice when a chat switches to them
    • Kept on purpose, see design decision 2
  • Image models missing from the cost map still lose the image signature
    • A new model on a pinned or local cost map keeps the old bug, including the third-turn 400
    • Setting model_info: {mode: image_generation} on the deployment turns signing on

Low

  • A bad thought_signatures entry now answers 400 on text-only Gemini chats too, where base sent the valid signature. drop_params drops it instead
  • Vertex batch lines whose body.model is 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-up

Live 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 without drop_params), Vertex to AI Studio and back, the response cache, /v1/responses, /v1/messages, text streaming, key create and delete, and spend logs

At 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

Case Base Head
Saved reply, empty content 1499 1499
List content, own signature 53 1490
Text + image replay 53 1490
Saved list text, text signature 53 53
Vertex reply to AI Studio 320 315
AI Studio reply to Vertex 61 47
Object in thought_signatures 53 400
Same, with drop_params 53 1490
Number as image URL 500 400
Image plus tools 37 37
Vertex image only to AI Studio 283 283

For 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

Case Base Head
Vertex image reply, then text model 1138 1138
Same, history saved before this fix 1138 1138
AI Studio image reply, then text model 1134 1144
Empty text, signed 17 17
Text + image replay 53 1490
List content, own signature 53 1490
Saved reply, empty content 1499 1499
Vertex image only to AI Studio 283 283
Same, streamed 283 283
Vertex reply to AI Studio 315 317
Object in thought_signatures 53 400
Same, with drop_params 53 1490
Image, tool call, tool result, edit no tool call 1548

With 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

Case Base Head
Fireworks replay 200 200
Mistral replay 422 422
String image_url 53 1490
Number as image_url 53 400
Same, with drop_params 53 53
Number as an image entry 53 400
Same, with drop_params 53 53
Number as an image's signature 53 400
Same, with drop_params 53 1490
mode: chat deployment 53 1490
Text + image replay 53 1490
Vertex image reply, then text model 1138 1138
Empty text, signed 17 17
Object in thought_signatures 53 400
Same, with drop_params 53 1490
litellm SDK, not streamed 53 1490
litellm SDK, streamed 53 1490

Fireworks 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_forbidden on images on both sides, a pre-existing bug filed separately. The mode: chat deployment registers gemini-3.1-flash-image with model_info: {mode: chat}, and head still signs the image

litellm SDK leg

Base

$ GOOGLE_APPLICATION_CREDENTIALS=$(dotenv-get GOOGLE_APPLICATION_CREDENTIALS) VERTEXAI_PROJECT=$(dotenv-get VERTEXAI_PROJECT) LITELLM_LOCAL_MODEL_COST_MAP=True PYTHONPATH=base base/.venv/bin/python sdk/sdk_leg.py ../qa/head/flash-text-image/turn1.json 'Make the chair blue. Keep everything else the same.' 2>&1 | grep -E '^(litellm loaded|non-streamed|streamed)'
litellm loaded: base/litellm/__init__.py
non-streamed prompt tokens: 53 images: 1
streamed prompt tokens: 53 images: 1

Head

$ GOOGLE_APPLICATION_CREDENTIALS=$(dotenv-get GOOGLE_APPLICATION_CREDENTIALS) VERTEXAI_PROJECT=$(dotenv-get VERTEXAI_PROJECT) LITELLM_LOCAL_MODEL_COST_MAP=True PYTHONPATH=head head/.venv/bin/python sdk/sdk_leg.py ../qa/head/flash-text-image/turn1.json 'Make the chair blue. Keep everything else the same.' 2>&1 | grep -E '^(litellm loaded|non-streamed|streamed)'
litellm loaded: head/litellm/__init__.py
non-streamed prompt tokens: 1490 images: 1
streamed prompt tokens: 1490 images: 1

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_params is set, on any assistant message with or without images, per litellm's drop_params rule (design decision 5). An object inside provider_specific_fields.thought_signatures answered 200 on base, which ignored it, and now answers 400 "Invalid assistant message: provider_specific_fields.thought_signatures entries must be strings". With drop_params: true the entry is dropped and the edit keeps the picture (1490 prompt tokens, base 53). A number as images[].image_url.url answered 500 "argument of type 'int' is not iterable" on base and now answers 400. An image entry or image_url that 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 unless drop_params is set

Each image in a response gains provider_specific_fields.thought_signature, and the same signature stays in the message's thought_signatures list, 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 reviewer

Image provider_specific_fields validates 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 through

Regression 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_params it 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 run

Dependency graph

Dependent Status
Chat history to Gemini, Vertex and AI Studio verified live
Saved histories without per-image signatures verified live
Odd per-image signatures verified live
Malformed history, 400 and drop_params verified live
Vertex to AI Studio replay and back verified live
Vertex image only to AI Studio verified live, one resend
Image model reply sent to a text model verified live
Image replay next to a tool call verified live
Strict providers after a model switch verified live
Deployment registered as mode: chat verified live
litellm Python SDK verified live
Vertex context caching unreachable
Response cache hit rebuild verified live
/v1/responses image output verified live, unchanged
/v1/messages adapter verified live, unchanged
Text streaming, keys, spend logs verified live, unchanged
Stream chunk reassembly tested
OpenAI-compatible upstream image fields tested
Vertex batch, OpenCode provider untested
Logging redaction traced
Dashboard, Python client, enterprise no consumers

Not 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" for gemini-3.1-flash-image (seen in the earlier run at cad8b63)

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR
Bugbot summary

[!NOTE]
Medium Risk
Changes Gemini chat-history translation and validation for assistant images and thought signatures; affects multi-turn image edits and can 400 on malformed history unless drop_params is set.

Overview
Fixes multi-turn Gemini image editing by preserving and replaying thought signatures on assistant-generated images instead of dropping them when chat history is sent back to Vertex/AI Studio.

Outbound (Gemini → OpenAI shape): Inline image parts now surface provider_specific_fields.thought_signature on each message.images[] entry (message-level thought_signatures is unchanged).

Inbound (history → Gemini): Assistant turns are built through new signing logic that attaches thoughtSignature to the text part and to each image part. Per-image signatures win; older histories without per-image data fall back by allocating unused message signatures (right-aligned to final images). Signatures already on tool calls are not duplicated on text. Image signatures are only attached when the target model is an image generator (cost map / deployment mode), so text-only models are not sent invalid image signatures.

Compatibility: strip_litellm_response_only_fields / internal field stripping also remove image provider_specific_fields for strict providers. Malformed assistant image or signature fields raise 400 unless drop_params drops the bad parts.

Reviewed by Cursor Bugbot for commit 670cad9. Bugbot is set up for automated code reviews on this repo. Configure here.

…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
@veria-ai

veria-ai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps

greptile-apps Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with the previous numbered finding addressed and no new actionable issues found.

Summary

The PR keeps each generated Gemini image's signature with that image and replays it on the matching part.

  • Gemini image replies keep each image’s signature for later edits.
  • Strict-provider adapters remove provider-only fields from assistant images.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    A[Gemini image reply] --> B[Image carries its own signature]
    B --> C[Client resends assistant message]
    C --> D[Check history fields]
    D --> E[Replay each image with its signature]
    D --> F[Return malformed-history value]
    F --> G[One boundary raises BadRequestError]
Loading

Reviews (5) · Last reviewed commit: "refactor(gemini): return malformed assis..." · Reviewed by Greptile

…history_thought_signature

# Conflicts:
#	litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

…_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.
…history_thought_signature

# Conflicts:
#	litellm/llms/vertex_ai/files/transformation.py
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ 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.

@mateo-berri mateo-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@mateo-berri
mateo-berri merged commit 2ca4eba into BerriAI:main Nov 22, 2026
55 checks passed
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.

Gemini assistant image history drops thought signatures during Chat Completions replay

2 participants