Skip to content

Comments

fix: save post-guardrail output to output_file instead of stale pre-guardrail result#4543

Open
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
hztBUAA:fix/output-file-guardrail-stale-vars
Open

fix: save post-guardrail output to output_file instead of stale pre-guardrail result#4543
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
hztBUAA:fix/output-file-guardrail-stale-vars

Conversation

@hztBUAA
Copy link

@hztBUAA hztBUAA commented Feb 20, 2026

Summary

Fixes a bug where output_file always receives the raw pre-guardrail result instead of the final post-guardrail output when guardrails are present.

Root cause: In both _execute_core (sync) and _aexecute_core (async), the output_file save logic used stale local variables (json_output, pydantic_output, result) that were explicitly set to None/raw-string when guardrails are present (lines 687-688). After guardrails process and update task_output, the file save still referenced these stale locals, writing the original raw result instead of the guardrail-transformed output.

Fix: Replace local variable references with task_output attributes (task_output.json_dict, task_output.pydantic, task_output.raw) in both sync and async code paths. This ensures the file always reflects the final post-guardrail result, consistent with what task.output and the TaskCompletedEvent receive.

Changes

  • lib/crewai/src/crewai/task.py: Use task_output.json_dict, task_output.pydantic, and task_output.raw for output_file save in both _execute_core and _aexecute_core
  • lib/crewai/tests/test_task_guardrails.py: Add 3 tests verifying output_file content correctness with and without guardrails

Test plan

  • test_output_file_saves_guardrail_result_not_stale_raw - verifies guardrail-transformed text is saved (single guardrail)
  • test_output_file_saves_guardrail_result_with_guardrails_list - verifies with guardrails list
  • test_output_file_saves_without_guardrails - verifies no regression without guardrails
  • All 21 existing guardrail tests pass
  • Existing output_file tests in test_task.py pass

… local variables

When guardrails are present, _execute_core and _aexecute_core set
pydantic_output and json_output to None before guardrail processing.
After guardrails update task_output with transformed results, the
output_file save still used the stale local variables, causing the
file to always contain the raw pre-guardrail result instead of the
final post-guardrail structured output.

Replace references to local variables (json_output, pydantic_output,
result) with task_output attributes (task_output.json_dict,
task_output.pydantic, task_output.raw) in both sync and async paths.
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant