mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-07-25 00:35:09 +00:00
Fix IndexError in CrewOutput.json when tasks_output is empty
- Add validation to check if tasks_output is empty before accessing [-1] - Provide meaningful error message for empty tasks case - Add comprehensive tests covering edge cases - Fixes #3185 Co-Authored-By: Jo\u00E3o <joao@crewai.com>
This commit is contained in:
@@ -25,6 +25,11 @@ class CrewOutput(BaseModel):
|
||||
|
||||
@property
|
||||
def json(self) -> Optional[str]:
|
||||
if not self.tasks_output:
|
||||
raise ValueError(
|
||||
"No tasks found in crew output. Please ensure the crew has completed at least one task before accessing JSON output."
|
||||
)
|
||||
|
||||
if self.tasks_output[-1].output_format != OutputFormat.JSON:
|
||||
raise ValueError(
|
||||
"No JSON output found in the final task. Please make sure to set the output_json property in the final task in your crew."
|
||||
|
||||
@@ -310,6 +310,41 @@ def test_crew_creation(researcher, writer):
|
||||
assert result.raw == expected_string_output
|
||||
|
||||
|
||||
def test_crew_output_json_empty_tasks():
|
||||
"""Test that CrewOutput.json raises ValueError when tasks_output is empty."""
|
||||
from crewai.crews.crew_output import CrewOutput
|
||||
from crewai.types.usage_metrics import UsageMetrics
|
||||
|
||||
output = CrewOutput(
|
||||
raw="Test output",
|
||||
tasks_output=[],
|
||||
token_usage=UsageMetrics()
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
_ = output.json
|
||||
|
||||
assert "No tasks found in crew output" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_crew_output_json_reproduction_case():
|
||||
"""Test reproduction case from GitHub issue #3185."""
|
||||
from crewai.crews.crew_output import CrewOutput
|
||||
|
||||
output = CrewOutput(
|
||||
raw="",
|
||||
pydantic=None,
|
||||
json_dict={"test": "value"},
|
||||
tasks_output=[],
|
||||
token_usage={}
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
json_output = output.json
|
||||
|
||||
assert "No tasks found in crew output" in str(excinfo.value)
|
||||
|
||||
|
||||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||||
def test_sync_task_execution(researcher, writer):
|
||||
from unittest.mock import patch
|
||||
|
||||
Reference in New Issue
Block a user