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.

MiMo server: report finish_reason "length" when the reply stops at max_tokens

YanissAmz4fb486db9212
diff --git a/tools/mimo/serve.py b/tools/mimo/serve.py
index 7dac24a..8f22a77 100644
--- a/tools/mimo/serve.py
+++ b/tools/mimo/serve.py
@@ -367,6 +367,13 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
return {"prompt_tokens": prompt_tokens, "completion_tokens": completion_tokens,
"total_tokens": prompt_tokens + completion_tokens}
+ def finish_for(message: dict[str, Any], raw: str) -> str:
+ # A reply cut by max_tokens is "length", not "stop" (clients use it to tell a truncated answer).
+ finish = message.pop("finish_reason", "stop")
+ if finish == "stop" and engine.count_tokens(raw) >= body.get("max_tokens", 4096):
+ finish = "length"
+ return finish
+
def timings() -> dict[str, Any]:
# llama.cpp names: the swap orchestrator reads cache_n as the verdict of a KV restore
st = getattr(engine, "last_stats", None) or {}
@@ -417,7 +424,7 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
if "tool_calls" in message:
await response.write(sse(event({"tool_calls": [
dict(call, index=n) for n, call in enumerate(message["tool_calls"])]})))
- await response.write(sse(event({}, message.get("finish_reason", "stop")) |
+ await response.write(sse(event({}, finish_for(message, text)) |
{"usage": usage(text), "timings": timings()}))
await response.write(b"data: [DONE]\n\n")
await response.write_eof()
@@ -426,7 +433,7 @@ def create_app(engine: Any, model_id: str, template: str) -> web.Application:
raw = "".join([d async for d in deltas()])
text, _ = stop_text(raw, body["_stop"])
message = parse_completion(text)
- finish = message.pop("finish_reason", "stop")
+ finish = finish_for(message, text)
return web.json_response({
"id": request_id, "object": "chat.completion", "created": created, "model": model_id,
"choices": [{"index": 0, "message": message, "finish_reason": finish}],
diff --git a/tools/mimo/test_serve.py b/tools/mimo/test_serve.py
index f402393..38e88db 100644
--- a/tools/mimo/test_serve.py
+++ b/tools/mimo/test_serve.py
@@ -188,6 +188,29 @@ class ServeTests(unittest.TestCase):
self.assertEqual("".join(d.get("reasoning_content", "") for d in deltas), "")
self.assertEqual("".join(d.get("content", "") for d in deltas), "</think>answer")
+ def test_finish_reason_length_at_max_tokens(self):
+ """A reply cut by max_tokens reports "length", streaming and not; a short reply stays "stop"."""
+ from aiohttp.test_utils import TestClient, TestServer
+
+ async def call(output, max_tokens, stream):
+ app = serve.create_app(FakeEngine(output), "m", TEMPLATE)
+ client = TestClient(TestServer(app))
+ await client.start_server()
+ r = await client.post("/v1/chat/completions", json={
+ "model": "m", "stream": stream, "max_tokens": max_tokens,
+ "messages": [{"role": "user", "content": "hi"}]})
+ if stream:
+ raw = (await r.read()).decode()
+ out = [json.loads(l[6:]) for l in raw.split("\n") if l.startswith("data: {")][-1]
+ else:
+ out = await r.json()
+ await client.close()
+ return out["choices"][0]["finish_reason"]
+
+ for stream in (False, True):
+ self.assertEqual(run(call("one two three four", 4, stream)), "length")
+ self.assertEqual(run(call("one two", 4, stream)), "stop")
+
def test_stop_string_cuts_stream(self):
from aiohttp.test_utils import TestClient, TestServer
engine = FakeEngine("alpha STOP beta")