From 01b84379409574e4bd8f292bd693bfd50375d77b Mon Sep 17 00:00:00 2001 From: Greyson LaLonde Date: Tue, 21 Apr 2026 01:59:42 +0800 Subject: [PATCH] fix: handle BaseModel result in guardrail retry loop The guardrail retry path passed a Pydantic object directly to TaskOutput.raw (which expects a string), causing a ValidationError when output_pydantic is set and a guardrail fails. Mirror the BaseModel check from the initial execution path into both sync and async retry loops. Closes #5544 (part 1) --- lib/crewai/src/crewai/task.py | 36 +++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) 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,