Fix Anthropic API rejection when resubmitting aborted thinking blocks

- Convert thinking blocks with missing/empty signatures to text blocks
- Prevents 400 error: 'Invalid signature in thinking block'
- Occurs when stream is aborted mid-thinking and message is resubmitted
This commit is contained in:
Mario Zechner 2025-11-18 14:36:57 +01:00
parent 6680034a64
commit 387cc97bac
2 changed files with 18 additions and 5 deletions

View file

@ -460,11 +460,20 @@ function convertMessages(messages: Message[], model: Model<"anthropic-messages">
});
} else if (block.type === "thinking") {
if (block.thinking.trim().length === 0) continue;
blocks.push({
type: "thinking",
thinking: sanitizeSurrogates(block.thinking),
signature: block.thinkingSignature || "",
});
// If thinking signature is missing/empty (e.g., from aborted stream),
// convert to text block to avoid API rejection
if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
blocks.push({
type: "text",
text: sanitizeSurrogates(`<thinking>\n${block.thinking}\n</thinking>`),
});
} else {
blocks.push({
type: "thinking",
thinking: sanitizeSurrogates(block.thinking),
signature: block.thinkingSignature,
});
}
} else if (block.type === "toolCall") {
blocks.push({
type: "tool_use",

View file

@ -2,6 +2,10 @@
## [Unreleased]
### Fixed
- **Anthropic Aborted Thinking**: Fixed error when resubmitting assistant messages with incomplete thinking blocks (from aborted streams). Thinking blocks without valid signatures are now converted to text blocks with `<thinking>` delimiters, preventing API rejection.
## [0.7.16] - 2025-11-17
### Fixed