Skip to content

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

Closed
arasovic wants to merge 1 commit into
NousResearch:mainfrom
arasovic:fix/strip-orphaned-think-tags
Closed

fix: strip orphaned think/reasoning tags from user-facing responses#4286
arasovic wants to merge 1 commit into
NousResearch:mainfrom
arasovic:fix/strip-orphaned-think-tags

Conversation

@arasovic

Copy link
Copy Markdown
Contributor

Problem

Some models (e.g. Kimi K2.5 on Alibaba's OpenAI-compatible endpoint) emit reasoning as plain text followed by a closing </think> tag without a matching opening <think>. _strip_think_blocks() uses paired-tag regexes (<think>.*?</think>) that require both tags — orphaned </think> passes through unmatched and appears in user-facing responses.

Closes #4285

Changes

run_agent.py — Add one catch-all regex to _strip_think_blocks():

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

tests/test_run_agent.py — 4 new tests:

  • some reasoning</think>actual answer</think> stripped
  • reasoning</thinking>answer</thinking> stripped
  • <think>orphaned without close<think> stripped
  • stray</think><think>paired</think> visible → all tags stripped, visible kept

Test plan

  • python -m pytest tests/test_run_agent.py::TestStripThinkBlocks -q — 8 passed, 0 failed

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 NousResearch#4285
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #4311. Your commit was cherry-picked onto current main with authorship preserved. Thanks for the fix!

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