[Bug]: TaskOutput.pydantic is None inside task guardrails when output_pydantic is configured
Summary of the Bug
When configuring a task with output_pydantic (or output_json) and a guardrail, task_output.pydantic is unconditionally None inside the guardrail function on the initial execution attempt. If a guardrail attempts to inspect attributes of the structured Pydantic model (e.g., task_output.pydantic.score), it crashes with an AttributeError.
Root Cause
In lib/crewai/src/crewai/task.py (_execute_core and _aexecute_core), output conversion was skipped when guardrails were present: \\python elif not self._guardrails and not self._guardrail: raw = result pydantic_output, json_output = self._export_output(result) else: raw = result pydantic_output, json_output = None, None \\ This leaves task_output.pydantic as None during guardrail validation, and remains None when a guardrail returns a TaskOutput instance.
Proposed Fix & PR
We have authored a PR with unit test coverage to export structured outputs before guardrail invocation and hydrate TaskOutput instances.
Confirmed on main (435262c): _execute_core and _aexecute_core skip _export_output when a guardrail is configured, so task_output.pydantic is None inside the guardrail - a guardrail reading task_output.pydantic.score crashes. The fix exports the structured output unconditionally before validation; the guardrail path itself already re-exports when a guardrail transforms the raw string. PR follows with unit tests covering guardrail and guardrails.
Please assign @Luckyou-d if available.
Thanks for the report — the fix here keeps the export unconditionally (both branches now call _export_output / _aexport_output), so .pydantic/.json_dict are populated inside the guardrail on the first attempt, and the guardrail path's own re-export keeps the format after a transform. Your PR was auto-closed by the first-contributor bot before I saw it, so I've opened #9533 with unit tests; if your version lands instead, no problem — happy to close this one.
Please assign @Luckyou-d if available.
Thanks @Luckyou-d! Just to clarify, the initial PR (#9494) was closed by the title check bot, but I reopened and updated it in #9496: https://github.com/crewAIInc/crewAI/pull/9496
In addition to exporting structured output prior to guardrails, #9496 also:
- Wraps the initial export in exception guards so malformed raw outputs don't abort execution prior to guardrail remediation.
- Rehydrates structured models from \ ask_output.raw\ if a guardrail modifies/redacts output (which resolved the finding in the Strix security review).
All tests and security checks are passing on #9496, ready for maintainer review.