Skip to content

Commit 3064c69

Browse files
committed
Handle missing opening tag in reasoning extraction
Some models (like Qwen3 thinking models) occasionally output reasoning content without the opening <think> tag but include the closing </think>. Added fallback detection that: - Checks for closing tags when opening tags are missing - Splits content on closing tag to extract reasoning - Reports detection as "</think> (missing open tag)" in troubleshooting
1 parent 079daf6 commit 3064c69

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

LMStudio.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,18 @@ def _extract_reasoning_auto(self, text: str) -> Tuple[str, str, Optional[str]]:
368368
clean_text = re.sub(pattern, "", text, flags=re.DOTALL)
369369
return clean_text.strip(), "\n---\n".join(reasoning_parts).strip(), open_tag
370370

371+
# Fallback: Check for closing tag without opening tag (model bug/edge case)
372+
# Some models forget the opening <think> but include closing </think>
373+
for _, open_tag, close_tag in COMMON_REASONING_PATTERNS:
374+
if close_tag in text and open_tag not in text:
375+
# Split on closing tag - everything before is reasoning
376+
parts = text.split(close_tag, 1)
377+
if len(parts) == 2:
378+
reasoning = parts[0].strip()
379+
response = parts[1].strip()
380+
if reasoning and response:
381+
return response, reasoning, f"{close_tag} (missing open tag)"
382+
371383
# No pattern matched
372384
return text, "", None
373385

0 commit comments

Comments
 (0)