fix: prevent LLM observation hallucination by properly attributing tool results#4182
Closed
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Closed
Conversation
…ol results Fixes #4181 The issue was that tool observations were being appended to the assistant message in the conversation history, which caused the LLM to learn to hallucinate fake observations during tool calls. Changes: - Add llm_response field to AgentAction to store the original LLM response before observation is appended - Modify handle_agent_action_core to store llm_response before appending observation to text (text still contains observation for logging) - Update CrewAgentExecutor._invoke_loop and _ainvoke_loop to: - Append LLM response as assistant message - Append observation as user message (not assistant) - Apply same fix to LiteAgent._invoke_loop - Apply same fix to CrewAgentExecutorFlow.execute_tool_action - Fix add_image_tool special case in both executors to use same pattern - Add comprehensive tests for proper message attribution Co-Authored-By: João <joao@crewai.com>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Contributor
Author
|
Closing due to inactivity for more than 7 days. Configure here. |
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.
fix: prevent LLM observation hallucination by properly attributing tool results
Summary
Fixes #4181
When agents use tools, the tool's observation was being appended to the assistant message in conversation history. This caused the LLM to learn that it should generate "Observation:" content itself, leading to hallucinated tool outputs during flow streaming.
The fix: Separate the LLM's actual response from the tool observation in message history:
Changes:
llm_responsefield toAgentActionto preserve the original LLM responsehandle_agent_action_coreto storellm_responsebefore appending observationCrewAgentExecutor,LiteAgent,CrewAgentExecutorFlow) to use proper message attributionadd_image_toolspecial case in bothCrewAgentExecutorandCrewAgentExecutorFlowReview & Testing Checklist for Human
This is a behavioral change to how conversation history is structured. Please verify:
add_image_toolwas modified - verify image tools still work correctlyRecommended test plan:
Notes
textfield onAgentActionstill contains the observation (for logging/tracing purposes), butllm_responsecontains the clean LLM outputLink to Devin run: https://app.devin.ai/sessions/344d8b0e09a0493981fc25eeb2285771
Requested by: João