From 7907c8a14772e4bb6cab29f696d686e70699dbdb Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 27 Mar 2025 09:28:27 +0000 Subject: [PATCH] Fix issue #2487: Ensure LLM errors are properly raised in async context Co-Authored-By: Joe Moura --- src/crewai/crew.py | 5 ++++- tests/crew_test.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/crewai/crew.py b/src/crewai/crew.py index c54d50425..8c7b310df 100644 --- a/src/crewai/crew.py +++ b/src/crewai/crew.py @@ -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] diff --git a/tests/crew_test.py b/tests/crew_test.py index e5f464131..48d252e4f 100644 --- a/tests/crew_test.py +++ b/tests/crew_test.py @@ -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():