From c4445d609829f695d09b65add0bcc01b7af1f41c Mon Sep 17 00:00:00 2001 From: nicoferdi96 Date: Wed, 4 Mar 2026 10:55:26 +0100 Subject: [PATCH] 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. --- .../llms/providers/gemini/completion.py | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/crewai/src/crewai/llms/providers/gemini/completion.py b/lib/crewai/src/crewai/llms/providers/gemini/completion.py index d854c150d..32ef3650e 100644 --- a/lib/crewai/src/crewai/llms/providers/gemini/completion.py +++ b/lib/crewai/src/crewai/llms/providers/gemini/completion.py @@ -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):