diff --git a/litellm/llms/vertex_ai/gemini/transformation.py b/litellm/llms/vertex_ai/gemini/transformation.py index bbd4a96980a..bc918381344 100644 --- a/litellm/llms/vertex_ai/gemini/transformation.py +++ b/litellm/llms/vertex_ai/gemini/transformation.py @@ -936,14 +936,17 @@ def gemini_convert_messages_with_history( model=model, forward_function_call_id=forward_function_call_id, ) - ## check if gemini_tool_call already exists in assistant_content - for gemini_tool_call_part in gemini_tool_call_parts: + ## check if gemini_tool_call already exists in assistant_content before this batch + pre_existing_parts = list(assistant_content) + assistant_content.extend( + gemini_tool_call_part + for gemini_tool_call_part in gemini_tool_call_parts if not check_if_part_exists_in_parts( - assistant_content, + pre_existing_parts, gemini_tool_call_part, excluded_keys=["thoughtSignature"], - ): - assistant_content.append(gemini_tool_call_part) + ) + ) # Only record this as the active tool-call message when it actually # carries tool calls. The `if` guard above is also entered for a # text-only assistant message (`assistant_msg.get("tool_calls", []) 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..b994c36689d 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 @@ -3016,3 +3016,28 @@ def test_anthropic_messages_history_replays_to_gemini_without_claude_signature() 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() + +def test_parallel_tool_calls_with_identical_name_and_arguments() -> None: + """Parallel tool calls with identical name and arguments must both be preserved. + + Regression test for https://github.com/BerriAI/litellm/issues/48831 + """ + messages: Final = [ + {"role": "user", "content": "What time is it? Ask twice."}, + { + "role": "assistant", + "content": None, + "tool_calls": [ + {"id": "call_1", "type": "function", "function": {"name": "get_time", "arguments": "{}"}}, + {"id": "call_2", "type": "function", "function": {"name": "get_time", "arguments": "{}"}}, + ], + }, + {"role": "tool", "tool_call_id": "call_1", "content": "12:00"}, + {"role": "tool", "tool_call_id": "call_2", "content": "12:01"}, + ] + + contents = gemini_convert_messages_with_history(messages=messages, model="gemini-2.5-flash") + model_parts = next(c["parts"] for c in contents if c.get("role") == "model") + function_calls = [p for p in model_parts if "function_call" in p or "functionCall" in p] + assert len(function_calls) == 2 +