Add missing chat_template.jinja

#26

Missing chat_template.jinja

This repo ships an encoding/ folder with a Python reference encoder (encoding_dsv4.py) instead of a Jinja chat_template. That means any server using the standard tokenizer.apply_chat_template() / a generic OpenAI-compatible endpoint (rather than the Python encoder directly) currently has no correct way to format prompts for this model.

This PR adds chat_template.jinja, reverse-engineered directly from encoding/encoding_dsv4.py and cross-checked against a real, locally-tokenized comparison of 48 hand-built conversations (system/no-system, multi-turn, reasoning retention on/off, all three reasoning_effort levels in both chat_template_kwargs.thinking_mode values, and tool calling) — all 48 now produce byte-identical token IDs to the reference encoder's output.

It also matches real serving behavior verified against a live vLLM 0.26.0 deployment on actual GPU hardware, including two things worth flagging separately since they affect vLLM's own built-in support for this model (opening a corresponding issue/PR there):

  1. vLLM's native tokenizer_mode=deepseek_v4 path currently produces identical output for reasoning_effort="low" and "high" (only "max" gets a non-empty prefix) — this template correctly distinguishes all three.
  2. Tool schemas must arrive as the standard top-level tools request field (not embedded in a message) for a template/renderer to see them at all in most serving stacks — this template supports both, in case a caller uses either convention.

Covers: BOS/EOS/turn tokens, system message placement (including no-system), reasoning_content retention rules (including the tools-present override), thinking_mode chat/thinking behavior, reasoning_effort prefixes for low/high/max, and DSML-format tool calls (including folding tool role results into the preceding user turn).

Not covered (documented in the template's own header comment): the internal developer role and quick-instruction task tokens, both explicitly described in this repo's own encoding/README.md as internal-pipeline-only, not reachable through a standard /v1/chat/completions request.

Independently verified against encoding/encoding_dsv4.py on vLLM 0.21.1rc1.dev339+g1967a5627bc3 (DSpark NVFP4 build, 2× DGX Spark GB10, TP=2). Confirming the reasoning_effort finding, with a correction: it's not only that low and high produce identical output — max renders the high prompt text.

Reference encoder (encode_messages, thinking_mode="thinking"):

low → 50 chars, empty prefix
high → 526 chars, prefix ends ...no assumption is left unchecked.
max → 576 chars, prefix ends ...no error remains undiscovered.

Server via /tokenize with chat_template_kwargs:

low → 5 tokens, empty
high → 5 tokens, empty (prefix dropped)
max → 84 tokens, prefix ends ...no assumption is left unchecked. — i.e. the high text

So high is unreachable and max silently delivers high. Anyone benchmarking effort levels through a standard OpenAI-compatible endpoint has been measuring high while believing they measured max. Your template distinguishes all three correctly.

Is this the reason why I see the thinking (including the closing thinking tag) in my opwenwebui output if i enable reasoning?

Possibly, but not confirmed — this PR fixes reasoning_effort levels being conflated (low/high identical, max mislabeled), not tag-boundary parsing per se.

That said, a plausible mechanism: if OpenWebUI detects reasoning by matching <think>...</think> in raw output, and the chat template actually applied at inference time doesn't prime the assistant turn the way this one does, that boundary detection can break and leak the closing tag as literal text.

To confirm: which backend are you serving on (vLLM/SGLang/other), and which chat_template is currently active — repo default or something OpenWebUI supplies? If you can paste a raw completion showing the leaked tag, I can check it against the encoder logic.

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment