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]

Fix gemini tool call indexes (#11558)

Low Jian Shengac3046227039
diff --git a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
index 495597a3812..53d4073e1e3 100644
--- a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
+++ b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
@@ -776,7 +776,6 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
@staticmethod
def _transform_parts(
parts: List[HttpxPartType],
- index: int,
is_function_call: Optional[bool],
) -> Tuple[
Optional[ChatCompletionToolCallFunctionChunk],
@@ -784,6 +783,9 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
]:
function: Optional[ChatCompletionToolCallFunctionChunk] = None
_tools: List[ChatCompletionToolCallChunk] = []
+ # in a single chunk, each tool call appears as a separate part
+ # they need to be separate indexes as they are separate tool calls
+ funcCallIndex = 0
for part in parts:
if "functionCall" in part:
_function_chunk = ChatCompletionToolCallFunctionChunk(
@@ -797,9 +799,10 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
id=f"call_{str(uuid.uuid4())}",
type="function",
function=_function_chunk,
- index=index,
+ index=funcCallIndex,
)
_tools.append(_tool_response_chunk)
+ funcCallIndex += 1
if len(_tools) == 0:
tools: Optional[List[ChatCompletionToolCallChunk]] = None
else:
@@ -1113,7 +1116,6 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
functions, tools = VertexGeminiConfig._transform_parts(
parts=candidate["content"]["parts"],
- index=candidate.get("index", idx),
is_function_call=is_function_call(standard_optional_params),
)
diff --git a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
index 6b16bb4de79..5c1648088c2 100644
--- a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
+++ b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
@@ -598,3 +598,130 @@ def test_vertex_ai_streaming_usage_web_search_calculation():
usage: Usage = completed_response.usage
assert usage.prompt_tokens_details.web_search_requests is not None
assert usage.prompt_tokens_details.web_search_requests == 1
+
+
+def test_vertex_ai_transform_parts():
+ """
+ Test the _transform_parts method for converting Vertex AI function calls
+ to OpenAI-compatible tool calls and function calls.
+ """
+ from litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import (
+ VertexGeminiConfig,
+ )
+ from litellm.types.llms.vertex_ai import HttpxPartType
+
+ # Test case 1: Function call mode (is_function_call=True)
+ parts_with_function = [
+ HttpxPartType(
+ functionCall={
+ "name": "get_current_weather",
+ "args": {"location": "Boston", "unit": "celsius"}
+ }
+ ),
+ HttpxPartType(text="Some text content")
+ ]
+
+ function, tools = VertexGeminiConfig._transform_parts(
+ parts=parts_with_function,
+ is_function_call=True
+ )
+
+ # Should return function, no tools
+ assert function is not None
+ assert function["name"] == "get_current_weather"
+ assert function["arguments"] == '{"location": "Boston", "unit": "celsius"}'
+ assert tools is None
+
+ # Test case 2: Tool call mode (is_function_call=False)
+ function, tools = VertexGeminiConfig._transform_parts(
+ parts=parts_with_function,
+ is_function_call=False
+ )
+
+ # Should return tools, no function
+ assert function is None
+ assert tools is not None
+ assert len(tools) == 1
+ assert tools[0]["type"] == "function"
+ assert tools[0]["function"]["name"] == "get_current_weather"
+ assert tools[0]["function"]["arguments"] == '{"location": "Boston", "unit": "celsius"}'
+ assert tools[0]["id"].startswith("call_")
+ assert tools[0]["index"] == 0
+
+ # Test case 3: Multiple function calls
+ parts_with_multiple_functions = [
+ HttpxPartType(
+ functionCall={
+ "name": "get_current_weather",
+ "args": {"location": "Boston"}
+ }
+ ),
+ HttpxPartType(
+ functionCall={
+ "name": "get_forecast",
+ "args": {"location": "New York", "days": 3}
+ }
+ ),
+ HttpxPartType(text="Some text content")
+ ]
+
+ function, tools = VertexGeminiConfig._transform_parts(
+ parts=parts_with_multiple_functions,
+ is_function_call=False
+ )
+
+ # Should return multiple tools
+ assert function is None
+ assert tools is not None
+ assert len(tools) == 2
+ assert tools[0]["function"]["name"] == "get_current_weather"
+ assert tools[0]["index"] == 0
+ assert tools[1]["function"]["name"] == "get_forecast"
+ assert tools[1]["index"] == 1
+ assert tools[1]["function"]["arguments"] == '{"location": "New York", "days": 3}'
+
+ # Test case 4: No function calls
+ parts_without_functions = [
+ HttpxPartType(text="Just some text content"),
+ HttpxPartType(text="More text")
+ ]
+
+ function, tools = VertexGeminiConfig._transform_parts(
+ parts=parts_without_functions,
+ is_function_call=False
+ )
+
+ # Should return nothing
+ assert function is None
+ assert tools is None
+
+ # Test case 5: Empty parts list
+ function, tools = VertexGeminiConfig._transform_parts(
+ parts=[],
+ is_function_call=False
+ )
+
+ # Should return nothing
+ assert function is None
+ assert tools is None
+
+ # Test case 6: Function call with empty args
+ parts_with_empty_args = [
+ HttpxPartType(
+ functionCall={
+ "name": "simple_function",
+ "args": {}
+ }
+ )
+ ]
+
+ function, tools = VertexGeminiConfig._transform_parts(
+ parts=parts_with_empty_args,
+ is_function_call=True
+ )
+
+ # Should handle empty args correctly
+ assert function is not None
+ assert function["name"] == "simple_function"
+ assert function["arguments"] == "{}"
+ assert tools is None