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

View File

@@ -15,8 +15,8 @@ dev = [
"pytest>=8.4.2",
"pytest-asyncio>=1.2.0",
"pytest-subprocess>=1.5.3",
"vcrpy==7.0.0", # pinned, less versions break pytest-recording
"pytest-recording>=0.13.4",
"vcrpy==7.0.0",
"pytest-vcr>=1.0.2",
"pytest-randomly>=4.0.1",
"pytest-timeout>=2.4.0",
"pytest-xdist>=3.8.0",