Fix issue #2487: Ensure LLM errors are properly raised in async context

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-27 09:28:27 +00:00
parent e1a73e0c44
commit 7907c8a147
2 changed files with 35 additions and 1 deletions

View File

@@ -684,7 +684,10 @@ class Crew(BaseModel):
async def kickoff_async(self, inputs: Optional[Dict[str, Any]] = {}) -> CrewOutput:
"""Asynchronous kickoff method to start the crew execution."""
return await asyncio.to_thread(self.kickoff, inputs)
try:
return await asyncio.to_thread(self.kickoff, inputs)
except Exception as e:
raise
async def kickoff_for_each_async(self, inputs: List[Dict]) -> List[CrewOutput]:
crew_copies = [self.copy() for _ in inputs]

View File

@@ -1499,6 +1499,37 @@ async def test_async_kickoff_for_each_async_empty_input():
# Assertion
assert results == [], "Result should be an empty list when input is empty"
@pytest.mark.asyncio
async def test_kickoff_async_error_handling():
"""Tests error handling in kickoff_async when kickoff raises an error."""
from unittest.mock import patch
inputs = {"topic": "dog"}
agent = Agent(
role="{topic} Researcher",
goal="Express hot takes on {topic}.",
backstory="You have a lot of experience with {topic}.",
)
task = Task(
description="Give me an analysis around {topic}.",
expected_output="1 bullet point about {topic} that's under 15 words.",
agent=agent,
)
# Create the crew
crew = Crew(
agents=[agent],
tasks=[task],
)
with patch.object(Crew, "kickoff", side_effect=Exception("Simulated LLM error")) as mock_kickoff:
with pytest.raises(Exception, match="Simulated LLM error"):
await crew.kickoff_async(inputs)
mock_kickoff.assert_called_once_with(inputs)
def test_set_agents_step_callback():