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.

Streaming tool calls: one stream message per call (Qwen, GLM, MiMo)

YanissAmz3e8ea2ed1958
diff --git a/tools/glm/serve.py b/tools/glm/serve.py
index 7fbdae0..6d0b1d0 100644
--- a/tools/glm/serve.py
+++ b/tools/glm/serve.py
@@ -614,8 +614,8 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
await flush(True)
message = parse_completion(text)
if "tool_calls" in message:
- await response.write(sse(event({"tool_calls": [
- dict(call, index=n) for n, call in enumerate(message["tool_calls"])]})))
+ for n, call in enumerate(message["tool_calls"]): # one stream message per call (clients that read one call per message)
+ await response.write(sse(event({"tool_calls": [dict(call, index=n)]})))
await response.write(sse(event({}, finish_for(message, text[len(prefix):])) |
{"usage": usage(text[len(prefix):]), "timings": timings(text)}))
await response.write(b"data: [DONE]\n\n")
diff --git a/tools/glm/test_serve.py b/tools/glm/test_serve.py
index effda68..8f01c4b 100644
--- a/tools/glm/test_serve.py
+++ b/tools/glm/test_serve.py
@@ -211,6 +211,24 @@ class ServeTests(unittest.TestCase):
run(check())
+ def test_stream_parallel_tool_calls_one_message_each(self):
+ from aiohttp.test_utils import TestClient, TestServer
+ call = '<tool_call>get_weather<arg_key>city</arg_key><arg_value>Paris</arg_value></tool_call>'
+ app = serve.create_app(FakeEngine("plan</think>Sure." + call * 3), "m", self.template + "<think>")
+
+ async def check():
+ client = TestClient(TestServer(app))
+ await client.start_server()
+ response = await client.post("/v1/chat/completions", json={
+ "model": "m", "stream": True, "messages": [{"role": "user", "content": "hi"}]})
+ raw = (await response.read()).decode()
+ await client.close()
+ return raw
+ chunks = [json.loads(l[6:]) for l in run(check()).split("\n") if l.startswith("data: {")]
+ msgs = [c["choices"][0]["delta"]["tool_calls"] for c in chunks if "tool_calls" in c["choices"][0]["delta"]]
+ self.assertEqual([len(m) for m in msgs], [1, 1, 1])
+ self.assertEqual([m[0]["index"] for m in msgs], [0, 1, 2])
+
def test_stream_open_think_and_tool_call(self):
from aiohttp.test_utils import TestClient, TestServer
engine = FakeEngine('plan it</think>Sure.<tool_call>get_weather<arg_key>city</arg_key>'
diff --git a/tools/mimo/serve.py b/tools/mimo/serve.py
index 9d8e729..aacc47c 100644
--- a/tools/mimo/serve.py
+++ b/tools/mimo/serve.py
@@ -531,8 +531,8 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
await flush(True)
message = parse_completion(text, body.get("tools"))
if "tool_calls" in message:
- await response.write(sse(event({"tool_calls": [
- dict(call, index=n) for n, call in enumerate(message["tool_calls"])]})))
+ for n, call in enumerate(message["tool_calls"]): # one stream message per call (clients that read one call per message)
+ await response.write(sse(event({"tool_calls": [dict(call, index=n)]})))
await response.write(sse(event({}, finish_for(message, text)) |
{"usage": usage(text), "timings": timings()}))
await response.write(b"data: [DONE]\n\n")
diff --git a/tools/mimo/test_serve.py b/tools/mimo/test_serve.py
index 1a5f633..b973532 100644
--- a/tools/mimo/test_serve.py
+++ b/tools/mimo/test_serve.py
@@ -201,6 +201,24 @@ class ServeTests(unittest.TestCase):
self.assertTrue(all(c["choices"][0]["finish_reason"] is None for c in chunks[:-1]))
self.assertIn("usage", chunks[-1])
+ def test_stream_parallel_tool_calls_one_message_each(self):
+ from aiohttp.test_utils import TestClient, TestServer
+ call = '<tool_call><function=get_weather>{"city": "Paris"}</function></tool_call>'
+ app = serve.create_app(FakeEngine("Sure." + call * 3), "m", TEMPLATE)
+
+ async def check():
+ client = TestClient(TestServer(app))
+ await client.start_server()
+ response = await client.post("/v1/chat/completions", json={
+ "model": "m", "stream": True, "messages": [{"role": "user", "content": "hi"}]})
+ raw = (await response.read()).decode()
+ await client.close()
+ return raw
+ chunks = [json.loads(l[6:]) for l in run(check()).split("\n") if l.startswith("data: {")]
+ msgs = [c["choices"][0]["delta"]["tool_calls"] for c in chunks if "tool_calls" in c["choices"][0]["delta"]]
+ self.assertEqual([len(m) for m in msgs], [1, 1, 1])
+ self.assertEqual([m[0]["index"] for m in msgs], [0, 1, 2])
+
def test_stream_and_parse_agree_on_bare_think_close(self):
"""No implicit think turn: a bare </think> is literal content in both paths."""
from aiohttp.test_utils import TestClient, TestServer
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index c3a7c44..58f49b3 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -1077,7 +1077,8 @@ def create_app(engine: Any, model_id: str, template: str, defaults: dict[str, An
await response.write(sse(event({key: piece})))
calls = parse_tool_calls(sp.tool_text, job["tools"])
if calls:
- await response.write(sse(event({"tool_calls": [dict(c, index=i) for i, c in enumerate(calls)]})))
+ for i, c in enumerate(calls): # one stream message per call: some clients read only the first call of a message
+ await response.write(sse(event({"tool_calls": [dict(c, index=i)]})))
reason, usage, timings = finish_and_stats(stats, full, bool(calls))
final = event({}, reason) | {"usage": usage, "timings": timings}
if job["return_token_ids"]:
diff --git a/tools/qwen/test_serve.py b/tools/qwen/test_serve.py
index 0453f7d..ab379d1 100644
--- a/tools/qwen/test_serve.py
+++ b/tools/qwen/test_serve.py
@@ -269,6 +269,15 @@ class HttpTests(unittest.TestCase):
self.assertEqual(ev[-1]["usage"]["completion_tokens"], 7)
self.assertIn("timings", ev[-1])
+ def test_parallel_tool_calls_one_stream_message_each(self):
+ eng = FakeEngine("</think>\n\n" + CALL + "\n" + CALL + "\n" + CALL)
+ st, raw = self.chat(eng, {"messages": [{"role": "user", "content": "hi"}], "tools": TOOLS}, stream=True)
+ msgs = [e["choices"][0]["delta"]["tool_calls"] for e in sse_events(raw) if e["choices"][0]["delta"].get("tool_calls")]
+ self.assertEqual([len(m) for m in msgs], [1, 1, 1])
+ self.assertEqual([m[0]["index"] for m in msgs], [0, 1, 2])
+ for m in msgs:
+ json.loads(m[0]["function"]["arguments"])
+
def test_stream_length(self):
eng = FakeEngine("abc", {"new_tokens": 2, "eos_reason": "max_new_tokens"})
_, raw = self.chat(eng, {"messages": [{"role": "user", "content": "hi"}]}, stream=True)