fix: strip hallucinated <tool_result> blocks from ReACT message history#530
Open
octo-patch wants to merge 1 commit into666ghj:mainfrom
Open
fix: strip hallucinated <tool_result> blocks from ReACT message history#530octo-patch wants to merge 1 commit into666ghj:mainfrom
octo-patch wants to merge 1 commit into666ghj:mainfrom
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #529
Problem
The ReACT section generation loop in
report_agent.pyappended 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 toReportAgentthat removes any<tool_result>...</tool_result>blocks from the LLM response before it is recorded in message history: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 viaREACT_OBSERVATION_TEMPLATEas before.Testing
<tool_result>blocks and verifying they are removed while the<tool_call>block is preserved.<tool_result>blocks and collapses resulting extra blank lines.