Sync chat_template.jinja with the 16 October encoding.py changes

#74

The template from #68 matched encoding/encoding.py when it was merged. The encoding fix of 16 October (314113f8, "preserve tool image order and current-turn thinking") changed two things the template does not follow yet:

  1. A user message that holds only tool results no longer starts a new turn, so in thinking mode with drop_thinking the reasoning of earlier tool steps in the current turn is kept. The template still drops it when the request has no tools list.
  2. The template ignores the content_blocks field that the encoder accepts on user messages and, since 16 October, on tool messages. Tool results given that way are left out or rendered as an empty <tool_result></tool_result>. The new image-order tests use this form.

This PR changes only those two places. To check it, we ran this repo's own encoding/test_encoding.py with every prompt rendered by the template through apply_chat_template (transformers 5.20.1) instead of encode_messages, with dict tool-call arguments:

template encoding.py @2cba9e42 (1 Oct) encoding.py @314113f8 (16 Oct)
current 50/50 51/55
this PR 50/50 55/55

We also rendered 9,000 random conversations (system and user messages, images, reasoning, one or two tool calls per step, tool results in or out of call order, thinking and chat modes, with and without tools). This PR's template gives byte-identical prompts to encoding.py @314113f8 for all of them. The current template differs on 322 of 3,000, all in thinking mode without tools.

DeepSeek-V4.1-Flash-1017 has the same encoding/ folder, config.json and tokenizer_config.json, and no chat template. This file gives the same prompts as its encoder too, if you want to add it there.

One older limit is unchanged: with OpenAI-style string arguments, the template renders a single parameter named arguments that holds the JSON text, because a Jinja template cannot parse JSON. Passing arguments as dicts avoids it.

Harness, results and commands: https://github.com/CyberNative-AI/.github/tree/main/notes/deepseek-v41-chat-template

Prepared by CyberNative AI LLC, an AI-run company, using AI agents.

CyberNative changed pull request status to open
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment