Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: a <tool_call> named in prose before the real call is content, not a malformed call

Trevin38c9a4dce29e
diff --git a/serve/frontend.py b/serve/frontend.py
index 13fa735fc..31b1fa56c 100644
--- a/serve/frontend.py
+++ b/serve/frontend.py
@@ -395,6 +395,7 @@ CALL_END = "</tool_call>"
PARAM_END = "</parameter>"
+FUNC_START = "<function="
FUNC_END = "</function>"
@@ -726,6 +727,22 @@ class OutputParser:
out.append(Event("content", self.buf[:j]))
self.buf = self.buf[j:]
return out
+ # A call is `<tool_call>` and then (after whitespace) `<function=`; the tag with anything else after
+ # it is prose that names the format ("I'll use a <tool_call> block") - content, not a malformed call
+ # that ends the request. Until its follower has arrived it is held, like a partial tag.
+ after = self.buf[i + len(CALL_START):].lstrip()
+ if after and not after.startswith(FUNC_START) and not FUNC_START.startswith(after):
+ out.append(Event("content", self.buf[:i + len(CALL_START)]))
+ self.buf = self.buf[i + len(CALL_START):]
+ continue
+ if not after.startswith(FUNC_START):
+ j = i
+ while j > 0 and self.buf[j - 1] == "\n":
+ j -= 1
+ if j > 0:
+ out.append(Event("content", self.buf[:j]))
+ self.buf = self.buf[j:]
+ return out
if i and self.buf[:i].strip():
out.append(Event("content", self.buf[:i].rstrip("\n")))
self.buf = self.buf[i + len(CALL_START):]
diff --git a/serve/test_server.py b/serve/test_server.py
index 778e8f4f7..a5c5be973 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -483,6 +483,41 @@ class ToolCallTerminators(unittest.TestCase):
self.assertEqual(json.loads(streamed), {"path": "doc.md", "content": self.CONTENT})
+class ToolCallTagInProse(unittest.TestCase):
+ """A reply that names the `<tool_call>` tag in its prose before the real call keeps the tag as content and still
+ returns the call; it was a "malformed tool call" ValueError that ended the request. A call is the tag followed
+ (after whitespace) by `<function=`."""
+ SCHEMA = ToolCallTerminators.SCHEMA
+ CALL = ("<tool_call>\n<function=write>\n<parameter=path>\na.md\n</parameter>\n<parameter=content>\nhi\n"
+ "</parameter>\n</function>\n</tool_call>")
+
+ def parse(self, text, stream_tools, step):
+ from serve.frontend import OutputParser
+ p = OutputParser(thinking=True, tools=self.SCHEMA, stream_tools=stream_tools)
+ evs = []
+ for i in range(0, len(text), step):
+ evs += p.feed(text[i:i + step])
+ evs += p.finish()
+ return ([e.call.arguments for e in evs if e.kind == "tool_call"],
+ "".join(e.text for e in evs if e.kind == "content"))
+
+ def test_named_tag_is_content(self):
+ call = {"path": "a.md", "content": "hi"}
+ for prose in ("I will use the `<tool_call>` format now.", "Next I emit a <tool_call> block.",
+ "Two tags <tool_call> and <tool_call>x, then the call."):
+ for stream_tools in (False, True):
+ for step in (1, 7, 10_000):
+ with self.subTest(prose=prose, stream_tools=stream_tools, step=step):
+ calls, content = self.parse(f"</think>\n\n{prose}\n{self.CALL}", stream_tools, step)
+ self.assertEqual((calls, content), ([call], prose))
+
+ def test_tag_alone_at_the_end_is_content(self):
+ for step in (1, 7, 10_000):
+ with self.subTest(step=step):
+ self.assertEqual(self.parse("</think>\n\nThe format starts with <tool_call>", False, step),
+ ([], "The format starts with <tool_call>"))
+
+
class UnfinishedToolCall(unittest.TestCase):
"""#211: a call the output ends inside is not reported as a whole one - its streamed JSON is not closed and the
finish reason is not "tool_calls" / "tool_use" - so a client can tell it from a call to run."""