Skip to content

Commit 7d4c7aa

Browse files
committed
fix(tasks): rename json property to json_output to avoid BaseModel method conflict
The TaskOutput and CrewOutput classes inherit from Pydantic's BaseModel, which provides a built-in .json() method. Both classes defined a @Property named 'json' which shadowed BaseModel.json(), causing serialization conflicts. This renames the property to 'json_output' and updates all references. Closes the TODO comment in task_output.py that acknowledged this issue.
1 parent 7accafb commit 7d4c7aa

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

lib/crewai/src/crewai/crews/crew_output.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ def usage_metrics(self) -> dict[str, Any]:
5757
return self.token_usage.model_dump()
5858

5959
@property
60-
def json(self) -> str | None: # type: ignore[override]
60+
def json_output(self) -> str | None:
61+
"""Get the JSON string representation of the crew output.
62+
63+
Returns:
64+
JSON string representation of the crew output.
65+
66+
Raises:
67+
ValueError: If the final task output format is not JSON.
68+
"""
6169
if self.tasks_output[-1].output_format != OutputFormat.JSON:
6270
raise ValueError(
6371
"No JSON output found in the final task. Please make sure to set the output_json property in the final task in your crew."

lib/crewai/src/crewai/tasks/task_output.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,14 @@ def set_summary(self) -> TaskOutput:
7272
return self
7373

7474
@property
75-
def json(self) -> str | None: # type: ignore[override]
75+
def json_output(self) -> str | None:
7676
"""Get the JSON string representation of the task output.
7777
7878
Returns:
7979
JSON string representation of the task output.
8080
8181
Raises:
8282
ValueError: If output format is not JSON.
83-
84-
Notes:
85-
TODO: Refactor to use model_dump_json() to avoid BaseModel method conflict
8683
"""
8784
if self.output_format != OutputFormat.JSON:
8885
raise ValueError(

lib/crewai/tests/test_task.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class ScoreOutput(BaseModel):
368368

369369
crew = Crew(agents=[scorer], tasks=[task], process=Process.sequential)
370370
result = crew.kickoff()
371-
assert '{"score": 4}' == result.json
371+
assert '{"score": 4}' == result.json_output
372372
assert result.to_dict() == {"score": 4}
373373

374374
if os.path.exists(output_file):
@@ -401,7 +401,7 @@ class ScoreOutput(BaseModel):
401401
manager_llm="gpt-4o",
402402
)
403403
result = crew.kickoff()
404-
assert result.json == '{"score": 4}'
404+
assert result.json_output == '{"score": 4}'
405405
assert result.to_dict() == {"score": 4}
406406

407407

@@ -482,7 +482,7 @@ def test_no_inject_date():
482482

483483

484484
@pytest.mark.vcr()
485-
def test_json_property_without_output_json():
485+
def test_json_output_property_without_output_json():
486486
class ScoreOutput(BaseModel):
487487
score: int
488488

@@ -504,7 +504,7 @@ class ScoreOutput(BaseModel):
504504
result = crew.kickoff()
505505

506506
with pytest.raises(ValueError) as excinfo:
507-
_ = result.json # Attempt to access the json property
507+
_ = result.json_output # Attempt to access the json_output property
508508

509509
assert "No JSON output found in the final task." in str(excinfo.value)
510510

@@ -630,7 +630,7 @@ class ScoreOutput(BaseModel):
630630

631631
crew = Crew(agents=[scorer], tasks=[task1, task2])
632632
result = crew.kickoff()
633-
assert '{"score": 3}' == result.json
633+
assert '{"score": 3}' == result.json_output
634634

635635

636636
@pytest.mark.vcr()

0 commit comments

Comments
 (0)