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.

feat: improve LLM message formatting performance (#3251)

633WHU915857541eb0
diff --git a/src/crewai/llm.py b/src/crewai/llm.py
index 40c026684..c701ddf0b 100644
--- a/src/crewai/llm.py
+++ b/src/crewai/llm.py
@@ -1134,23 +1134,13 @@ class LLM(BaseLLM):
if "mistral" in self.model.lower():
# Check if the last message has a role of 'assistant'
if messages and messages[-1]["role"] == "assistant":
- # Add a dummy user message to ensure the last message has a role of 'user'
- messages = (
- messages.copy()
- ) # Create a copy to avoid modifying the original
- messages.append({"role": "user", "content": "Please continue."})
+ return messages + [{"role": "user", "content": "Please continue."}]
return messages
# TODO: Remove this code after merging PR https://github.com/BerriAI/litellm/pull/10917
# Ollama doesn't supports last message to be 'assistant'
- if (
- "ollama" in self.model.lower()
- and messages
- and messages[-1]["role"] == "assistant"
- ):
- messages = messages.copy()
- messages.append({"role": "user", "content": ""})
- return messages
+ if "ollama" in self.model.lower() and messages and messages[-1]["role"] == "assistant":
+ return messages + [{"role": "user", "content": ""}]
# Handle Anthropic models
if not self.is_anthropic: