Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

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

nicoferdi96ad82e52d39c4
diff --git a/lib/crewai/src/crewai/llms/providers/gemini/completion.py b/lib/crewai/src/crewai/llms/providers/gemini/completion.py
index d854c150d..fd0530abe 100644
--- a/lib/crewai/src/crewai/llms/providers/gemini/completion.py
+++ b/lib/crewai/src/crewai/llms/providers/gemini/completion.py
@@ -634,9 +634,17 @@ 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])
- )
+ if (
+ contents
+ and contents[-1].role == "user"
+ and contents[-1].parts
+ and contents[-1].parts[-1].function_response is not None
+ ):
+ 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):