Repository navigation
fix(router): strip foreign Gemini thought signatures on vertex_ai/gemini fallback - #33471
mathew-felix wants to merge 5 commits into
Conversation
Greptile SummaryThis PR fixes a "Corrupted thought signature" 400 error that occurs when the Router falls back from a Vertex AI Gemini deployment to a Google AI Studio deployment (or vice versa) mid-conversation. Thought signatures are endpoint-bound tokens and must be stripped from replayed tool calls before the fallback call is issued.
Confidence Score: 4/5Safe to merge with one fix: the boundary detector incorrectly fires for vertex_ai ↔ vertex_ai_beta transitions, stripping valid Vertex AI signatures unnecessarily. The core logic in Files Needing Attention: litellm/router_utils/fallback_event_handlers.py — specifically
|
| Filename | Overview |
|---|---|
| litellm/router_utils/fallback_event_handlers.py | Adds boundary detection and signature-stripping wiring to run_async_fallback; _crosses_gemini_endpoint_boundary returns a false positive for vertex_ai ↔ vertex_ai_beta fallbacks, causing valid Vertex AI signatures to be stripped unnecessarily. |
| litellm/utils.py | Adds _strip_foreign_thought_signature_from_tool_call and _remove_foreign_thought_signatures_from_messages; correctly handles both dict and Pydantic tool-call objects, strips provider_specific_fields and id suffix, and strips the matching tool_call_id on role=tool messages. |
| tests/test_litellm/router_utils/test_fallback_event_handlers.py | Adds well-structured unit tests covering provider-lookup, boundary detection, signature stripping on fallback, same-provider preservation, and single-lookup-per-original-model-group; all mocked, no real network calls. |
| tests/test_litellm/test_utils.py | Adds TestRemoveForeignThoughtSignaturesFromMessages covering dict and Pydantic messages, role=tool pairing, immutability, and passthrough cases; no real network calls. |
Reviews (5): Last reviewed commit: "test: cover remaining branches flagged b..." | Re-trigger Greptile
Greptile SummaryThis PR fixes a 400 "Corrupted thought signature" error that occurs when the Router falls back between a Vertex AI Gemini deployment and a Google AI Studio Gemini deployment (or vice versa). Thought signatures are endpoint-bound tokens that the receiving endpoint rejects if they were minted by a different endpoint.
Confidence Score: 4/5Safe to merge; the fix is well-targeted, the happy paths are tested, and it is purely additive with no existing call sites changed. The core logic correctly strips thought signatures from both channels (provider_specific_fields and id suffix) on assistant messages before crossing the endpoint boundary, and factory.py already handles the corresponding tool-response messages downstream. The main nits are that
|
| Filename | Overview |
|---|---|
| litellm/router_utils/fallback_event_handlers.py | Adds _get_model_group_custom_llm_provider and _crosses_gemini_endpoint_boundary helpers, and wires them into run_async_fallback to strip foreign Gemini thought signatures before making the fallback call; original_provider is recomputed on every loop iteration when it could be computed once before the loop. |
| litellm/utils.py | Adds _strip_foreign_thought_signature_from_tool_call and _remove_foreign_thought_signatures_from_messages to remove endpoint-bound signatures from assistant tool-call messages; the new function intentionally skips role=tool messages (handled downstream by factory.py) but this gap is undocumented. |
| tests/test_litellm/router_utils/test_fallback_event_handlers.py | Adds unit tests for the new helpers and two async integration tests verifying signature stripping across the Gemini endpoint boundary and preservation within the same provider; tests are properly mocked with no real network calls. |
| tests/test_litellm/test_utils.py | Adds four unit tests for _remove_foreign_thought_signatures_from_messages covering signature removal, field preservation, non-mutation, and no-op on non-tool-call messages. |
Reviews (2): Last reviewed commit: "fix(router): strip foreign Gemini though..." | Re-trigger Greptile
|
Pushed a follow-up commit addressing the review feedback:
Added regression tests for all three. Full suite still passes (258 tests). |
…ini fallback Thought signatures returned by Gemini tool calls are endpoint-bound: a signature minted by Vertex AI is rejected as corrupted by Google AI Studio, and vice versa. When the Router fell back between a Vertex AI Gemini deployment and its Google AI Studio twin mid-conversation, the foreign signature was replayed unchanged in both the tool call's provider_specific_fields and its ID suffix, causing the fallback call itself to fail with a 400 error. run_async_fallback now detects when a fallback crosses the vertex_ai <-> gemini boundary and strips the foreign signature from replayed tool calls before the fallback request is sent, so the receiving endpoint treats it as missing and fills in its own dummy placeholder, the same way a genuinely missing signature is already handled. Fixes BerriAI#32140
…pping - Strip the matching tool_call_id on role=tool messages too, not just the assistant's tool_calls[].id, so the two stay paired after a vertex_ai/gemini boundary crossing strips the embedded signature. - Add vertex_ai_beta to the recognized Gemini-family providers, since it is a distinct custom_llm_provider from vertex_ai in this codebase. - Hoist the original model group's provider lookup out of the fallback loop; it does not change across fallback candidates.
523a2b2 to
bda6e9a
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance1 warning
Comparing Footnotes |
Adds regression tests for the 9 lines Codecov flagged as uncovered in the diff: the two early-return branches in _get_model_group_custom_llm_provider (no model to infer from, and get_llm_provider raising for an unrecognized model string), and the Pydantic-object and pass-through branches in _remove_foreign_thought_signatures_from_messages for both messages and tool calls.
|
Pushed a follow-up commit adding tests for the 9 lines Codecov flagged as uncovered: two early-return branches in _get_model_group_custom_llm_provider (no model string to infer a provider from, and get_llm_provider raising for an unrecognized model), plus the Pydantic-object and pass-through branches in _remove_foreign_thought_signatures_from_messages for both messages and tool calls. Patch coverage should be clean now. |
…ve test_utils.py conflict
| return exception.status_code == 400 and bool(getattr(exception, "llm_provider", "")) | ||
|
|
||
|
|
||
| _GEMINI_ENDPOINT_PROVIDERS = ("vertex_ai", "vertex_ai_beta", "gemini") |
There was a problem hiding this comment.
do we need a hardcoded list here? this seems like it could go out of date fast
|
Closing as superseded by #49697, which resends with the placeholder thought signature after a cross-backend fallback. Thanks for the groundwork here |
Relevant issues
Closes #32140
Linear ticket
Pre-Submission checklist
@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewDelays 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
I don't have Vertex AI and Google AI Studio credentials configured against the same Gemini model to reproduce this against live endpoints, so I verified the fix at the unit level instead. Running the reproduction snippet from the issue through
_get_model_group_custom_llm_provider,_crosses_gemini_endpoint_boundary, and_remove_foreign_thought_signatures_from_messagesdirectly shows the fallback path now substitutes the documented dummy value (skip_thought_signature_validator, base64 encoded) instead of forwarding the foreign signature. Full test output belowType
🐛 Bug Fix
✅ Test
Changes
Gemini's tool calls carry a thought signature, an opaque token the model uses to preserve reasoning continuity across turns. That signature is only valid on the endpoint that minted it, so a signature returned by Vertex AI is rejected as corrupted if replayed to Google AI Studio, and the reverse direction fails the same way. This PR makes the Router strip that signature out of replayed tool calls whenever a fallback crosses the vertex_ai <-> gemini boundary, so the receiving endpoint treats it as missing and fills in its own dummy placeholder instead of rejecting the request
#32140 reports that a Router configured with a Vertex AI Gemini deployment and a same-model Google AI Studio fallback breaks on every mid-conversation fallback attempt. Turn one succeeds and returns a signature tied to whichever endpoint served it. When the primary fails on turn two and the Router falls back to the twin deployment, the signature from turn one gets replayed unchanged, in both the tool call's provider_specific_fields and its id suffix, and the new endpoint responds with a 400 "Corrupted thought signature" error. The existing dummy-signature fallback in
_get_thought_signature_from_toolonly fires when a signature is missing, and the id-stripping added in #18374 only fires when the target is not a Gemini model, so neither existing safety net catches a Gemini-to-Gemini boundary crossingAdded
_get_model_group_custom_llm_providerand_crosses_gemini_endpoint_boundarytolitellm/router_utils/fallback_event_handlers.py, wired intorun_async_fallbackso it strips foreign thought signatures from the replayed messages before making the fallback callAdded
_remove_foreign_thought_signature_from_tool_calland_remove_foreign_thought_signatures_from_messagestolitellm/utils.py, next to the existing sibling function#18374added for the non-Gemini caseThis PR covers the channel where the signature lives in a tool call's provider_specific_fields and id suffix. Gemini can also attach a signature to thinking_blocks on a plain assistant text message with no tool call involved, and that channel is not touched here. I scoped this PR to the tool call channel since it already has test coverage to build on, and I'm glad to open a follow-up for the thinking_blocks channel if that's wanted