#21832·ai

@ai-sdk/anthropic: skipped toolset tool call still sends its tool_result, causing a 400 "unexpected tool_use_id"

Author: rachel-paceCreated Sep 30, 2026Updated Sep 30, 2026
Labelstask-identify-issue-type-donetask-bug-reproduction-successfactory-activefactory-automatictask-identify-harness-labels-done

convertToAnthropicPrompt skips an assistant toolset tool call when its input has no string action. It only logs the warning toolset tool call for tool <name> is missing the action. The matching tool_result in the next tool message is still sent, so Anthropic rejects the request:

messages.N.content.0: unexpected tool_use_id found in tool_result blocks

This happens in practice whenever the model sends a toolset call that fails input validation, for example action: null, or JSON that doesn't parse. ai records the invalid call along with an error tool result. The next step, and every retry built from that history, then fails with the same non-retryable 400, so the agent can't recover.

Versions: @ai-sdk/anthropic 4.0.68 and 4.0.69, ai 7.0.122

Repro:

typescript
import { createAnthropic } from "@ai-sdk/anthropic";
import { generateText, type ModelMessage } from "ai";

const anthropic = createAnthropic({
  apiKey: "test",
  fetch: async (_url, init) => {
    const body = JSON.parse(init!.body as string);
    const parts = body.messages.flatMap((m: any) => m.content);
    const ids = new Set(parts.filter((p: any) => p.type === "tool_use").map((p: any) => p.id));
    const orphans = parts.filter((p: any) => p.type === "tool_result" && !ids.has(p.tool_use_id));
    console.log("orphaned tool_result blocks:", orphans.length); // prints 1
    return Response.json({
      id: "msg", type: "message", role: "assistant", model: "claude-opus-5-5",
      content: [{ type: "text", text: "ok" }], stop_reason: "end_turn",
      stop_sequence: null, usage: { input_tokens: 1, output_tokens: 1 },
    });
  },
});

const messages = [
  { role: "user", content: "go" },
  { role: "assistant", content: [{ type: "tool-call", toolCallId: "T1", toolName: "computer",
      input: { action: null, actions: [{ action: "mouse_move", coordinate: [1, 2] }] } }] },
  { role: "tool", content: [{ type: "tool-result", toolCallId: "T1", toolName: "computer",
      output: { type: "error-text", value: "Invalid input for tool computer: …" } }] },
] as ModelMessage[];

await generateText({
  model: anthropic("claude-opus-5-5"),
  messages,
  tools: { computer: anthropic.tools.computerToolset_20260801({ execute: async () => "ran" }) },
});

Expected: the call and its result are both kept or both dropped. Two possible fixes:

  • When a toolset tool_use is skipped, also skip the matching tool_result. It could be replaced with a text block carrying the error, so the model still learns the call failed.
  • Or send the invalid call as a normal tool_use with the raw input, as the code already does for non-toolset tools, so the model sees what it sent.

Workaround: we use a transformParams middleware that removes both halves and keeps the error as user text.