@@ -4986,10 +4986,9 @@ async def handle_sse_stream(self, genparams, api_format):
49864986 if api_format == 4 and using_openai_tools:
49874987 tokenStr = tokenReserve + tokenStr
49884988 tokenReserve = ""
4989- splitter = tool_segment_tag
4990- if splitter in tokenStr:
4989+ if tool_segment_tag in tokenStr:
49914990 if not genparams.get("sync_toolcall_potential_triggered",False):
4992- sync_potential_toolcall_splitmatch = splitter
4991+ sync_potential_toolcall_splitmatch = tool_segment_tag
49934992 genparams['sync_toolcall_potential_triggered'] = True #if tool calls is triggered, rest will be sync fake streaming. we'll buffer it for later
49944993
49954994 need_split_final_msg = True if (currfinishreason is not None and streamDone and tokenStr!="") else False
@@ -5004,14 +5003,23 @@ async def handle_sse_stream(self, genparams, api_format):
50045003 encap_in_thinking = False
50055004 foundend = True
50065005 out1, out2 = tokenStr.split(pair["end"], 1)
5006+ # Swallow any extraneous start tags that appear while already thinking
5007+ if pair["start"] in out1:
5008+ out1 = out1.replace(pair["start"], "")
50075009 if out1:
50085010 delta['reasoning_content'] = out1
5011+ # Swallow any extraneous end tags that appear after already ending
5012+ if pair["end"] in out2:
5013+ out2 = out2.replace(pair["end"], "")
50095014 if out2:
50105015 delta['content'] = out2
50115016 break
50125017 if not foundend:
5013- # Still thinking
5014- delta['reasoning_content'] = tokenStr
5018+ # Still thinking - swallow extraneous start tags from THIS pair only
5019+ cleaned = tokenStr
5020+ if pair["start"] in cleaned:
5021+ cleaned = cleaned.replace(pair["start"], "")
5022+ delta['reasoning_content'] = cleaned
50155023 else:
50165024 # Not thinking. Let's see if a start tag appears in this chunk.
50175025 matched_start = False
@@ -5045,8 +5053,13 @@ async def handle_sse_stream(self, genparams, api_format):
50455053 matched_start = True
50465054 break
50475055 # Condition C: No start tag found, just normal text
5056+ # Swallow any extraneous end tags that appear while not thinking
50485057 if not matched_start:
5049- delta['content'] = tokenStr
5058+ cleaned = tokenStr
5059+ # Only swallow stray end tags if we've already locked in a pair
5060+ if len(thinkpairs) == 1 and thinkpairs[0]["end"] in cleaned:
5061+ cleaned = cleaned.replace(thinkpairs[0]["end"], "")
5062+ delta['content'] = cleaned
50505063 encap_first_loop = False
50515064 else:
50525065 delta['content'] = tokenStr
0 commit comments