Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix: handle BaseModel result in guardrail retry loop

Greyson LaLonde01b843794095
diff --git a/lib/crewai/src/crewai/task.py b/lib/crewai/src/crewai/task.py
index e12caa2af..1cac87cb8 100644
--- a/lib/crewai/src/crewai/task.py
+++ b/lib/crewai/src/crewai/task.py
@@ -1241,12 +1241,26 @@ Follow these guidelines:
tools=tools,
)
- pydantic_output, json_output = self._export_output(result)
+ if isinstance(result, BaseModel):
+ raw = result.model_dump_json()
+ if self.output_pydantic:
+ pydantic_output = result
+ json_output = None
+ elif self.output_json:
+ pydantic_output = None
+ json_output = result.model_dump()
+ else:
+ pydantic_output = None
+ json_output = None
+ else:
+ raw = result
+ pydantic_output, json_output = self._export_output(result)
+
task_output = TaskOutput(
name=self.name or self.description,
description=self.description,
expected_output=self.expected_output,
- raw=result,
+ raw=raw,
pydantic=pydantic_output,
json_dict=json_output,
agent=agent.role,
@@ -1337,12 +1351,26 @@ Follow these guidelines:
tools=tools,
)
- pydantic_output, json_output = self._export_output(result)
+ if isinstance(result, BaseModel):
+ raw = result.model_dump_json()
+ if self.output_pydantic:
+ pydantic_output = result
+ json_output = None
+ elif self.output_json:
+ pydantic_output = None
+ json_output = result.model_dump()
+ else:
+ pydantic_output = None
+ json_output = None
+ else:
+ raw = result
+ pydantic_output, json_output = self._export_output(result)
+
task_output = TaskOutput(
name=self.name or self.description,
description=self.description,
expected_output=self.expected_output,
- raw=result,
+ raw=raw,
pydantic=pydantic_output,
json_dict=json_output,
agent=agent.role,