Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

Qwen serve: a <tool_call> tag quoted in the reasoning or the answer is text, not the start of a call

YanissAmz663dc55912d0
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index fa0ee11..a2e6326 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -151,6 +151,20 @@ class Splitter:
self.said, self.after_call = True, False
out.append((self.mode, body))
+ def _tool_open(self, final: bool) -> tuple[int, bool]:
+ """(position, undecided) of the first <tool_call> in buf that opens a call: <function= or a JSON object follows it.
+ The tag quoted in the text (a thought about tool calls, an answer that explains them) is text: taking it for a call
+ swallowed the rest of the reply (a reply of 2602 tokens delivered 1364 characters, empty answer, finish stop)."""
+ i = self.buf.find(TOOL_OPEN)
+ while i >= 0:
+ rest = self.buf[i + len(TOOL_OPEN):].lstrip()
+ if rest.startswith(("<function=", "{")):
+ return i, False
+ if not final and (not rest or "<function=".startswith(rest)):
+ return i, True
+ i = self.buf.find(TOOL_OPEN, i + 1)
+ return -1, False
+
def feed(self, text: str, final: bool = False) -> list[tuple[str, str]]:
out: list[tuple[str, str]] = []
self.buf += text
@@ -167,9 +181,16 @@ class Splitter:
self.mode, self.lead, self.after_call = "content", True, True
continue
tags = [TOOL_OPEN] + ([THINK_CLOSE] if self.mode == "reasoning" else [])
- hits = [(self.buf.find(t), t) for t in tags if t in self.buf]
+ hits = [(self.buf.find(t), t) for t in tags if t in self.buf and t != TOOL_OPEN]
+ tpos, undecided = self._tool_open(final)
+ if tpos >= 0:
+ hits.append((tpos, TOOL_OPEN))
if hits:
pos, tag = min(hits)
+ if tag == TOOL_OPEN and undecided: # the text after the tag is too short to tell a call from a quotation: wait
+ self._emit(self.buf[:pos], out)
+ self.buf = self.buf[pos:]
+ return out
self._emit(self.buf[:pos], out)
self.ws = ""
self.buf = self.buf[pos + len(tag):]
diff --git a/tools/qwen/test_serve.py b/tools/qwen/test_serve.py
index 2b63b49..439291f 100644
--- a/tools/qwen/test_serve.py
+++ b/tools/qwen/test_serve.py
@@ -121,6 +121,31 @@ class ParserTests(unittest.TestCase):
self.assertNotIn("EXL3_DRAFT2S", dict(serve.SERVE_ENV))
self.assertFalse(serve.DRAFT2S_IN_PACK.endswith(".safetensors"))
+ def test_quoted_tool_call_tag_is_text(self):
+ # reasoning and answers that mention the tag: no call, nothing swallowed (streamed in 3-character pieces too)
+ text = "the Splitter (handles `<tool_call>` blocks) matters\n</think>\n\nUse <tool_call> then </tool_call> like this."
+ for step in (len(text), 3, 1):
+ sp = serve.Splitter(True)
+ pieces = []
+ for i in range(0, len(text), step):
+ pieces += sp.feed(text[i:i + step])
+ pieces += sp.finish()
+ self.assertEqual("".join(t for k, t in pieces if k == "reasoning"), "the Splitter (handles `<tool_call>` blocks) matters")
+ self.assertEqual("".join(t for k, t in pieces if k == "content"), "Use <tool_call> then </tool_call> like this.")
+ self.assertEqual(sp.tool_text, "")
+
+ def test_real_call_after_a_quotation(self):
+ text = "see `<tool_call>` here\n</think>\n\n<tool_call>\n<function=f>\n<parameter=a>1</parameter>\n</function>\n</tool_call>"
+ for step in (len(text), 2, 1):
+ sp = serve.Splitter(True)
+ for i in range(0, len(text), step):
+ sp.feed(text[i:i + step])
+ sp.finish()
+ self.assertEqual([c["function"]["name"] for c in serve.parse_tool_calls(sp.tool_text, None)], ["f"])
+ sp = serve.Splitter(False)
+ sp.feed('<tool_call>\n{"name": "g", "arguments": {}}\n</tool_call>'); sp.finish()
+ self.assertEqual([c["function"]["name"] for c in serve.parse_tool_calls(sp.tool_text, None)], ["g"])
+
def test_reasoning_first_split(self):
m = serve.split_completion("let me think\nmore\n</think>\n\nThe answer.\n", True, None)
self.assertEqual(m["reasoning_content"], "let me think\nmore")