diff --git a/lib/crewai/src/crewai/crews/crew_output.py b/lib/crewai/src/crewai/crews/crew_output.py index 20b9686a0b..655bfa5dd9 100644 --- a/lib/crewai/src/crewai/crews/crew_output.py +++ b/lib/crewai/src/crewai/crews/crew_output.py @@ -62,7 +62,10 @@ def json(self) -> str | None: # type: ignore[override] None when that output could not be converted to its ``output_json`` model. """ - if self.tasks_output[-1].output_format != OutputFormat.JSON: + if ( + not self.tasks_output + or 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." ) diff --git a/lib/crewai/tests/test_crew_output.py b/lib/crewai/tests/test_crew_output.py new file mode 100644 index 0000000000..3345964a6b --- /dev/null +++ b/lib/crewai/tests/test_crew_output.py @@ -0,0 +1,12 @@ +import pytest + +from crewai.crews.crew_output import CrewOutput + + +def test_json_raises_value_error_when_tasks_output_is_empty(): + """A CrewOutput built outside a kickoff (empty tasks_output) must raise the + guided ValueError instead of a bare IndexError.""" + output = CrewOutput(raw="no tasks") + + with pytest.raises(ValueError, match="No JSON output"): + _ = output.json