Skip to content

Commit

Permalink
Fix tests and code that was broken
Browse files Browse the repository at this point in the history
  • Loading branch information
bhancockio committed Jan 22, 2025
1 parent 37d425b commit 8bd292e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 249 deletions.
3 changes: 2 additions & 1 deletion src/crewai/agents/crew_agent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ def _invoke_loop(self):
"""
formatted_answer = None
while not isinstance(formatted_answer, AgentFinish):
self.iterations += 1
try:
if self._has_reached_max_iterations():
formatted_answer = self._handle_max_iterations_exceeded(
Expand Down Expand Up @@ -159,6 +158,8 @@ def _invoke_loop(self):
content=f"Unhandled exception: {e}",
color="red",
)
finally:
self.iterations += 1

self._show_logs(formatted_answer)
return formatted_answer
Expand Down
27 changes: 12 additions & 15 deletions tests/agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from crewai.tools.tool_calling import InstructorToolCalling
from crewai.tools.tool_usage import ToolUsage
from crewai.tools.tool_usage_events import ToolUsageFinished
from crewai.utilities import RPMController
from crewai.utilities import Printer, RPMController
from crewai.utilities.events import Emitter


Expand Down Expand Up @@ -670,7 +670,7 @@ def get_final_answer() -> float:
moveon.assert_called_once()


@pytest.mark.vcr(filter_headers=["authorization"])
# @pytest.mark.vcr(filter_headers=["authorization"])
def test_agent_error_on_parsing_tool(capsys):
from unittest.mock import patch

Expand Down Expand Up @@ -710,6 +710,7 @@ def get_final_answer() -> float:
force_exception_2.side_effect = Exception("Error on parsing tool.")
crew.kickoff()
captured = capsys.readouterr()
print("Captured output:", captured.out)
assert "Error on parsing tool." in captured.out


Expand Down Expand Up @@ -1637,21 +1638,19 @@ def test_litellm_auth_error_handling():
mock_llm_call.assert_called_once()


@pytest.mark.vcr(filter_headers=["authorization"])
def test_crew_agent_executor_litellm_auth_error():
"""Test that CrewAgentExecutor properly identifies and handles LiteLLM authentication errors."""
from litellm import AuthenticationError as LiteLLMAuthenticationError

from crewai.agents.tools_handler import ToolsHandler
from crewai.utilities import Logger
from crewai.utilities import Printer

# Create an agent and executor with max_retry_limit=0
agent = Agent(
role="test role",
goal="test goal",
backstory="test backstory",
llm=LLM(model="gpt-4"),
max_retry_limit=0, # Disable retries for authentication errors
llm=LLM(model="gpt-4", api_key="invalid_api_key"),
)
task = Task(
description="Test task",
Expand All @@ -1677,7 +1676,7 @@ def test_crew_agent_executor_litellm_auth_error():
# Mock the LLM call to raise LiteLLMAuthenticationError
with (
patch.object(LLM, "call") as mock_llm_call,
patch.object(Logger, "log") as mock_logger,
patch.object(Printer, "print") as mock_printer,
pytest.raises(LiteLLMAuthenticationError, match="Invalid API key"),
):
mock_llm_call.side_effect = LiteLLMAuthenticationError(
Expand All @@ -1686,20 +1685,18 @@ def test_crew_agent_executor_litellm_auth_error():
executor.invoke(
{
"input": "test input",
"tool_names": "", # Required template variable
"tools": "", # Required template variable
"tool_names": "",
"tools": "",
}
)

# Verify error handling
mock_logger.assert_any_call(
level="error",
message="Authentication error with litellm occurred. Please check your API key and configuration.",
mock_printer.assert_any_call(
content="Authentication error with litellm occurred. Please check your API key and configuration.",
color="red",
)
mock_logger.assert_any_call(
level="error",
message="Error details: litellm.AuthenticationError: Invalid API key",
mock_printer.assert_any_call(
content="Error details: litellm.AuthenticationError: Invalid API key",
color="red",
)
# Verify the call was only made once (no retries)
Expand Down
233 changes: 0 additions & 233 deletions tests/cassettes/test_agent_error_on_parsing_tool.yaml

This file was deleted.

0 comments on commit 8bd292e

Please sign in to comment.