Fix output file writes outdated task result after guardrail execution #4157
+169
−6
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 #4156 - Output file was being written with pre-guardrail output instead of post-guardrail output.
The bug occurred because the file save logic in both
_execute_coreand_aexecute_coremethods used the original variables (json_output,pydantic_output,result) instead of the updatedtask_outputobject after guardrails executed and potentially modified the output.The fix changes the file save logic to use
task_output.json_dict,task_output.pydantic, andtask_output.rawwhich contain the correct post-guardrail values.Review & Testing Checklist for Human
execute_sync), but the same fix was applied to_aexecute_core. Consider testing with async execution to ensure the fix works there too._save_filehandles dict types - Whentask_output.json_dictis used, it's a dict. Verify_save_fileserializes it correctly.Notes
Link to Devin run: https://app.devin.ai/sessions/dc1209f2778f4b72b9aad8e9bdebfff7
Requested by: João (joao@crewai.com)
Note
Fixes output persistence after guardrails
crewai/task.pyin both_execute_coreand_aexecute_coreto saveoutput_filefromtask_output.json_dict→ elsetask_output.pydantic.model_dump_json()→ elsetask_output.raw, ensuring the post-guardrail result is persistedtests/test_task_guardrails.pyverifyingoutput_filecontains the final guardrail-modified output for: raw, JSON (with model), Pydantic, single guardrail, and chained guardrailsWritten by Cursor Bugbot for commit 1c59ff8. This will update automatically on new commits. Configure here.