diff --git a/packages/ai/CHANGELOG.md b/packages/ai/CHANGELOG.md index a49246fa3..cf73d98b7 100644 --- a/packages/ai/CHANGELOG.md +++ b/packages/ai/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixed - Fixed tool-call argument parsing dropping fields when a string contains an invalid `\u` escape such as `\usepackage` ([#10868](https://github.com/earendil-works/pi/issues/10868)) +- Fixed Anthropic requests failing with a 400 when an error tool result contains images; the images are now replaced with a text note ([#10794](https://github.com/earendil-works/pi/issues/10794)) ## [1.2.0] - 2026-10-12 diff --git a/packages/ai/src/api/anthropic-messages.ts b/packages/ai/src/api/anthropic-messages.ts index 29e13db1d..3081c7246 100644 --- a/packages/ai/src/api/anthropic-messages.ts +++ b/packages/ai/src/api/anthropic-messages.ts @@ -1328,10 +1328,19 @@ function normalizeToolCallId(id: string): string { } function convertToolResult(msg: ToolResultMessage): ContentBlockParam { + // Anthropic rejects error tool results that contain anything but text, so images are replaced + // with a note (#10794). + const content = msg.isError + ? msg.content.map((block) => + block.type === "image" + ? { type: "text" as const, text: "[image omitted: error tool results cannot contain images]" } + : block, + ) + : msg.content; return { type: "tool_result", tool_use_id: msg.toolCallId, - content: convertContentBlocks(msg.content), + content: convertContentBlocks(content), is_error: msg.isError, }; }