From 6f8211c8ead9591ed2314337a6b7acaa4ff73ade Mon Sep 17 00:00:00 2001 From: Piyush Kumar Date: Sat, 31 Oct 2026 07:47:49 +0000 Subject: [PATCH 1/2] 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 From 6bd65ed6f9064e593164ba421ccac964c2ccc64a Mon Sep 17 00:00:00 2001 From: Piyush Kumar Date: Sat, 31 Oct 2026 15:43:37 +0000 Subject: [PATCH 2/2] test(gemini): update transformation test assertions for canonical camelCase tool parts --- .../test_thought_signature_in_tool_call_id.py | 2 +- ...st_tool_call_followed_by_text_assistant.py | 4 +- .../test_vertex_ai_gemini_transformation.py | 62 +++++++++---------- ...test_vertex_and_google_ai_studio_gemini.py | 20 +++--- 4 files changed, 44 insertions(+), 44 deletions(-) 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 804626f15ee..5d124fa37e3 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 "functionCall" in gemini_parts[0] or "function_call" in gemini_parts[0] + assert "functionCall" in gemini_parts[0] assert "thoughtSignature" in gemini_parts[0] assert gemini_parts[0]["thoughtSignature"] == test_signature diff --git a/tests/unit/llms/vertex_ai/gemini/test_tool_call_followed_by_text_assistant.py b/tests/unit/llms/vertex_ai/gemini/test_tool_call_followed_by_text_assistant.py index a292b712a49..efb3764bbc6 100644 --- a/tests/unit/llms/vertex_ai/gemini/test_tool_call_followed_by_text_assistant.py +++ b/tests/unit/llms/vertex_ai/gemini/test_tool_call_followed_by_text_assistant.py @@ -48,10 +48,10 @@ def test_tool_result_matches_tool_call_with_text_assistant_in_between(): # The function response must be present and carry the correct tool name. function_responses = [ - part["function_response"] + part["functionResponse"] for content in contents for part in content["parts"] - if isinstance(part, dict) and part.get("function_response") + if isinstance(part, dict) and part.get("functionResponse") ] assert function_responses, f"expected a functionResponse part, got: {contents}" assert function_responses[0]["name"] == "shell" diff --git a/tests/unit/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py b/tests/unit/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py index c0ff931a41d..1a243f84b32 100644 --- a/tests/unit/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py +++ b/tests/unit/llms/vertex_ai/gemini/test_vertex_ai_gemini_transformation.py @@ -75,7 +75,7 @@ def test_responses_media_reaches_each_gemini_role(role: str) -> None: "role": "user", "parts": [ { - "function_response": { + "functionResponse": { "id": "call_media", "name": "get_media", "response": {"ok": True}, @@ -155,9 +155,9 @@ def test_tool_uri_policy_follows_provider_and_model_placement( response: Final = {"name": "get_media", "response": {"ok": True}} # Checked 2026-10-26: https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1/Content#FunctionResponsePart assert result[-1]["parts"] == ( - [{"function_response": {**response, "id": "call_media", "parts": [file]}}] + [{"functionResponse": {**response, "id": "call_media", "parts": [file]}}] if nested - else [{"function_response": response}, {**file, "video_metadata": {"fps": 2}}] + else [{"functionResponse": response}, {**file, "video_metadata": {"fps": 2}}] ) @@ -728,12 +728,12 @@ def test_thought_signature_preservation_in_conversion(): # Verify thought signature is preserved in first function call part assert len(gemini_parts) == 2 - assert "function_call" in gemini_parts[0] + assert "functionCall" in gemini_parts[0] assert "thoughtSignature" in gemini_parts[0] assert gemini_parts[0]["thoughtSignature"] == test_signature # Verify second function call part does not have thought signature - assert "function_call" in gemini_parts[1] + assert "functionCall" in gemini_parts[1] assert "thoughtSignature" not in gemini_parts[1] @@ -864,7 +864,7 @@ def test_dummy_signature_added_for_gemini_3_conversation_history(): # Verify dummy signature is added assert len(gemini_parts) == 1 - assert "function_call" in gemini_parts[0] + assert "functionCall" in gemini_parts[0] assert "thoughtSignature" in gemini_parts[0] # Verify it's the expected dummy signature (base64 encoded "skip_thought_signature_validator") @@ -905,7 +905,7 @@ def test_dummy_signature_not_added_for_gemini_2_5(): # Verify no dummy signature is added for non-gemini-3 models assert len(gemini_parts) == 1 - assert "function_call" in gemini_parts[0] + assert "functionCall" in gemini_parts[0] assert "thoughtSignature" not in gemini_parts[0] @@ -944,7 +944,7 @@ def test_dummy_signature_not_added_when_signature_exists(): # Verify real signature is preserved, not replaced with dummy assert len(gemini_parts) == 1 - assert "function_call" in gemini_parts[0] + assert "functionCall" in gemini_parts[0] assert "thoughtSignature" in gemini_parts[0] assert gemini_parts[0]["thoughtSignature"] == real_signature @@ -975,7 +975,7 @@ def test_dummy_signature_with_function_call_mode(): # Verify dummy signature is added assert len(gemini_parts) == 1 - assert "function_call" in gemini_parts[0] + assert "functionCall" in gemini_parts[0] assert "thoughtSignature" in gemini_parts[0] # Verify it's the expected dummy signature @@ -1778,9 +1778,9 @@ def test_convert_tool_response_with_base64_image(): assert isinstance(result, list), "Should return a parts list when media is present" assert len(result) == 1, "Should return one function_response part" result_part = result[0] - assert "function_response" in result_part + assert "functionResponse" in result_part assert "inline_data" not in result_part - function_response = result_part["function_response"] + function_response = result_part["functionResponse"] assert function_response["name"] == "click_at" assert "response" in function_response # Verify JSON response is parsed correctly @@ -1883,7 +1883,7 @@ def test_gemini_history_places_tool_media_for_model( assert contents[-2] == { "role": "user", "parts": [ - {"function_response": function_response}, + {"functionResponse": function_response}, *([] if nested else media_parts), ], } @@ -1920,8 +1920,8 @@ def test_convert_tool_response_text_only(): assert not isinstance(result, list), "Should return single part when no image" # Check function_response exists - assert "function_response" in result - function_response = result["function_response"] + assert "functionResponse" in result + function_response = result["functionResponse"] assert function_response["name"] == "wait_5_seconds" # Verify JSON response is parsed correctly assert "status" in function_response["response"] @@ -2209,9 +2209,9 @@ def test_convert_tool_response_with_pdf_file(): assert isinstance(result, list), "Should return a parts list when media is present" assert len(result) == 1, "Should return one function_response part" result_part = result[0] - assert "function_response" in result_part + assert "functionResponse" in result_part assert "inline_data" not in result_part - function_response = result_part["function_response"] + function_response = result_part["functionResponse"] assert function_response["name"] == "analyze_document" assert "response" in function_response # Verify JSON response is parsed correctly @@ -2259,7 +2259,7 @@ def test_convert_tool_response_with_input_file_type(): # Check inline_data is nested under functionResponse.parts. assert isinstance(result, list), "Should return a parts list when media is present" assert len(result) == 1, "Should return one function_response part" - function_response = result[0]["function_response"] + function_response = result[0]["functionResponse"] assert ( function_response["parts"][0]["inline_data"]["mime_type"] == "application/pdf" ) @@ -2296,7 +2296,7 @@ def test_convert_tool_response_with_nested_file_object(): # Check inline_data is nested under functionResponse.parts. assert isinstance(result, list), "Should return a parts list when media is present" assert len(result) == 1, "Should return one function_response part" - function_response = result[0]["function_response"] + function_response = result[0]["functionResponse"] inline_data: BlobType = function_response["parts"][0]["inline_data"] assert "data" in inline_data assert "mime_type" in inline_data @@ -2548,11 +2548,11 @@ def test_function_response_has_user_role(): assert contents[0]["role"] == "user" assert contents[1]["role"] == "model" - assert "function_call" in contents[1]["parts"][0] + assert "functionCall" in contents[1]["parts"][0] # The critical assertion: function response must have role="user" assert contents[2]["role"] == "user" - assert "function_response" in contents[2]["parts"][0] + assert "functionResponse" in contents[2]["parts"][0] def test_multi_turn_function_calling_roles(): @@ -2624,9 +2624,9 @@ def test_multi_turn_function_calling_roles(): # Verify the function response blocks specifically have role="user" for i, content in enumerate(contents): for part in content["parts"]: - if "function_response" in part: + if "functionResponse" in part or "function_response" in part: assert content["role"] == "user", ( - f"Content block {i} with function_response has role='{content['role']}', expected 'user'" + f"Content block {i} with functionResponse has role='{content['role']}', expected 'user'" ) @@ -2695,7 +2695,7 @@ def test_gemini_thought_signature_preservation_real_response(): assert ( parts_out[0]["thoughtSignature"] == "mock_signature_from_text_part" ) - assert "function_call" in parts_out[1] + assert "functionCall" in parts_out[1] assert "thoughtSignature" not in parts_out[1] @@ -2730,7 +2730,7 @@ def test_gemini_thought_signature_deduplication_assumed_response(): assert len(parts_out) == 2 assert "text" in parts_out[0] assert "thoughtSignature" not in parts_out[0] - assert "function_call" in parts_out[1] + assert "functionCall" in parts_out[1] assert parts_out[1]["thoughtSignature"] == "mock_signature_63k" @@ -2788,7 +2788,7 @@ def test_gemini_thought_signature_pure_tool_call(): assert "parts" in converted[0] parts_out = converted[0]["parts"] assert len(parts_out) == 1 - assert "function_call" in parts_out[0] + assert "functionCall" in parts_out[0] assert parts_out[0]["thoughtSignature"] == "pure_tool_signature" @@ -2816,7 +2816,7 @@ def test_gemini_distinct_text_and_tool_signatures_are_both_preserved(): assert parts[0]["text"] == "Some analysis." assert parts[0]["thoughtSignature"] == "text_signature" - assert "function_call" in parts[1] + assert "functionCall" in parts[1] assert parts[1]["thoughtSignature"] == "tool_signature" @@ -2847,7 +2847,7 @@ def test_gemini_25_text_signature_survives_replay_to_gemini_3(): assert parts[0]["text"] == "I will list the directory." assert parts[0]["thoughtSignature"] == "real_25_signature" - assert "function_call" in parts[1] + assert "functionCall" in parts[1] assert parts[1]["thoughtSignature"] == _get_dummy_thought_signature() @@ -2890,7 +2890,7 @@ def test_gemini_function_call_signature_round_trip_no_duplicate(): signatures = [p["thoughtSignature"] for p in parts if "thoughtSignature" in p] assert signatures == ["signature_from_function_call"] assert "thoughtSignature" not in parts[0] - assert "function_call" in parts[1] + assert "functionCall" in parts[1] def test_gemini_server_side_tool_signature_not_duplicated_on_text(): @@ -2958,8 +2958,8 @@ def test_convert_tool_response_with_url_image(content_type: str, nested: bool) - inline: Final = {"inline_data": {"data": base64.b64encode(WHITE_PNG).decode(), "mime_type": "image/png"}} response: Final = {"name": "type_text_at", "response": {"url": "https://example.com"}} assert result == ( - [{"function_response": {**response, "parts": [inline]}}] if nested else - [{"function_response": response}, {**inline, "media_resolution": {"level": "MEDIA_RESOLUTION_HIGH"}}] + [{"functionResponse": {**response, "parts": [inline]}}] if nested else + [{"functionResponse": response}, {**inline, "media_resolution": {"level": "MEDIA_RESOLUTION_HIGH"}}] ) @@ -3015,4 +3015,4 @@ def test_anthropic_messages_history_replays_to_gemini_without_claude_signature() signatures: Final = [part["thoughtSignature"] for part in parts if "thoughtSignature" in part] assert signatures == [_get_dummy_thought_signature()] assert [part for part in parts if part.get("text") == thinking] == [{"thought": True, "text": thinking}] - assert next(part for part in parts if "function_call" in part)["thoughtSignature"] == _get_dummy_thought_signature() + assert next(part for part in parts if "functionCall" in part)["thoughtSignature"] == _get_dummy_thought_signature() diff --git a/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py b/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py index 98d1b8f1300..af0c9de0f70 100644 --- a/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py +++ b/tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py @@ -3784,10 +3784,10 @@ def _collect_function_call_ids(contents): function_response_ids = [] for content in contents: for part in content.get("parts", []): - fc = part.get("function_call") + fc = part.get("functionCall") or part.get("function_call") if fc is not None: function_call_ids.append(fc.get("id")) - fr = part.get("function_response") + fr = part.get("functionResponse") or part.get("function_response") if fr is not None: function_response_ids.append(fr.get("id")) return function_call_ids, function_response_ids @@ -3841,10 +3841,10 @@ def test_gemini_25_tool_calls_omit_function_call_id(custom_llm_provider): for content in contents: for part in content.get("parts", []): - fc = part.get("function_call") + fc = part.get("functionCall") or part.get("function_call") if fc is not None: assert "id" not in fc, f"gemini-2.5 payload must not include id: {fc}" - fr = part.get("function_response") + fr = part.get("functionResponse") or part.get("function_response") if fr is not None: assert "id" not in fr, f"gemini-2.5 payload must not include id: {fr}" @@ -9178,7 +9178,7 @@ def test_tool_call_no_arguments(tool_call_no_arguments): result = convert_to_gemini_tool_call_invoke(tool_call_no_arguments) print(result) - assert result == [{"function_call": {"name": "Get-FAQ", "args": {}}}] + assert result == [{"functionCall": {"name": "Get-FAQ", "args": {}}}] def vertex_httpx_mock_post_valid_response(*args, **kwargs): @@ -9587,7 +9587,7 @@ def test_tool_name_conversion(): ] translated_messages = gemini_convert_messages_with_history(messages=messages) print(f"\n\ntranslated_messages: {translated_messages}\ntranslated_messages") - assert translated_messages[-1]["parts"][0]["function_response"]["name"] == "get_weather" + assert translated_messages[-1]["parts"][0]["functionResponse"]["name"] == "get_weather" def test_prompt_factory_nested(): @@ -9807,7 +9807,7 @@ def test_gemini_function_call_parameter_in_messages(): "role": "model", "parts": [ { - "function_call": { + "functionCall": { "name": "search", "args": {"queries": ["weather in boston"]}, } @@ -9818,7 +9818,7 @@ def test_gemini_function_call_parameter_in_messages(): "role": "user", "parts": [ { - "function_response": { + "functionResponse": { "name": "search", "response": {"content": "The current weather in Boston is 22°F."}, } @@ -9888,7 +9888,7 @@ def test_gemini_function_call_parameter_in_messages_2(): "parts": [ {"text": "Sure, let me check."}, { - "function_call": { + "functionCall": { "name": "search", "args": {"queries": ["weather in boston"]}, } @@ -9899,7 +9899,7 @@ def test_gemini_function_call_parameter_in_messages_2(): "role": "user", "parts": [ { - "function_response": { + "functionResponse": { "name": "search", "response": {"content": "The weather in Boston is 100 degrees."}, }