Improve error handling in kickoff_async with LLMError exception class

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-27 09:37:26 +00:00
parent 7907c8a147
commit ce38a3d70e
3 changed files with 73 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
import asyncio
import json
import logging
import re
import uuid
import warnings
@@ -38,6 +39,7 @@ from crewai.tasks.conditional_task import ConditionalTask
from crewai.tasks.task_output import TaskOutput
from crewai.tools.agent_tools.agent_tools import AgentTools
from crewai.tools.base_tool import BaseTool, Tool
from crewai.utilities.exceptions.llm_error import LLMError
from crewai.types.usage_metrics import UsageMetrics
from crewai.utilities import I18N, FileHandler, Logger, RPMController
from crewai.utilities.constants import TRAINING_DATA_FILE
@@ -683,11 +685,23 @@ class Crew(BaseModel):
return results
async def kickoff_async(self, inputs: Optional[Dict[str, Any]] = {}) -> CrewOutput:
"""Asynchronous kickoff method to start the crew execution."""
"""Asynchronous kickoff method to start the crew execution.
Args:
inputs (Optional[Dict[str, Any]]): Input parameters for the crew execution
Returns:
CrewOutput: The result of the crew execution
Raises:
LLMError: When LLM-specific errors occur
Exception: For other unexpected errors
"""
try:
return await asyncio.to_thread(self.kickoff, inputs)
except Exception as e:
raise
logging.error(f"Error during async crew execution: {str(e)}")
raise LLMError(f"Crew execution failed: {str(e)}", original_error=e)
async def kickoff_for_each_async(self, inputs: List[Dict]) -> List[CrewOutput]:
crew_copies = [self.copy() for _ in inputs]

View File

@@ -0,0 +1,16 @@
"""Exception class for LLM-related errors."""
from typing import Optional
class LLMError(Exception):
"""Base exception class for LLM operation errors."""
def __init__(self, message: str, original_error: Optional[Exception] = None):
"""Initialize the LLM error.
Args:
message: The error message to display
original_error: The original exception that caused this error, if any
"""
super().__init__(message)
self.original_error = original_error