Skip to content

fix: strip hallucinated <tool_result> blocks from ReACT message history#530

Open
octo-patch wants to merge 1 commit into666ghj:mainfrom
octo-patch:fix/issue-529-strip-fake-tool-results
Open

fix: strip hallucinated <tool_result> blocks from ReACT message history#530
octo-patch wants to merge 1 commit into666ghj:mainfrom
octo-patch:fix/issue-529-strip-fake-tool-results

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #529

Problem

The ReACT section generation loop in report_agent.py appended the raw LLM response to message history including any <tool_result> blocks the LLM may have fabricated in its own reply. This caused subsequent iterations to treat invented data as authoritative, producing reports with completely fabricated usernames, quotes, and statistics.

Solution

Added a _strip_fake_tool_results() static method to ReportAgent that removes any <tool_result>...</tool_result> blocks from the LLM response before it is recorded in message history:

@staticmethod
def _strip_fake_tool_results(response: str) -> str:
    cleaned = re.sub(r'<tool_result>.*?</tool_result>', '', response, flags=re.DOTALL)
    cleaned = re.sub(r'\n{3,}', '\n\n', cleaned)
    return cleaned.strip()

The call site in the tool execution path (previously line ~1456) now passes the cleaned response to messages.append() instead of the raw response, while the real tool result is still injected via REACT_OBSERVATION_TEMPLATE as before.

Testing

  • The static method can be unit-tested by passing a string containing fabricated <tool_result> blocks and verifying they are removed while the <tool_call> block is preserved.
  • Manually verified that the regex correctly strips only <tool_result> blocks and collapses resulting extra blank lines.

…ry (fixes 666ghj#529)

LLMs sometimes fabricate <tool_result> blocks inside their own responses,
polluting the message history with invented data that subsequent iterations
treat as authoritative. This adds _strip_fake_tool_results() which removes
any such blocks before the assistant response is appended to history.
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: ReACT report generation hallucinates entities via self-fabricated <tool_result> blocks

1 participant