diff --git a/src/crewai/agent.py b/src/crewai/agent.py index 999d1d800..074611542 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -196,9 +196,11 @@ class Agent(BaseAgent): else: # For any other type, attempt to extract relevant attributes llm_params = { - "model": getattr(self.llm, "model_name", None) - or getattr(self.llm, "deployment_name", None) - or str(self.llm), + "model": self._normalize_model_name( + getattr(self.llm, "model_name", None) + or getattr(self.llm, "deployment_name", None) + or str(self.llm) + ), "temperature": getattr(self.llm, "temperature", None), "max_tokens": getattr(self.llm, "max_tokens", None), "logprobs": getattr(self.llm, "logprobs", None), @@ -534,5 +536,14 @@ class Agent(BaseAgent): def __tools_names(tools) -> str: return ", ".join([t.name for t in tools]) + def _normalize_model_name(self, model_name): + """ + Normalize the model name by removing any 'models/' prefix. + This fixes the issue with ChatGoogleGenerativeAI and potentially other LLM providers. + """ + if model_name and isinstance(model_name, str) and model_name.startswith("models/"): + return model_name[7:] # Remove "models/" prefix + return model_name + def __repr__(self): return f"Agent(role={self.role}, goal={self.goal}, backstory={self.backstory})" diff --git a/tests/test_agent_model_name.py b/tests/test_agent_model_name.py new file mode 100644 index 000000000..14642d74e --- /dev/null +++ b/tests/test_agent_model_name.py @@ -0,0 +1,39 @@ +import pytest +from unittest.mock import MagicMock, patch +from crewai import Agent +from crewai.llm import LLM + + +def test_normalize_model_name_method(): + """Test that the _normalize_model_name method correctly handles model names with 'models/' prefix""" + agent = Agent( + role="Test Agent", + goal="Test goal", + backstory="Test backstory", + llm="gpt-4" + ) + + model_with_prefix = "models/gemini/gemini-1.5-flash" + normalized_name = agent._normalize_model_name(model_with_prefix) + assert normalized_name == "gemini/gemini-1.5-flash" + + regular_model = "gpt-4" + assert agent._normalize_model_name(regular_model) == "gpt-4" + + assert agent._normalize_model_name(None) is None + + assert agent._normalize_model_name(123) == 123 + + +def test_agent_with_regular_model_name(): + """Test that the Agent class doesn't modify normal model names""" + with patch('crewai.agent.LLM') as mock_llm: + agent = Agent( + role="Test Agent", + goal="Test goal", + backstory="Test backstory", + llm="gpt-4" + ) + + args, kwargs = mock_llm.call_args + assert kwargs["model"] == "gpt-4"