-
-
Notifications
You must be signed in to change notification settings - Fork 55.7k
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Summary
When an assistant message has stopReason: "aborted" (in addition to "error"), the repairToolUseResultPairing() function incorrectly creates synthetic tool_result entries for incomplete tool calls. This causes all subsequent API requests to fail with:
unexpected tool_use_id found in tool_result blocks: toolu_XXXXX.
Each tool_result block must have a corresponding tool_use block in the previous message.
Root Cause
When a request is aborted mid-stream:
- The assistant message is saved with
stopReason: "aborted"and incompletetool_useblocks (often withpartialJson: true) repairToolUseResultPairing()sees atool_usewithout a matchingtool_result- It creates a synthetic
tool_resultto "fix" the pairing - During API serialization, the incomplete
tool_usemay be filtered out - Result: orphaned
tool_result→ API rejects with 400 error
Evidence
A corrupted session had the following stopReason distribution:
1 "stopReason":"aborted" <-- caused the corruption
3 "stopReason":"error"
121 "stopReason":"stop"
521 "stopReason":"toolUse"
The single "aborted" message corrupted the entire session.
Proposed Fix
Skip tool call extraction for assistant messages with stopReason: "error" OR stopReason: "aborted":
const stopReason = (assistant as { stopReason?: string }).stopReason;
if (stopReason === "error" || stopReason === "aborted") {
out.push(msg);
continue;
}Related Issues
- Bug: Session tree branching causes tool_use_id mismatch error (LLM request rejected) #4553 - Session tree branching causes tool_use_id mismatch (related but different root cause)
- Synthetic tool_result for terminated tool_use causes all subsequent requests to fail #1826 - Same underlying issue
- fix(agents): skip extracting tool calls from errored assistant turns #1859 - Similar fix but only handles
"error", not"aborted" - [Bug] Anthropic API Error: Unexpected
tool_use_idintool_resultblocks anthropics/claude-code#11064 - Same issue in claude-code
Impact
- Without fix: Any aborted request during a tool call permanently corrupts the session
- With fix: Sessions remain usable after interruptions
I have a PR ready with the fix and tests.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working