Skip to content

fix: strip orphaned think/reasoning tags from user-facing responses#4311

Merged
teknium1 merged 1 commit into
mainfrom
hermes/hermes-98ff0c21
Mar 31, 2026
Merged

fix: strip orphaned think/reasoning tags from user-facing responses#4311
teknium1 merged 1 commit into
mainfrom
hermes/hermes-98ff0c21

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

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():

content = re.sub(r'</?(?:think|thinking|reasoning|REASONING_SCRATCHPAD)>\s*', '', content, flags=re.IGNORECASE)

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.

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
@teknium1 teknium1 merged commit 0240baa into main Mar 31, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Orphaned </think> closing tags leak into user-facing responses

2 participants