fix: save post-guardrail output to output_file instead of stale pre-guardrail result#4543
Open
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
Open
fix: save post-guardrail output to output_file instead of stale pre-guardrail result#4543hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
hztBUAA wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
… 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.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a bug where
output_filealways 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), theoutput_filesave logic used stale local variables (json_output,pydantic_output,result) that were explicitly set toNone/raw-string when guardrails are present (lines 687-688). After guardrails process and updatetask_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_outputattributes (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 whattask.outputand theTaskCompletedEventreceive.Changes
lib/crewai/src/crewai/task.py: Usetask_output.json_dict,task_output.pydantic, andtask_output.rawforoutput_filesave in both_execute_coreand_aexecute_corelib/crewai/tests/test_task_guardrails.py: Add 3 tests verifyingoutput_filecontent correctness with and without guardrailsTest 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 listtest_output_file_saves_without_guardrails- verifies no regression without guardrailsoutput_filetests intest_task.pypass