From 0cba3449769fc8f63f7f7916bb2085abea483c2c Mon Sep 17 00:00:00 2001 From: Brandon Hancock Date: Fri, 24 Jan 2025 11:54:05 -0500 Subject: [PATCH] wip --- src/crewai/agent.py | 4 ++-- tests/agent_test.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/crewai/agent.py b/src/crewai/agent.py index 5823ef7f9..b53f30f5a 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -262,8 +262,8 @@ class Agent(BaseAgent): } )["output"] except Exception as e: - if isinstance(e, LiteLLMAuthenticationError): - # Do not retry on authentication errors + if e.__class__.__module__.startswith("litellm.exceptions"): + # Do not retry on litellm errors raise e self._times_executed += 1 if self._times_executed > self.max_retry_limit: diff --git a/tests/agent_test.py b/tests/agent_test.py index 46b20004e..7450b110d 100644 --- a/tests/agent_test.py +++ b/tests/agent_test.py @@ -1700,3 +1700,38 @@ def test_crew_agent_executor_litellm_auth_error(): ) # Verify the call was only made once (no retries) mock_llm_call.assert_called_once() + + +def test_litellm_anthropic_error_handling(): + """Test that AnthropicError from LiteLLM is handled correctly and not retried.""" + from litellm.llms.anthropic.common_utils import AnthropicError + + # Create an agent with a mocked LLM that uses an Anthropic model + agent = Agent( + role="test role", + goal="test goal", + backstory="test backstory", + llm=LLM(model="claude-3.5-sonnet-20240620"), + max_retry_limit=0, + ) + + # Create a task + task = Task( + description="Test task", + expected_output="Test output", + agent=agent, + ) + + # Mock the LLM call to raise AnthropicError + with ( + patch.object(LLM, "call") as mock_llm_call, + pytest.raises(AnthropicError, match="Test Anthropic error"), + ): + mock_llm_call.side_effect = AnthropicError( + status_code=500, + message="Test Anthropic error", + ) + agent.execute_task(task) + + # Verify the LLM call was only made once (no retries) + mock_llm_call.assert_called_once()