Repository navigation
fix(vertex_ai): reject client-mangled thought_signature embedded in tool_call_id - #38333
libaojiang wants to merge 7 commits into
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Greptile SummaryThis PR validates thought signatures extracted from Vertex/Gemini tool-call IDs and falls back to the existing dummy-or-drop behavior when strict base64 decoding fails.
Confidence Score: 4/5The PR should not merge until decodable but client-mangled signatures are prevented from bypassing the existing fallback. The validator distinguishes malformed base64 from valid base64 but cannot establish signature integrity, so a normalized suffix that remains decodable is still forwarded and rejected by Vertex. Files Needing Attention: litellm/litellm_core_utils/prompt_templates/factory.py; tests/test_litellm/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/prompt_templates/factory.py | Adds syntactic base64 validation, but decodable client corruption can still bypass the fallback and reach Vertex. |
| tests/test_litellm/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py | Adds targeted fallback tests, but all corrupted fixtures contain _ and therefore omit the decodable-but-semantically-invalid case. |
Reviews (1): Last reviewed commit: "fix(vertex_ai): reject client-mangled th..." | Re-trigger Greptile
Merging this PR will not alter performance
|
16d8cff to
a60da7e
Compare
a60da7e to
91789da
Compare
…ool_call_id A Gemini `thoughtSignature` is standard-alphabet base64 bytes, but LiteLLM appends it to `tool_calls[].id` after a `__thought__` marker. Any client that normalizes ids (Claude Code, OpenClaw, editor integrations that shorten + hash the id) mangles the base64 and replays it verbatim on the next turn. `_get_thought_signature_from_tool` used to extract that mangled tail as-is, so `convert_to_gemini_tool_call_invoke` forwarded it to Vertex, which rejects the request with `Base64 decoding failed for "..."` or `Invalid thought signature`. Once a corrupted value entered the history, every following turn failed the same way and the session was dead. Validate the extracted tail against Vertex's strict base64 decoder (padding tolerant) and return None when it fails. Callers already handle the "no signature" case correctly: on Gemini 3+ the first function call falls back to the documented skip-validator dummy signature; on Gemini 2.x the signature is simply omitted. Either way the hard 400 becomes a graceful degradation, and clients that keep the signature in `provider_specific_fields` (LiteLLM SDK) are unaffected because that path is checked first. Fixes BerriAI#37849
The strict-rule gate hard-failed PR BerriAI#38333 because the new `_is_valid_thought_signature` returned inside the ``try`` body (TRY300). Move the success return into a trailing statement so ruff sees the canonical shape and the budget stays base-neutral. Also add a regression test that pins what the ID-embedded fallback actually guarantees: it rejects wire-encoding damage, not semantic integrity. Documents Greptile's observation on the PR that a client normalizer preserving standard base64 characters can still produce a value Vertex will reject as an unauthenticated signature; callers that need semantic verification must compare against the provider-issued signature. Co-authored-by: Cursor <cursoragent@cursor.com>
91789da to
8a5d679
Compare
Rework onto main renamed the helpers to public names and upstream added factory tests pinning the pre-checksum wire format, so update those expectations to the checksummed layout and use a valid base64 signature for the embedded-id round trip.
The type discipline gate now counts LIT010 against the merge-base ceiling and the rebase carried one un-annotated assignment over.
basedpyright counts the str isinstance as reportUnnecessaryIsInstance now that the id carries a str cast, and the type gate rejects any growth over the merge-base count.
|
@greptileai Please review the current head again. Both previous findings are resolved, including checksum validation and removal of redundant comments. |
TLDR
Problem this solves:
__thought__suffix intool_call_id400How it solves it:
Nonewhen either failsUser Flow
Before: an operator using a Telegram assistant built on an OpenAI-compatible client (Claude Code, OpenClaw, some editor integrations) that normalizes
call_idsees the assistant answer the first question, then every following turn fails and the conversation is deadPOST https://litellm-domain/v1/responseswith"model": "gemini-3.1-pro-preview"and afunctiontool.200comes back with afunction_callitem whosecall_idis 1,223 characters long:call_2156408__thought__AY89a1/Iab3+RVjKCabD+h7qYLWIf2BWZ9CZDSDoMZpQytG...(idandcall_idare identical).call_id. Because it's an identifier, it normalizes it into something id-shaped: e.g.call_2156408__thought__AY89a1/_57b05e78dc.call_idtogether with thefunction_call_output.400:Invalid value at 'contents[1].parts[0].thought_signature' (TYPE_BYTES), Base64 decoding failed for "AY89a1/_57b05e78dc".LLM request failedin the chat. Retrying doesn't help: the broken value is now part of the conversation history, so every following turn fails the same way.After: the same conversation continues normally because the corrupted signature is treated as "no signature" and either falls back to the documented dummy (Gemini 3+) or is simply dropped (Gemini 2.x)
POST https://litellm-domain/v1/responseswith"model": "gemini-3.1-pro-preview".200comes back with the samecall_idshape as before, still carrying the raw signature after__thought__.call_2156408__thought__AY89a1/_57b05e78dc.call_id.200and the model produces the final message.Relevant issues
Fixes #37849
Linear ticket
Pre-Submission checklist
uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CIScreenshots / Proof of Fix
Reproducer (no live credentials needed; the corruption happens entirely inside
_get_thought_signature_from_tool→convert_to_gemini_tool_call_invoke):Before (litellm_internal_staging tip prior to this PR)
Command: run the reproducer above.
Output:
Vertex rejects
AY89a1/_57b05e78dcwithBase64 decoding failed, so every turn that quotes this id 400s.After (f62514f)
Same command.
Output:
The signature is dropped and the Gemini 3+ code path substitutes the documented skip-validator dummy; the request goes through instead of 400-ing.
Targeted regression tests
Command:
Output:
The broader Vertex slice passes 47 tests, and the Anthropic compatibility slice passes 6 tests
Type
🐛 Bug Fix
Caveats
Low
tool_calls[].thought_signatureremains additive follow-up work outside this focused bug fix"signature_for_first_call"as a placeholder signature that isn't valid base64. Real Gemini signatures always are, so the test now usesbase64.b64encode(b"signature_for_first_call")to keep coverage identical under the stricter extraction.Final Attestation