jeanbaptdzd commited on
Commit
a5e663f
·
1 Parent(s): d39e295

Fix reasoning tag handling: better support for unclosed <think> tags

Browse files
app/providers/transformers_provider.py CHANGED
@@ -605,12 +605,20 @@ class TransformersProvider:
605
  flags=re.DOTALL | re.IGNORECASE
606
  )
607
 
608
- # Handle unclosed reasoning tags (split on closing tag)
609
  if "</think>" in cleaned_text:
610
  parts = cleaned_text.split("</think>", 1)
611
  if len(parts) > 1:
612
  cleaned_text = parts[1].strip()
613
 
 
 
 
 
 
 
 
 
614
  # Step 2: Try to find JSON wrapped in markdown code blocks
615
  json_code_block = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', cleaned_text, re.DOTALL)
616
  if json_code_block:
 
605
  flags=re.DOTALL | re.IGNORECASE
606
  )
607
 
608
+ # Also handle unclosed reasoning tags (split on closing tag)
609
  if "</think>" in cleaned_text:
610
  parts = cleaned_text.split("</think>", 1)
611
  if len(parts) > 1:
612
  cleaned_text = parts[1].strip()
613
 
614
+ # If still has opening tag but no closing, remove everything before first }
615
+ # This handles cases where reasoning tag is not closed
616
+ if "<think>" in cleaned_text.lower() and "}" in cleaned_text:
617
+ # Find first { and take everything from there
618
+ brace_pos = cleaned_text.find('{')
619
+ if brace_pos != -1:
620
+ cleaned_text = cleaned_text[brace_pos:]
621
+
622
  # Step 2: Try to find JSON wrapped in markdown code blocks
623
  json_code_block = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', cleaned_text, re.DOTALL)
624
  if json_code_block: