fix: deduplicate observations with same tool_call_id#2114
Draft
fix: deduplicate observations with same tool_call_id#2114
Conversation
Fixes Anthropic API error: "each tool_use must have a single result. Found multiple tool_result blocks with id: <tool_call_id>" Root cause: When a server restart occurs while a tool is in progress, the EventService creates an AgentErrorEvent for unmatched actions. If the tool then completes, an ObservationEvent is also recorded. Both events share the same tool_call_id, causing duplicate tool_result blocks when the events are converted to LLM messages. The fix adds a _deduplicate_observations() method to View that: 1. Identifies the best observation for each tool_call_id based on priority (ObservationEvent > UserRejectObservation > AgentErrorEvent) 2. Emits only one observation per tool_call_id, preserving event order This ensures that when both an error event and a successful observation exist for the same tool call (e.g., after a restart), only the most informative one (typically the ObservationEvent with actual results) is sent to the LLM. Co-authored-by: openhands <openhands@all-hands.dev>
Contributor
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.
Summary
Fixes Anthropic API error:
"each tool_use must have a single result. Found multiple tool_result blocks with id: <tool_call_id>"Root Cause Analysis
Investigation of trajectory fcbbaec269bc4903ad4027c3bef0bc57 revealed a complex interaction:
What Happened
examples/02_remote_agent_server/01_convo_with_local_agent_server.py)execution_status = RUNNINGAgentErrorEventfor the "interrupted" actionObservationEventtool_call_idTimeline from Events
The log output in Event 65 shows:
This confirms the nested server resumed the same conversation that was actively running.
Why This Causes the API Error
When the events are converted to LLM messages via
events_to_messages(), both AgentErrorEvent and ObservationEvent get converted totool_resultblocks with the same ID. The Anthropic API rejects this:Solution
Added a
_deduplicate_observations()method to theViewclass that:tool_call_idbased on priority:ObservationEvent(highest - contains actual tool result)UserRejectObservation(user action)AgentErrorEvent(lowest - error notification)tool_call_id, preserving event orderThis is a defensive fix at the View level. The event_service cannot prevent this because:
Alternative Considerations
Other potential fixes (not implemented, for future consideration):
AgentErrorEventinget_unmatched_actions()to prevent duplicate error eventsTesting
test_view_duplicate_observations.py