Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

test(vertex_ai): cover id-embedded, tool-level, and end-to-end parallel signature replay

ljogeigerd5af42717e97
diff --git a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py
index 28c551ab824..562f27c11cf 100644
--- a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py
+++ b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py
@@ -806,6 +806,27 @@ def _parallel_tool_calls(*signatures):
]
+def _parallel_tool_calls_signed_via_id(*signatures):
+ """Parallel tool calls in the shape LiteLLM actually hands back to clients.
+
+ The signature rides in the tool call id behind __thought__, which is what an
+ OpenAI-format client echoes back on the next turn.
+ """
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ _encode_tool_call_id_with_signature,
+ )
+
+ return [
+ {
+ "id": _encode_tool_call_id_with_signature(f"call_{idx}", signature),
+ "type": "function",
+ "function": {"name": f"tool_{idx}", "arguments": '{"location": "Paris"}'},
+ "index": idx,
+ }
+ for idx, signature in enumerate(signatures)
+ ]
+
+
REAL_THOUGHT_SIGNATURE = "Co4CAdHtim/rWgXbz2Ghp4tShzLeMASrPw6JJyYIC3cbVyZnKzU3uv8/wVzyS2sKRPL2m8QQHHXbNQhEEz500G7n"
@@ -899,6 +920,166 @@ def test_no_signatures_on_parallel_tool_calls_for_gemini_2_5():
assert all("thoughtSignature" not in part for part in gemini_parts)
+def test_signature_embedded_in_tool_call_id_only_on_first_parallel_call():
+ """The production shape: the signature arrives inside the first call's id, siblings have bare ids."""
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ convert_to_gemini_tool_call_invoke,
+ )
+
+ gemini_parts = convert_to_gemini_tool_call_invoke(
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": _parallel_tool_calls_signed_via_id(
+ REAL_THOUGHT_SIGNATURE, None, None
+ ),
+ },
+ model="gemini-3-pro-preview",
+ )
+
+ assert len(gemini_parts) == 3
+ assert gemini_parts[0]["thoughtSignature"] == REAL_THOUGHT_SIGNATURE
+ assert "thoughtSignature" not in gemini_parts[1]
+ assert "thoughtSignature" not in gemini_parts[2]
+
+
+def test_tool_level_provider_specific_fields_signature_leaves_siblings_empty():
+ """A signature on the tool call itself, rather than on its function, behaves the same way."""
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ convert_to_gemini_tool_call_invoke,
+ )
+
+ tool_calls = _parallel_tool_calls(None, None)
+ tool_calls[0]["provider_specific_fields"] = {
+ "thought_signature": REAL_THOUGHT_SIGNATURE
+ }
+
+ gemini_parts = convert_to_gemini_tool_call_invoke(
+ {"role": "assistant", "content": None, "tool_calls": tool_calls},
+ model="gemini-3-pro-preview",
+ )
+
+ assert len(gemini_parts) == 2
+ assert gemini_parts[0]["thoughtSignature"] == REAL_THOUGHT_SIGNATURE
+ assert "thoughtSignature" not in gemini_parts[1]
+
+
+def test_placeholder_lands_on_first_emitted_part_not_first_tool_call_entry():
+ """A non-function entry (e.g. an OpenAI custom tool call) emits no part, so it must not
+ consume the one placeholder slot and leave the real first function call bare."""
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ _get_dummy_thought_signature,
+ convert_to_gemini_tool_call_invoke,
+ )
+
+ tool_calls = [
+ {"id": "call_custom", "type": "custom", "custom": {"name": "noop", "input": ""}}
+ ] + _parallel_tool_calls(None, None)
+
+ gemini_parts = convert_to_gemini_tool_call_invoke(
+ {"role": "assistant", "content": None, "tool_calls": tool_calls},
+ model="gemini-3-pro-preview",
+ )
+
+ assert len(gemini_parts) == 2
+ assert gemini_parts[0]["thoughtSignature"] == _get_dummy_thought_signature()
+ assert "thoughtSignature" not in gemini_parts[1]
+
+
+def test_no_placeholder_when_model_is_unknown():
+ """Without a model there is nothing to prove the target needs a placeholder, so none is added."""
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ convert_to_gemini_tool_call_invoke,
+ )
+
+ gemini_parts = convert_to_gemini_tool_call_invoke(
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": _parallel_tool_calls(None, None),
+ },
+ )
+
+ assert len(gemini_parts) == 2
+ assert all("thoughtSignature" not in part for part in gemini_parts)
+
+
+def test_real_signature_forwarded_to_gemini_2_5_without_placeholder_siblings():
+ """Older models still receive a real signature that a client replays, and still get no placeholder."""
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ convert_to_gemini_tool_call_invoke,
+ )
+
+ gemini_parts = convert_to_gemini_tool_call_invoke(
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": _parallel_tool_calls(REAL_THOUGHT_SIGNATURE, None),
+ },
+ model="gemini-2.5-flash",
+ )
+
+ assert len(gemini_parts) == 2
+ assert gemini_parts[0]["thoughtSignature"] == REAL_THOUGHT_SIGNATURE
+ assert "thoughtSignature" not in gemini_parts[1]
+
+
+def test_parallel_tool_call_history_replayed_through_full_message_conversion():
+ """End to end through the message-history converter, the path a real /chat/completions replay takes."""
+ from litellm.llms.vertex_ai.gemini.transformation import (
+ _gemini_convert_messages_with_history,
+ )
+
+ messages = [
+ {"role": "user", "content": "Weather in Paris, London and Tokyo?"},
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": _parallel_tool_calls_signed_via_id(
+ REAL_THOUGHT_SIGNATURE, None, None
+ ),
+ },
+ ]
+
+ contents = _gemini_convert_messages_with_history(
+ messages=messages, model="gemini-3-pro-preview"
+ )
+
+ model_parts = contents[1]["parts"]
+ assert len(model_parts) == 3
+ assert model_parts[0]["thoughtSignature"] == REAL_THOUGHT_SIGNATURE
+ assert "thoughtSignature" not in model_parts[1]
+ assert "thoughtSignature" not in model_parts[2]
+
+
+def test_signed_text_part_survives_alongside_unsigned_parallel_tool_calls():
+ """gemini-2.5 history with a signed text part and parallel unsigned calls: the text keeps its real
+ signature, only the first call gets the placeholder, and the siblings stay bare."""
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ _get_dummy_thought_signature,
+ )
+ from litellm.llms.vertex_ai.gemini.transformation import (
+ _gemini_convert_messages_with_history,
+ )
+
+ msg = {
+ "role": "assistant",
+ "content": "Checking all three cities.",
+ "provider_specific_fields": {"thought_signatures": ["real_25_signature"]},
+ "tool_calls": _parallel_tool_calls(None, None, None),
+ }
+
+ parts = _gemini_convert_messages_with_history(
+ messages=[msg], model="gemini-3-pro-preview"
+ )[0]["parts"]
+
+ assert parts[0]["text"] == "Checking all three cities."
+ assert parts[0]["thoughtSignature"] == "real_25_signature"
+ assert parts[1]["thoughtSignature"] == _get_dummy_thought_signature()
+ assert "thoughtSignature" not in parts[2]
+ assert "thoughtSignature" not in parts[3]
+
+
# Tests for media_resolution (detail parameter) handling - Issue #17084
class TestMediaResolution:
"""Tests for media_resolution handling in Gemini 2.x models"""