fix: strip orphaned think/reasoning tags from user-facing responses#4311
Merged
Conversation
Some models (e.g. Kimi K2.5 on Alibaba OpenAI-compatible endpoint) emit reasoning text followed by a closing </think> without a matching opening <think> tag. The existing paired-tag regexes in _strip_think_blocks() cannot match these orphaned tags, so </think> leaks into user-facing responses on all platforms. Add a catch-all regex that strips any remaining opening or closing think/thinking/reasoning/REASONING_SCRATCHPAD tags after the existing paired-block removal pass. Closes #4285
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Salvages PR #4286 by @arasovic — strips orphaned think/reasoning tags from user-facing responses.
The bug
Models like Kimi K2.5 on Alibaba's endpoint emit reasoning as plain text followed by a bare
</think>without a matching opening tag. The existing paired-tag regexes (<think>.*?</think>) require both tags, so orphaned tags pass through and appear in user responses.The fix
One catch-all regex added after the paired-tag removals in
_strip_think_blocks():Runs after paired removal so it only catches orphans. 4 new tests covering orphaned close, orphaned open, and mixed scenarios.
Testing
TestStripThinkBlocks— 8/8 pass (4 existing + 4 new)Closes #4286. Fixes #4285.