diff --git a/serve/frontend.py b/serve/frontend.py index 500ceeb0..716e64f8 100644 --- a/serve/frontend.py +++ b/serve/frontend.py @@ -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 `` 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 ` 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": diff --git a/serve/test_reasoning_tools.py b/serve/test_reasoning_tools.py index a75adcd1..4468fbd3 100644 --- a/serve/test_reasoning_tools.py +++ b/serve/test_reasoning_tools.py @@ -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 " + "thinking on and on " * 3000 + "\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 + self.assertEqual(joined(evs, "reasoning"), text.split("")[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