Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix litellm issues to be more broad #1960

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ Now let's get you set up! 🚀
│ └── __init__.py
└── config/
├── agents.yaml
├── config.yaml
└── tasks.yaml
```
</Frame>
Expand Down
24 changes: 5 additions & 19 deletions src/crewai/agents/crew_agent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,33 +149,19 @@ def _invoke_loop(self):
if self._is_context_length_exceeded(e):
self._handle_context_length()
continue
elif self._is_litellm_authentication_error(e):
self._handle_litellm_auth_error(e)
raise e
else:
self._printer.print(
content=f"Unhandled exception: {e}",
color="red",
)
self._handle_unknown_error(e)
raise e
finally:
self.iterations += 1

self._show_logs(formatted_answer)
return formatted_answer

def _is_litellm_authentication_error(self, exception: Exception) -> bool:
"""Check if the exception is a litellm authentication error."""
if LiteLLMAuthenticationError and isinstance(
exception, LiteLLMAuthenticationError
):
return True

return False

def _handle_litellm_auth_error(self, exception: Exception) -> None:
"""Handle litellm authentication error by informing the user and exiting."""
def _handle_unknown_error(self, exception: Exception) -> None:
"""Handle unknown errors by informing the user."""
self._printer.print(
content="Authentication error with litellm occurred. Please check your API key and configuration.",
content="An unknown error occurred. Please check the details below.",
color="red",
)
self._printer.print(
Expand Down
6 changes: 3 additions & 3 deletions tests/agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,13 +1638,13 @@ def test_litellm_auth_error_handling():


def test_crew_agent_executor_litellm_auth_error():
"""Test that CrewAgentExecutor properly identifies and handles LiteLLM authentication errors."""
"""Test that CrewAgentExecutor handles LiteLLM authentication errors by raising them."""
from litellm import AuthenticationError as LiteLLMAuthenticationError

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

# Create an agent and executor with max_retry_limit=0
# Create an agent and executor
agent = Agent(
role="test role",
goal="test goal",
Expand Down Expand Up @@ -1691,7 +1691,7 @@ def test_crew_agent_executor_litellm_auth_error():

# Verify error handling
mock_printer.assert_any_call(
content="Authentication error with litellm occurred. Please check your API key and configuration.",
content="An unknown error occurred. Please check the details below.",
color="red",
)
mock_printer.assert_any_call(
Expand Down
Loading