From 6f8211c8ead9591ed2314337a6b7acaa4ff73ade Mon Sep 17 00:00:00 2001 From: Piyush Kumar Date: Sat, 31 Oct 2026 07:47:49 +0000 Subject: [PATCH] fix(gemini): emit canonical camelCase tool parts and fix thought signature fallback - In Google Gemini /v1beta and Proto3 JSON mapping, the canonical field names are lowerCamelCase functionCall and functionResponse. - Emit canonical functionCall in convert_to_gemini_tool_call_invoke. - Emit canonical functionResponse in convert_to_gemini_tool_call_result. - Add functionCall and functionResponse to PartType TypedDict while preserving backwards-compatible input types for snake_case. - Use stateful has_emitted_function_call tracking instead of len(_parts_list) == 0 to ensure dummy thought signatures are attached to the first emitted tool call even when preceding message text is present, while strictly respecting Gemini's parallel function calling rule that only the first part in a parallel batch may carry a thought signature. - Update test assertion in test_thought_signature_in_tool_call_id.py. --- .../litellm_core_utils/prompt_templates/factory.py | 11 ++++++----- litellm/types/llms/vertex_ai.py | 6 ++++-- .../gemini/test_thought_signature_in_tool_call_id.py | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index de896cde4dc..2326ae47392 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -1381,6 +1381,7 @@ def convert_to_gemini_tool_call_invoke( ) needs_dummy_signature: Final = model is not None and VertexGeminiConfig.is_gemini_3_or_newer(model) + has_emitted_function_call: bool = False if tool_calls is not None: for tool in tool_calls: @@ -1390,17 +1391,17 @@ def convert_to_gemini_tool_call_invoke( tool_call_id=(tool.get("id") if forward_function_call_id else None), ) if gemini_function_call is not None: - part_dict: VertexPartType = {"function_call": gemini_function_call} + part_dict: VertexPartType = {"functionCall": gemini_function_call} thought_signature = get_thought_signature_from_tool(dict(tool)) # Gemini signs only the first functionCall part of a parallel batch, so scope the # placeholder fallback to that part instead of fabricating one per sibling call: # https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/thinking/thought-signatures#parallel_function_calling_example - is_first_function_call = len(_parts_list) == 0 - if not thought_signature and is_first_function_call and needs_dummy_signature: + if not thought_signature and not has_emitted_function_call and needs_dummy_signature: thought_signature = _get_dummy_thought_signature() if thought_signature: part_dict["thoughtSignature"] = thought_signature + has_emitted_function_call = True _parts_list.append(part_dict) else: # don't silently drop params. Make it clear to user what's happening. raise Exception( @@ -1409,7 +1410,7 @@ def convert_to_gemini_tool_call_invoke( elif function_call is not None: gemini_function_call = _gemini_tool_call_invoke_helper(function_call_params=function_call) if gemini_function_call is not None: - part_dict_function: Final[VertexPartType] = {"function_call": gemini_function_call} + part_dict_function: Final[VertexPartType] = {"functionCall": gemini_function_call} # Extract thought signature from function_call's provider_specific_fields thought_signature = None @@ -1742,7 +1743,7 @@ def convert_to_gemini_tool_call_result( if gemini_call_id: _function_response["id"] = gemini_call_id - _part: Final[VertexPartType] = {"function_response": _function_response} + _part: Final[VertexPartType] = {"functionResponse": _function_response} if media_parts: if not supports_multimodal_function_responses: diff --git a/litellm/types/llms/vertex_ai.py b/litellm/types/llms/vertex_ai.py index a938f6a7b1c..34a56ec0eb0 100644 --- a/litellm/types/llms/vertex_ai.py +++ b/litellm/types/llms/vertex_ai.py @@ -62,8 +62,10 @@ class PartType(TypedDict, total=False): text: str inline_data: BlobType file_data: FileDataType - function_call: FunctionCall - function_response: FunctionResponse + function_call: FunctionCall # Accepted on input + functionCall: FunctionCall # Canonical Proto3 JSON emitted to upstream + function_response: FunctionResponse # Accepted on input + functionResponse: FunctionResponse # Canonical Proto3 JSON emitted to upstream thought: bool thoughtSignature: str media_resolution: Literal["low", "medium", "high"] 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 6c5c3f8c05f..804626f15ee 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 @@ -176,7 +176,7 @@ def test_convert_to_gemini_with_embedded_signature(): # Verify thought signature is extracted and sent to Gemini assert len(gemini_parts) == 1 - assert "function_call" in gemini_parts[0] + assert "functionCall" in gemini_parts[0] or "function_call" in gemini_parts[0] assert "thoughtSignature" in gemini_parts[0] assert gemini_parts[0]["thoughtSignature"] == test_signature