Compare commits

...

2 Commits

Author SHA1 Message Date
Devin AI
482c7e5318 Fix lint error: use underscore for unused variable in test
- Replace json_output with _ to indicate intentionally unused variable
- Fixes F841 lint error in test_crew_output_json_reproduction_case

Co-Authored-By: Jo\u00E3o <joao@crewai.com>
2025-07-18 16:35:31 +00:00
Devin AI
5896f6a119 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>
2025-07-18 16:32:34 +00:00
2 changed files with 40 additions and 0 deletions

View File

@@ -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."

View File

@@ -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:
_ = 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