Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot commented Dec 29, 2025

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_core and _aexecute_core methods used the original variables (json_output, pydantic_output, result) instead of the updated task_output object after guardrails executed and potentially modified the output.

The fix changes the file save logic to use task_output.json_dict, task_output.pydantic, and task_output.raw which contain the correct post-guardrail values.

Review & Testing Checklist for Human

  • Verify async execution path works correctly - The tests only cover sync execution (execute_sync), but the same fix was applied to _aexecute_core. Consider testing with async execution to ensure the fix works there too.
  • End-to-end test with real agent - The new tests use mocks. Verify the fix works in a real scenario where an agent executes a task with guardrails and an output file.
  • Check _save_file handles dict types - When task_output.json_dict is used, it's a dict. Verify _save_file serializes it correctly.

Notes

  • 5 new tests added covering: raw output, JSON output, pydantic output, single guardrail, and multiple chained guardrails
  • All 26 guardrail tests pass locally
  • Lint checks pass

Link to Devin run: https://app.devin.ai/sessions/dc1209f2778f4b72b9aad8e9bdebfff7
Requested by: João (joao@crewai.com)


Note

Fixes output persistence after guardrails

  • Update crewai/task.py in both _execute_core and _aexecute_core to save output_file from task_output.json_dict → else task_output.pydantic.model_dump_json() → else task_output.raw, ensuring the post-guardrail result is persisted
  • Add tests in tests/test_task_guardrails.py verifying output_file contains the final guardrail-modified output for: raw, JSON (with model), Pydantic, single guardrail, and chained guardrails

Written by Cursor Bugbot for commit 1c59ff8. This will update automatically on new commits. Configure here.

The output file was being written with pre-guardrail output instead of
post-guardrail output. This was because the file save logic used the
original variables (json_output, pydantic_output, result) instead of
the updated task_output object after guardrails executed.

Fixed by using task_output.json_dict, task_output.pydantic, and
task_output.raw in the file save logic for both sync (_execute_core)
and async (_aexecute_core) execution paths.

Added 5 tests to verify output file contains post-guardrail results:
- test_output_file_contains_guardrail_modified_raw_result
- test_output_file_contains_guardrail_modified_json_result
- test_output_file_contains_guardrail_modified_pydantic_result
- test_output_file_with_single_guardrail_modification
- test_output_file_with_multiple_guardrails_chained_modifications

Fixes #4156

Co-Authored-By: João <joao@crewai.com>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant