From 34a8bb38b2592c61bbae69413c89b720366b533c Mon Sep 17 00:00:00 2001 From: David Brailovsky Date: Tue, 13 Oct 2026 15:44:49 +0200 Subject: [PATCH] fix(ai): drop images from Anthropic error tool results Anthropic rejects error tool results whose content is not all text, which stalled sessions after a failed codemode script that had shown images. Images in error tool results are now replaced with a text note. fixes #10794 --- packages/ai/CHANGELOG.md | 1 + packages/ai/src/api/anthropic-messages.ts | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) 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, }; }