Fix thinking tag leakage by converting unsigned blocks to plain text

Closes #302
This commit is contained in:
Mario Zechner 2025-12-24 18:15:19 +01:00
parent 2b22c3ce75
commit 29379ea0a6
2 changed files with 9 additions and 2 deletions

View file

@ -1,5 +1,11 @@
# Changelog
## [Unreleased]
### Fixed
- **Thinking tag leakage**: Fixed Claude mimicking literal `</thinking>` tags in responses. Unsigned thinking blocks (from aborted streams) are now converted to plain text without `<thinking>` tags. The TUI still displays them as thinking blocks. ([#302](https://github.com/badlogic/pi-mono/pull/302) by [@nicobailon](https://github.com/nicobailon))
## [0.25.1] - 2025-12-21
### Added

View file

@ -463,11 +463,12 @@ function convertMessages(messages: Message[], model: Model<"anthropic-messages">
} else if (block.type === "thinking") {
if (block.thinking.trim().length === 0) continue;
// If thinking signature is missing/empty (e.g., from aborted stream),
// convert to text block to avoid API rejection
// convert to plain text block without <thinking> tags to avoid API rejection
// and prevent Claude from mimicking the tags in responses
if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
blocks.push({
type: "text",
text: sanitizeSurrogates(`<thinking>\n${block.thinking}\n</thinking>`),
text: sanitizeSurrogates(block.thinking),
});
} else {
blocks.push({