diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index 1339703ab1f..3b2b129e309 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -1266,9 +1266,9 @@ def _is_valid_thought_signature(signature: str) -> bool: try: padding = "=" * (-len(signature) % 4) base64.b64decode(signature + padding, validate=True) - return True except (binascii.Error, ValueError): return False + return True def get_thought_signature_from_tool(tool: Mapping[str, object]) -> str | None: diff --git a/tests/unit/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py b/tests/unit/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py index 1cdfa13073b..f7ebf89bc1d 100644 --- a/tests/unit/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py +++ b/tests/unit/llms/vertex_ai/gemini/test_thought_signature_in_tool_call_id.py @@ -322,6 +322,18 @@ def test_is_valid_thought_signature_tolerates_missing_padding(): assert _is_valid_thought_signature(encoded) is True +def test_is_valid_thought_signature_scope_is_syntactic(): + """The extractor validates the wire encoding, not semantic integrity: a + client normalizer that keeps standard base64 characters produces a value + that still decodes even though Vertex will reject it as an unauthenticated + signature. Callers that need semantic verification have to compare against + the provider-issued signature; the ID-embedded fallback can only catch + encoding damage. Documented so future readers understand issue #37849's + guarantee is bounded.""" + decodable_but_wrong = base64.b64encode(b"not-the-real-signature").decode("ascii") + assert _is_valid_thought_signature(decodable_but_wrong) is True + + def test_get_thought_signature_drops_client_mangled_id_suffix(): """When the segment after ``__thought__`` isn't valid base64, the extractor must return ``None`` so the caller can either fall back to the dummy