Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
fix: append trailing user turn in native Gemini provider (#6973)
Havel Cyrus92eb5f91830c
diff --git a/lib/crewai/src/crewai/llm.py b/lib/crewai/src/crewai/llm.pyindex c64445518..c2924e542 100644--- a/lib/crewai/src/crewai/llm.py+++ b/lib/crewai/src/crewai/llm.py@@ -2391,6 +2391,14 @@ class LLM(BaseLLM):):return [*messages, {"role": "user", "content": ""}] # type: ignore[list-item]+ # Handle Gemini models - they require the last message to not be 'assistant'+ if (+ ("gemini" in self.model.lower() or "google" in self.model.lower())+ and messages+ and messages[-1]["role"] == "assistant"+ ):+ return [*messages, {"role": "user", "content": "Please continue."}] # type: ignore[list-item]+if not self.is_anthropic:return messages # type: ignore[return-value]diff --git a/lib/crewai/src/crewai/llms/providers/gemini/completion.py b/lib/crewai/src/crewai/llms/providers/gemini/completion.pyindex 0bdf8ec0e..98edc1238 100644--- a/lib/crewai/src/crewai/llms/providers/gemini/completion.py+++ b/lib/crewai/src/crewai/llms/providers/gemini/completion.py@@ -562,12 +562,19 @@ class GeminiCompletion(BaseLLM):- System messages are separate system_instruction- Content is organized as Content objects with Parts- Roles are 'user' and 'model' (not 'assistant')+ - History may not end on a model turn; a "Please continue." user turn+ is appended when it doesArgs:messages: Input messagesReturns:Tuple of (formatted_contents, system_instruction)++ Raises:+ ValueError: If the history ends on a model turn with an unresolved+ function call, which requires a function response rather than a+ continuation prompt."""base_formatted = super()._format_messages(messages)@@ -680,6 +687,23 @@ class GeminiCompletion(BaseLLM):gemini_content = types.Content(role=gemini_role, parts=parts)contents.append(gemini_content)+ if contents and contents[-1].role == "model":+ # Gemini's generateContent API rejects a request whose history ends+ # on a model turn (agent loops can produce this, e.g. after+ # max-iteration handling or a guardrail retry).+ last_parts = contents[-1].parts or []+ if any(part.function_call for part in last_parts):+ raise ValueError(+ "Gemini message history ends on an unresolved function call "+ "-- a function response must be provided before calling the "+ "model again."+ )+ contents.append(+ types.Content(+ role="user", parts=[types.Part.from_text(text="Please continue.")]+ )+ )+return contents, system_instructiondef _validate_and_emit_structured_output(diff --git a/lib/crewai/tests/llms/google/test_google.py b/lib/crewai/tests/llms/google/test_google.pyindex 2c03dcd23..48c9f3224 100644--- a/lib/crewai/tests/llms/google/test_google.py+++ b/lib/crewai/tests/llms/google/test_google.py@@ -500,6 +500,66 @@ def test_gemini_message_formatting():assert formatted_contents[1].role == "model"+def test_gemini_message_formatting_appends_user_turn_after_trailing_model_turn():+ """+ Gemini's generateContent API rejects a request whose history ends on a+ model turn ("Requests ending with a model turn are not supported"). Agent+ loops can produce this (e.g. after max-iteration handling or a guardrail+ retry), so formatting must append a synthetic user turn to recover.+ """+ llm = LLM(model="google/gemini-2.0-flash-001")++ test_messages = [+ {"role": "user", "content": "Hello"},+ {"role": "assistant", "content": "Hi there!"},+ ]++ formatted_contents, _ = llm._format_messages_for_gemini(test_messages)++ assert [content.role for content in formatted_contents] == [+ "user",+ "model",+ "user",+ ]+ assert formatted_contents[0].parts[0].text == "Hello"+ assert formatted_contents[1].parts[0].text == "Hi there!"+ assert formatted_contents[2].parts[0].text == "Please continue."+ assert test_messages == [+ {"role": "user", "content": "Hello"},+ {"role": "assistant", "content": "Hi there!"},+ ]+++def test_gemini_message_formatting_leaves_user_terminated_history_unchanged():+ llm = LLM(model="google/gemini-2.0-flash-001")++ test_messages = [{"role": "user", "content": "Hello"}]++ formatted_contents, _ = llm._format_messages_for_gemini(test_messages)++ assert len(formatted_contents) == 1+ assert formatted_contents[0].role == "user"+ assert formatted_contents[0].parts[0].text == "Hello"+++def test_gemini_message_formatting_raises_on_unresolved_function_call():+ llm = LLM(model="google/gemini-2.0-flash-001")++ test_messages = [+ {"role": "user", "content": "What's the weather?"},+ {+ "role": "assistant",+ "content": "",+ "tool_calls": [+ {"function": {"name": "get_weather", "arguments": "{}"}}+ ],+ },+ ]++ with pytest.raises(ValueError, match="unresolved function call"):+ llm._format_messages_for_gemini(test_messages)++def test_gemini_streaming_parameter():"""Test that streaming parameter is properly handleddiff --git a/lib/crewai/tests/test_llm.py b/lib/crewai/tests/test_llm.pyindex 7f16cd933..ec15e8a3c 100644--- a/lib/crewai/tests/test_llm.py+++ b/lib/crewai/tests/test_llm.py@@ -849,6 +849,33 @@ def test_ollama_does_not_modify_when_last_is_user(ollama_llm):assert formatted == original_messages+def gemini_litellm_llm():+ return LLM(model="gemini/gemini-flash-latest", is_litellm=True)+++def test_gemini_litellm_appends_user_message_when_last_is_assistant(gemini_litellm_llm):+ original_messages = [+ {"role": "user", "content": "Hi there"},+ {"role": "assistant", "content": "Hello!"},+ ]++ formatted = gemini_litellm_llm._format_messages_for_provider(original_messages)++ assert len(formatted) == len(original_messages) + 1+ assert formatted[-1] == {"role": "user", "content": "Please continue."}+++def test_gemini_litellm_does_not_modify_when_last_is_user(gemini_litellm_llm):+ original_messages = [+ {"role": "user", "content": "Tell me a joke."},+ ]++ formatted = gemini_litellm_llm._format_messages_for_provider(original_messages)++ assert formatted == original_messages++def test_native_provider_raises_error_when_supported_but_fails():"""Test that when a native provider is in SUPPORTED_NATIVE_PROVIDERS but fails to instantiate, we raise the error."""with patch("crewai.llm.SUPPORTED_NATIVE_PROVIDERS", ["openai"]):