Fix agent output sanitization to prevent internal ReAct fields from leaking

Fixes #3873

This commit addresses a bug where internal ReAct-style fields like 'Thought:',
'Action:', 'Action Input:', and 'Observation:' were leaking into the final
agent output, particularly in hierarchical crews when delegated tasks failed
or when agents hit max iterations.

Changes:
- Added sanitize_react_output() utility function to strip internal ReAct fields
- Applied sanitization in handle_max_iterations_exceeded() when converting
  AgentAction to AgentFinish
- Applied sanitization in format_answer() exception handling to prevent leaks
  when LLM responses cannot be parsed
- Added comprehensive unit tests to verify the fix and prevent regressions
- Updated pyproject.toml to use pytest-vcr instead of pytest-recording to
  resolve plugin conflicts

The sanitization function removes lines starting with ReAct field markers
while preserving legitimate content, ensuring clean user-facing outputs.

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-11-10 11:32:48 +00:00
parent 0f1c173d02
commit efb005afaf
4 changed files with 4288 additions and 4015 deletions

8086
uv.lock generated

File diff suppressed because it is too large Load Diff