serve: a <tool_call> in the reasoning that never closes stops holding the thinking back after 32 KiB (#804)

This commit is contained in:
Niko1221 committed 2026-10-05 13:16:54 +02:00
1 parent 378dc39348
commit ee412cd776
2 files changed
+18

No files matched your search

+7
View File
@@ -538,6 +538,9 @@ def parse_tool_call(body: str, schema: dict | None = None) -> ToolCall:
return ToolCall(name=name, arguments=args)
RCALL_MAX = 32768 # the most of a `<tool_call>` inside the reasoning that is held while waiting for its end (#804)
class OutputParser:
"""Incremental parser of the model's text. Feed deltas; get events. A tag split across deltas is held back
until it is complete, so clients never see `<tool_` or `</thi`."""
@@ -701,6 +704,10 @@ class OutputParser:
out.append(Event("reasoning", self.buf[:len(CALL_START) + think]))
self.buf = body[think:]
self.state = "reasoning"
elif len(body) > RCALL_MAX: # a tag in the prose that never closes: stop holding the thinking back
out.append(Event("reasoning", self.buf))
self.buf = ""
self.state = "reasoning"
else:
return out
elif self.state == "reasoning":
+11
View File
@@ -10,6 +10,7 @@ from __future__ import annotations
import json
import sys
import unittest
import unittest.mock
import urllib.request
from pathlib import Path
@@ -93,6 +94,16 @@ class Parser(unittest.TestCase):
self.assertEqual(len([e for e in evs if e.kind == "tool_call"]), 1)
self.assertEqual(joined(evs, "reasoning"), "think")
def test_a_tag_that_never_closes_does_not_hold_the_thinking_back(self):
from serve import frontend
text = "a <tool_call> " + "thinking on and on " * 3000 + "</think>\n\nanswer"
with unittest.mock.patch.object(frontend, "RCALL_MAX", 1000):
evs = run(text, 50)
first_reasoning_at = next(i for i, e in enumerate(evs) if e.kind == "reasoning" and len(e.text) > 100)
self.assertLess(first_reasoning_at, 100) # streamed, not kept until </think>
self.assertEqual(joined(evs, "reasoning"), text.split("</think>")[0])
self.assertEqual(joined(evs, "content"), "answer")
def test_a_partial_tag_is_held(self):
p = OutputParser(thinking=True, tools=TOOLS)
evs = p.feed("hello <tool_c")