openclaw - 💡(How to fix) Fix Bug: Orphaned tool_result after API error mid-tool-call corrupts session permanently [4 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#50059Fetched 2026-04-08 00:59:42
View on GitHub
Comments
4
Participants
2
Timeline
4
Reactions
0
Timeline (top)
commented ×2mentioned ×1subscribed ×1

When the Anthropic API returns an error (e.g., overloaded_error) mid-tool-call, the session becomes permanently corrupted with an orphaned tool_result that references a malformed tool_use block.

Error Message

Bug: Orphaned tool_result after API error mid-tool-call corrupts session permanently

When the Anthropic API returns an error (e.g., overloaded_error) mid-tool-call, the session becomes permanently corrupted with an orphaned tool_result that references a malformed tool_use block. 3. Assistant message is saved with stopReason: "error" and an incomplete tool_use block containing partialJson // Skip incomplete tool calls (truncated by API error mid-stream)

Affected Error Types

| Error Type | Can Trigger Bug? |

Root Cause

In session-tool-result-guard.js and session-transcript-repair.js, the extractAssistantToolCalls() / extractToolCallsFromAssistant() functions extract tool calls based only on:

  • rec.type === "toolCall" (or toolUse/functionCall)
  • rec.id being a valid string

They do NOT check for the partialJson field, which indicates the tool call was truncated mid-stream and is incomplete/malformed.

Fix Action

Workaround

For already-corrupted sessions, move the session file:

mv ~/.clawdbot/agents/main/sessions/SESSION_ID.jsonl ~/.clawdbot/agents/main/sessions/SESSION_ID-BROKEN.jsonl

Code Example

messages.X.content.Y: unexpected tool_use_id found in tool_result blocks: toolu_XXXX
   Each tool_result block must have a corresponding tool_use block in the previous message.

---

if (rec.type === "toolCall" || rec.type === "toolUse" || rec.type === "functionCall") {
    // Skip incomplete tool calls (truncated by API error mid-stream)
    if (rec.partialJson !== undefined) {
        continue;
    }
    toolCalls.push({
        id: rec.id,
        name: typeof rec.name === "string" ? rec.name : undefined,
    });
}

---

mv ~/.clawdbot/agents/main/sessions/SESSION_ID.jsonl ~/.clawdbot/agents/main/sessions/SESSION_ID-BROKEN.jsonl
RAW_BUFFERClick to expand / collapse

Bug: Orphaned tool_result after API error mid-tool-call corrupts session permanently

Description

When the Anthropic API returns an error (e.g., overloaded_error) mid-tool-call, the session becomes permanently corrupted with an orphaned tool_result that references a malformed tool_use block.

Reproduction

  1. Start a tool call that streams a response
  2. API returns overloaded_error (or api_error, timeout, etc.) mid-stream
  3. Assistant message is saved with stopReason: "error" and an incomplete tool_use block containing partialJson
  4. session-tool-result-guard.js detects the pending tool call ID and inserts a synthetic tool_result
  5. Every subsequent API request fails with:
    messages.X.content.Y: unexpected tool_use_id found in tool_result blocks: toolu_XXXX
    Each tool_result block must have a corresponding tool_use block in the previous message.

Root Cause

In session-tool-result-guard.js and session-transcript-repair.js, the extractAssistantToolCalls() / extractToolCallsFromAssistant() functions extract tool calls based only on:

  • rec.type === "toolCall" (or toolUse/functionCall)
  • rec.id being a valid string

They do NOT check for the partialJson field, which indicates the tool call was truncated mid-stream and is incomplete/malformed.

The Fix

Add a check to skip tool calls with partialJson:

if (rec.type === "toolCall" || rec.type === "toolUse" || rec.type === "functionCall") {
    // Skip incomplete tool calls (truncated by API error mid-stream)
    if (rec.partialJson !== undefined) {
        continue;
    }
    toolCalls.push({
        id: rec.id,
        name: typeof rec.name === "string" ? rec.name : undefined,
    });
}

Apply to both:

  • dist/agents/session-tool-result-guard.js (line ~13-20)
  • dist/agents/session-transcript-repair.js (line ~12-17)

Affected Error Types

Error TypeCan Trigger Bug?
overloaded_error✅ Yes
api_error (500)✅ Yes
Client timeout/abort✅ Yes
rate_limit_error⚠️ Possible if stream started
context_length_exceeded❌ No (fails before response)

Workaround

For already-corrupted sessions, move the session file:

mv ~/.clawdbot/agents/main/sessions/SESSION_ID.jsonl ~/.clawdbot/agents/main/sessions/SESSION_ID-BROKEN.jsonl

Environment

  • clawdbot version: 2026.1.24-3+
  • Provider: Anthropic
  • Model: claude-opus-4-5

extent analysis

Fix Plan

To resolve the issue of orphaned tool_result after an API error mid-tool-call, apply the following steps:

  • Update the extractAssistantToolCalls() and extractToolCallsFromAssistant() functions in both session-tool-result-guard.js and session-transcript-repair.js to skip tool calls with partialJson.
  • Modify the code as follows:
if (rec.type === "toolCall" || rec.type === "toolUse" || rec.type === "functionCall") {
    // Skip incomplete tool calls (truncated by API error mid-stream)
    if (rec.partialJson !== undefined) {
        continue;
    }
    toolCalls.push({
        id: rec.id,
        name: typeof rec.name === "string" ? rec.name : undefined,
    });
}
  • Apply this change to both files:
    • dist/agents/session-tool-result-guard.js (line ~13-20)
    • dist/agents/session-transcript-repair.js (line ~12-17)

Verification

To verify the fix, test the following scenarios:

  • Start a tool call that streams a response
  • Simulate an API error (e.g., overloaded_error) mid-stream
  • Check that the session is not corrupted and subsequent API requests do not fail with the messages.X.content.Y: unexpected tool_use_id found in tool_result blocks error

Extra Tips

For already-corrupted sessions, consider moving the session file as a temporary workaround:

mv ~/.clawdbot/agents/main/sessions/SESSION_ID.jsonl ~/.clawdbot/agents/main/sessions/SESSION_ID-BROKEN.jsonl

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix Bug: Orphaned tool_result after API error mid-tool-call corrupts session permanently [4 comments, 2 participants]