Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

feat(v4): continue an open assistant turn on deepseek_v4

enie62dbed91269
diff --git a/c/openai_server.py b/c/openai_server.py
index b87f1bba..cf331328 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -1149,13 +1149,17 @@ def render_chat_kimi(messages, enable_thinking=False, reasoning_effort=None, too
def render_chat_v4(messages, enable_thinking=False, reasoning_effort=None, tools=None,
- tool_choice=None):
+ tool_choice=None, add_generation_prompt=True):
"""DeepSeek V4's native multi-turn chat template.
The target engine receives this as a raw prompt. Prior assistant turns end
with the checkpoint's EOS marker; the final assistant marker selects the
thinking or direct-answer prefix for the new turn.
+ add_generation_prompt=False continues a trailing assistant turn: the last assistant turn
+ is rendered open, i.e. without its closing EOS and with no cue, the position the model
+ occupies mid-turn. EOS is the terminator to drop here, as <|im_end|> is for ChatML.
+
Tool use follows the official DSML format (encoding/encoding_dsv4.py): tool
schemas are declared on the first system/developer message, assistant tool
calls are DSML blocks, and tool results are <tool_result> blocks merged into
@@ -1229,7 +1233,7 @@ def render_chat_v4(messages, enable_thinking=False, reasoning_effort=None, tools
effort = DSV4_REASONING_EFFORT.get(reasoning_effort, "low")
if effort != "low":
parts.append(DSV4_REASONING_EFFORT_PROMPTS[effort])
- for message in merged:
+ for m_index, message in enumerate(merged):
role = message["role"]
if role in ("system", "developer"):
if role == "developer":
@@ -1251,8 +1255,11 @@ def render_chat_v4(messages, enable_thinking=False, reasoning_effort=None, tools
parts.append(message["content"])
if message.get("tool_calls"):
parts.append(_dsv4_tool_calls(message["tool_calls"]))
- parts.append(eos)
- parts.extend((assistant, "<think>" if enable_thinking else "</think>"))
+ # A continued turn is the last message rendered open: no EOS, no cue below.
+ if add_generation_prompt or m_index != len(merged) - 1:
+ parts.append(eos)
+ if add_generation_prompt:
+ parts.extend((assistant, "<think>" if enable_thinking else "</think>"))
return "".join(parts)
@@ -2381,7 +2388,7 @@ def render_chat_dsv41(messages, enable_thinking=False, reasoning_effort=None, to
# (the cue is appended, exactly as before this existed) rather than erroring -- continuation is
# on by default, and a family without its open-turn shape yet must not start rejecting requests
# nobody opted into. Each renderer adds itself here in the same commit that derives its shape.
-CONTINUATION_FAMILIES = {"glm53", "qwen38", "qwen36", "glm", "olmoe"}
+CONTINUATION_FAMILIES = {"glm53", "qwen38", "qwen36", "glm", "olmoe", "deepseek_v4"}
def resolve_generation_prompt(messages, body):
@@ -2481,8 +2488,10 @@ def render_chat_for_arch(messages, enable_thinking=False, reasoning_effort=None,
if ARCH == "olmoe":
return render_chat_olmoe(messages, enable_thinking, reasoning_effort, tools,
tool_choice, add_generation_prompt)
+ if ARCH == "deepseek_v4":
+ return render_chat_v4(messages, enable_thinking, reasoning_effort, tools,
+ tool_choice, add_generation_prompt)
renderer = (render_chat_kimi if ARCH == "kimi" else
- render_chat_v4 if ARCH == "deepseek_v4" else
render_chat_dsv41 if ARCH == "deepseek_v41" else render_chat)
return renderer(messages, enable_thinking, reasoning_effort, tools, tool_choice)
diff --git a/c/tests/test_openai_server.py b/c/tests/test_openai_server.py
index 432dfc90..874c8d72 100644
--- a/c/tests/test_openai_server.py
+++ b/c/tests/test_openai_server.py
@@ -21,7 +21,8 @@ from openai_server import (APIError, APIHandler, APIServer, ClientCancelled,
_engine_error, _image_bytes_from_url, cap_for_arch, conversation_cache_slot, model_arch,
generation_options, parse_tool_calls, parse_dsv4_tool_calls,
parse_arch_tool_calls, parse_k3_tool_calls, parse_qwen38_tool_calls,
- read_engine_turn, render_chat, render_chat_glm53, render_chat_kimi,
+ read_engine_turn, render_chat, render_chat_for_arch,
+ render_chat_glm53, render_chat_kimi,
render_chat_olmoe,
render_chat_qwen38, render_chat_v4, _dsv4_tool_calls, serve,
resolve_generation_prompt, split_thinking_reply,
@@ -2148,10 +2149,39 @@ class TrailingAssistantTurnTest(unittest.TestCase):
yet must render as before -- append the cue -- not reject a request nobody opted into.
Each family leaves this list in the commit that derives its shape; Kimi K3 (framed
engine-side, so its open turn is a change in kimi_k3.c) is the standing case."""
- for arch in ("deepseek_v4", "kimi", "inkling"):
+ for arch in ("kimi", "inkling"):
with self.on(), patch("openai_server.ARCH", arch):
self.assertTrue(resolve_generation_prompt(self.OPEN_TURN, {}))
+ def test_continuation_open_turn_deepseek_v4(self):
+ """deepseek_v4 has no authoritative vendored jinja template to diff against:
+ render_chat_v4 is pinned to the official encoding_dsv4.py, and the community
+ reap-150b template on the Hub diverges on the reasoning-block convention (a bare
+ </think> for a direct answer vs <think></think>). So this is the expected-string
+ pin the maintainer allows for such families -- the open turn is pinned to a literal
+ here, with the past-turn-minus-EOS invariant kept as an added check, in both
+ thinking modes."""
+ EOS = "<|end▁of▁sentence|>"
+ ASSISTANT = "<|Assistant|>"
+ # The literal open turn, written out so the test does not lean on another renderer
+ # call for its only expected value -- a bug that corrupts render_chat_v4 and the
+ # continuation path identically would slip past the comparison below but not this.
+ # Identical in both thinking modes: the open turn is the shape of the PAST turn,
+ # which carries no generation cue for enable_thinking to steer.
+ EXPECTED_OPEN = ("<|begin▁of▁sentence|><|User|>capitale della Francia?"
+ "<|Assistant|></think>La capitale e'")
+ for enable_thinking in (True, False):
+ cue = ASSISTANT + ("<think>" if enable_thinking else "</think>")
+ with patch("openai_server.ARCH", "deepseek_v4"):
+ normal = render_chat_v4(self.OPEN_TURN, enable_thinking=enable_thinking)
+ cont = render_chat_for_arch(self.OPEN_TURN, enable_thinking=enable_thinking,
+ add_generation_prompt=False)
+ self.assertEqual(cont, EXPECTED_OPEN)
+ # and the invariant tying it to the normal render: past turn without its EOS + cue
+ self.assertEqual(normal, cont + EOS + cue)
+ self.assertTrue(cont.endswith("La capitale e'"), cont[-40:])
+ self.assertFalse(cont.endswith(EOS))
+
def test_splitter_starts_in_content_mode_on_a_continued_turn(self):
"""Measured on glm53 int4, CPU: content '' with reasoning_chars 10 and 109,
clean stop, and a byte-correct open turn on the wire. The model was fine; the