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(v41): continue an open assistant turn on deepseek_v41

eni23278566f17a
diff --git a/c/openai_server.py b/c/openai_server.py
index 133587b0..7ffe309a 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -2311,13 +2311,18 @@ def _dsv41_merge_turns(messages):
def render_chat_dsv41(messages, enable_thinking=False, reasoning_effort=None, tools=None,
- tool_choice=None):
+ tool_choice=None, add_generation_prompt=True):
"""encoding.py _encode_messages_text for one turn.
Tool use follows the checkpoint's own DSML format (encoding/encoding.py, vendored in
v41_dsml.py): schemas are declared at the end of the system message, assistant tool
calls are <|DSML| calls> blocks, and tool results are <tool_result> blocks merged
into the following user turn.
+
+ add_generation_prompt=False continues a trailing assistant turn: the last turn is rendered
+ open -- its <think></think> block and content as a PAST turn, but without the closing
+ <|end▁of▁sentence|> and with no cue appended. That is the same drop-the-terminator move as
+ deepseek_v4, whose EOS this shares; the model resumes from the content it was handed.
"""
if not isinstance(messages, list) or not messages:
raise APIError(400, "`messages` must be a non-empty array.", "messages")
@@ -2377,10 +2382,13 @@ def render_chat_dsv41(messages, enable_thinking=False, reasoning_effort=None, to
prompt.append(turn["content"])
if turn.get("tool_calls"):
prompt.append(v41_dsml.render_tool_calls(turn["tool_calls"]))
- prompt.append(DSV41_EOS)
+ # A continued turn is the last message rendered open: no EOS, no cue below.
+ if add_generation_prompt or index != len(turns) - 1:
+ prompt.append(DSV41_EOS)
# the generation cue, exactly as render_message appends it after a user turn
- prompt.append(DSV41_ASSISTANT)
- prompt.append("<think>" if enable_thinking and len(turns) - 1 >= last_user else "</think>")
+ if add_generation_prompt:
+ prompt.append(DSV41_ASSISTANT)
+ prompt.append("<think>" if enable_thinking and len(turns) - 1 >= last_user else "</think>")
return "".join(prompt)
@@ -2413,7 +2421,7 @@ def render_chat_dsv41(messages, enable_thinking=False, reasoning_effort=None, to
# 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", "deepseek_v4", "inkling",
- "kimi"}
+ "kimi", "deepseek_v41"}
def resolve_generation_prompt(messages, body):
@@ -2522,7 +2530,8 @@ def render_chat_for_arch(messages, enable_thinking=False, reasoning_effort=None,
return render_chat_kimi(messages, enable_thinking, reasoning_effort, tools,
tool_choice, add_generation_prompt=add_generation_prompt)
if ARCH == "deepseek_v41":
- return render_chat_dsv41(messages, enable_thinking, reasoning_effort, tools, tool_choice)
+ return render_chat_dsv41(messages, enable_thinking, reasoning_effort, tools,
+ tool_choice, add_generation_prompt)
return render_chat(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 5bec807a..60ed002f 100644
--- a/c/tests/test_openai_server.py
+++ b/c/tests/test_openai_server.py
@@ -25,7 +25,8 @@ from openai_server import (APIError, APIHandler, APIServer, ClientCancelled,
read_engine_turn, render_chat, render_chat_for_arch,
render_chat_glm53, render_chat_inkling, render_chat_kimi,
render_chat_olmoe,
- render_chat_qwen38, render_chat_v4, _dsv4_tool_calls, serve,
+ render_chat_qwen38, render_chat_v4, render_chat_dsv41,
+ _dsv4_tool_calls, serve,
resolve_generation_prompt, split_thinking_reply,
starts_in_reasoning,
stop_policy, tune_child_env)
@@ -2184,6 +2185,42 @@ class TrailingAssistantTurnTest(unittest.TestCase):
self.assertTrue(cont.endswith("La capitale e'"), cont[-40:])
self.assertFalse(cont.endswith(EOS))
+ def test_continuation_open_turn_deepseek_v41(self):
+ """deepseek_v41, like deepseek_v4, is pinned to encoding.py rather than a diffable
+ vendored jinja template, so it gets the same expected-string pin: the open turn is a
+ literal here, with the past-turn-minus-EOS invariant kept as an added check, in both
+ thinking modes.
+
+ Unlike v4, the v41 open turn is NOT identical across the modes: thinking-on carries the
+ <|System|> Reasoning Effort line and closes the continued turn's (empty) reasoning as
+ <think></think>; thinking-off has neither. The empty <think></think> is the SAFE
+ position #1327 is about, not the out-of-distribution one -- it is followed by the
+ client's content ('La capitale e''), never left dangling at the end of the prompt,
+ which resolve_generation_prompt guarantees by refusing an empty continuation."""
+ EOS = "<|end▁of▁sentence|>"
+ ASSISTANT = "<|Assistant|>"
+ EFFORT = ("<|System|>Reasoning Effort: 75 (range 1-100, the higher the value, the "
+ "more thorough the reasoning)\n\n")
+ # The literal open turns, written out so the test does not lean on another renderer
+ # call for its only expected value (see the deepseek_v4 test), one per thinking mode.
+ EXPECTED_OPEN = {
+ True: ("<|begin▁of▁sentence|>" + EFFORT + "<|User|>capitale della Francia?"
+ "<|Assistant|><think></think>La capitale e'"),
+ False: ("<|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_v41"):
+ normal = render_chat_dsv41(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[enable_thinking])
+ # 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_continuation_open_turn_inkling(self):
"""inkling uses its own markers, and render_chat_inkling deliberately deviates from
the template's generation cue (it prefills <|content_text|> in the thinking-off case