Define the role of a chat template when fine-tuning on (system, user, assistant) data
A chat template is the model-specific formatter that wraps each turn in role-marker tokens so the model sees the same turn boundaries at training and inference.
Imagine you write a script for a play, but the actor cannot tell who is supposed to speak each line because the script just has names jammed against the dialogue. So you stamp special brackets around every speaker tag and every line ending. Now the actor reads cleanly, knows when one character stops and another starts, and stops at the right moment. The chat template is the rubber stamp that adds those brackets. Different theatre companies use different stamps, and an actor trained with one set of brackets gets confused if you suddenly hand them a script stamped with another set. So whichever model you are training, you have to use the same stamp pattern the model was originally taught to read.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: define the dictionaries to string job, walk through three concrete template formats, then cover the EOS and loss-masking traps that catch most teams.
Real products, models, and research that use this idea.
- Hugging Face TRL's SFTTrainer calls tokenizer.apply_chat_template on every example to render the conversation into the model's exact template format.
- Llama 4 Maverick fine-tunes use the Llama-3 style header tokens `<|start_header_id|>` and `<|eot_id|>` inherited from the Llama-3 family lineage.
- Qwen 3.5 fine-tunes use ChatML markers `<|im_start|>` and `<|im_end|>` because Qwen was instruction-tuned with the ChatML convention.
- Mistral and Mixtral-derived fine-tunes still use the older `[INST]` and `[/INST]` markers from the Mistral instruct lineage.
- Axolotl and Unsloth both expose chat_template config keys so the user explicitly chooses the template rather than silently inheriting the tokenizer default.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy are role-marker tokens implemented as single special-token IDs rather than plain text strings?
QHow does the chat template interact with loss masking during supervised fine-tuning?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Using the tokenizer's default chat template without checking whether it actually matches the base model you are fine-tuning, then wondering why generations never stop cleanly.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.