fix(gemini): group parallel function_response parts in a single Content object

When Gemini makes N parallel tool calls, the API requires all N function_response parts in one Content object. Previously each tool result created a separate Content, causing 400 INVALID_ARGUMENT errors. Merge consecutive function_response parts into the existing Content instead of appending new ones.
This commit is contained in:
nicoferdi96
2026-03-04 10:55:26 +01:00
parent 9336702ebc
commit c4445d6098

View File

@@ -634,9 +634,25 @@ class GeminiCompletion(BaseLLM):
function_response_part = types.Part.from_function_response(
name=tool_name, response=response_data
)
contents.append(
types.Content(role="user", parts=[function_response_part])
)
# Gemini requires all parallel function responses in a single
# Content object. When the previous Content already holds
# function_response parts, merge into it instead of creating
# a new Content.
if (
contents
and contents[-1].role == "user"
and contents[-1].parts
and all(
hasattr(p, "function_response")
and p.function_response is not None
for p in contents[-1].parts
)
):
contents[-1].parts.append(function_response_part)
else:
contents.append(
types.Content(role="user", parts=[function_response_part])
)
elif role == "assistant" and message.get("tool_calls"):
raw_parts: list[Any] | None = message.get("raw_tool_call_parts")
if raw_parts and all(isinstance(p, types.Part) for p in raw_parts):