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
This commit is contained in:
David Brailovsky 2026-10-13 15:44:49 +02:00
parent cea257439d
commit 34a8bb38b2
2 changed files with 11 additions and 1 deletions

View file

@ -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

View file

@ -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,
};
}