feat: add Qwen2.5 training chat template with generation markers#5522
Conversation
Adds qwen2_5.jinja (official Qwen2.5 ChatML template) and
qwen2_5_training.jinja (training variant with {% generation %} /
{% endgeneration %} markers for assistant-only loss masking in SFT).
Registers both in chat_template_utils.py and adds Qwen2.5 to the
TestGetTrainingChatTemplate test suite.
Built by Rudrendu Paul, developed with Claude Code
|
@codex review |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
|
Codex Review: Didn't find any major issues. 🚀 ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com>
Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 814cb0d. Configure here.

What does this PR do?
Adds a training-compatible chat template for Qwen2.5 models (e.g.
Qwen/Qwen2.5-0.5B-Instruct), following the same pattern introduced for LLaMA 3 in #5493 and GPT-OSS.Files added:
trl/chat_templates/qwen2_5.jinja— exact copy of the official Qwen2.5 ChatML tokenizer template (sourced fromQwen/Qwen2.5-0.5B-Instruct)trl/chat_templates/qwen2_5_training.jinja— training variant with{% generation %}/{% endgeneration %}markers wrapping all assistant output (both plain and tool-call branches) for assistant-only loss masking in SFTChanges to existing files:
trl/chat_template_utils.py: loads both templates, registers Qwen2.5 inget_training_chat_template(), updates docstringtests/test_chat_template_utils.py: addstrl-internal-testing/tiny-Qwen2ForCausalLM-2.5toTestGetTrainingChatTemplateThe training template handles all three assistant message cases: (1) plain text response, (2) tool calls with content, (3) tool calls without content — all wrapped in the generation markers.
Closes part of #5471 (tracking issue for training chat templates).
Before submitting
AI writing disclosure
I used AI assistance (Claude Code) to draft and implement this change.
Who can review?
@qgallouedec
Note
Low Risk
Low risk: additive template support and a small selector change, covered by existing chat-template compatibility tests. Main risk is mismatching the upstream Qwen2.5 template, which could affect prompt rendering for those models.
Overview
Adds Qwen2.5 support to
get_training_chat_template()by loading a newqwen2_5.jinjabase template and returning a newqwen2_5_training.jinjavariant that wraps all assistant output (including tool-call branches) in{% generation %}/{% endgeneration %}for assistant-only loss masking.Extends
TestGetTrainingChatTemplateto include a Qwen2.5 tiny tokenizer, ensuring the patched template remains prefix-preserving and behavior-compatible with the original rendering.Reviewed by Cursor Bugbot for commit bd28f10. Bugbot is set up for automated code reviews on this repo. Configure here.