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

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
This commit is contained in:
arasovic 2026-03-31 19:42:44 +03:00 committed by Teknium
parent c1606aed69
commit 0240baa357
2 changed files with 22 additions and 0 deletions

View file

@ -1389,6 +1389,7 @@ class AIAgent:
content = re.sub(r'<thinking>.*?</thinking>', '', content, flags=re.DOTALL | re.IGNORECASE)
content = re.sub(r'<reasoning>.*?</reasoning>', '', content, flags=re.DOTALL)
content = re.sub(r'<REASONING_SCRATCHPAD>.*?</REASONING_SCRATCHPAD>', '', content, flags=re.DOTALL)
content = re.sub(r'</?(?:think|thinking|reasoning|REASONING_SCRATCHPAD)>\s*', '', content, flags=re.IGNORECASE)
return content
def _looks_like_codex_intermediate_ack(

View file

@ -230,6 +230,27 @@ class TestStripThinkBlocks:
assert "line1" not in result
assert "visible" in result
def test_orphaned_closing_think_tag(self, agent):
result = agent._strip_think_blocks("some reasoning</think>actual answer")
assert "</think>" not in result
assert "actual answer" in result
def test_orphaned_closing_thinking_tag(self, agent):
result = agent._strip_think_blocks("reasoning</thinking>answer")
assert "</thinking>" not in result
assert "answer" in result
def test_orphaned_opening_think_tag(self, agent):
result = agent._strip_think_blocks("<think>orphaned reasoning without close")
assert "<think>" not in result
def test_mixed_orphaned_and_paired_tags(self, agent):
text = "stray</think><think>paired reasoning</think> visible"
result = agent._strip_think_blocks(text)
assert "</think>" not in result
assert "<think>" not in result
assert "visible" in result
class TestExtractReasoning:
def test_reasoning_field(self, agent):