Accept consecutive leading system messages in chat template

#10
by jlapenna - opened

Same template fix as https://huggingface.co/Qwen/Qwen3.8-Flash-Next/discussions/76, applied to this repo's copy, which is byte-identical to the base template and is what vllm serve nvidia/Qwen3.8-Flash-Next-NVFP4 renders (a base-repo fix does not reach it).

Evidence and repro: https://huggingface.co/Qwen/Qwen3.8-Flash-Next/discussions/75

Fixes https://huggingface.co/Qwen/Qwen3.8-Flash-Next/discussions/75: the template rejects consecutive leading system messages with System message must be at the beginning.

Change

chat_template.jinja, with the chat_template string in tokenizer_config.json updated to the identical text:

  • The template now collects the contiguous run of system messages at the start of messages. Each one is trimmed as before, empty ones are skipped, and the rest are joined with "\n\n".
  • Both the tools and no-tools branches render that text in the single system block where messages[0]'s content used to go.
  • A system message is rejected only when it appears after a non-system message. Before this change, any system message after index 0 was rejected.

Unchanged

  • Requests with no system message or one system message render byte-identically, with or without tools.
  • The tool-definition block and the reasoning-effort preamble do not change.
  • A system message after a user, assistant or tool message is still rejected. This PR does not decide that question.
  • The rest of tokenizer_config.json is unchanged (the diff is one line).

Verification

  • repro_leading_system.py (in https://huggingface.co/Qwen/Qwen3.8-Flash-Next/discussions/75): [system, system, user] rendered with the original template raises TemplateError. With the fixed template it renders one system block containing both texts, with and without tools. Six previously handled conversation shapes (no system, one system, empty system, list content, a tool-call turn, and a system message after a user message), each with and without tools, render identically before and after.
  • transformers 5.20.1 apply_chat_template returns the same result. The original raises, and the fixed template renders exactly one <|im_start|>system.

šŸ¤– Generated with Claude Code

https://claude.ai/code/session_01Knj2s56UENx8AitLLRDr6T

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

Sign up or log in to comment