@@ -464,26 +464,27 @@ def handle_reasoning(self, text: str) -> None:
464464 text: Cleaned text to process for reasoning
465465 """
466466 # Default implementation for <think> style reasoning
467- if "<think>" in text and not self .state .state_changes ["reasoning_started" ]:
467+ # Check for tags in the complete buffer
468+ if "<think>" in self .state .buffer and not self .state .state_changes ["reasoning_started" ]:
468469 self .state .state_changes ["reasoning_started" ] = True
469470 if self .timing :
470471 self .timing .start_reasoning ()
471472
472- if "</think>" in text and not self . state . state_changes [ "reasoning_ended" ]:
473- self .state .state_changes [ "reasoning_ended" ] = True
474- if self . timing :
475- # Estimate token count from character count (rough approximation)
476- token_count = len ( self . state . buffer . split ("<think>" )[ 1 ]. split ( "< /think>")[ 0 ]) // 4
477- self .timing . end_reasoning ( token_count )
478-
479- if "<think>" in self . state . buffer :
480- parts = self . state . buffer . split ( "</think>" , 1 )
481- if len ( parts ) > 1 :
482- self .state .reasoning = parts [ 0 ]. split ( "<think>" , 1 )[ 1 ]. strip ()
483- self .state . response = parts [ 1 ]. strip ()
484- else :
485- self . state . reasoning = self .state .buffer . split ( "<think>" , 1 )[ 1 ]. strip ()
486- self .state . response = ""
473+ # Extract content and handle end of reasoning
474+ parts = self .state .buffer . split ( "<think>" , 1 )
475+ if len ( parts ) > 1 :
476+ reasoning_text = parts [ 1 ]
477+ end_parts = reasoning_text . split ("</think>" , 1 )
478+ self .state . reasoning = end_parts [ 0 ]. strip ( )
479+ self . state . response = end_parts [ 1 ]. strip () if len ( end_parts ) > 1 else ""
480+
481+ # Check for end tag in complete buffer
482+ if "</think>" in self . state . buffer and not self . state . state_changes [ "reasoning_ended" ] :
483+ self .state .state_changes [ "reasoning_ended" ] = True
484+ if self .timing :
485+ # Estimate token count from character count (rough approximation)
486+ token_count = len ( self .state .reasoning ) // 4
487+ self .timing . end_reasoning ( token_count )
487488 else :
488489 self .state .response = self .state .buffer
489490
0 commit comments