Topic 1 of 5 · 5 min read
Chat formats for training examples
Goal: Fix a training example whose roles, template or loss masking would teach the model the wrong behaviour.
A training example is one conversation written in the exact format the model will see in production, with the loss counted only on the turns you want it to learn. Get the format wrong and the model learns the wrong thing from perfectly good content.
Check yourself · 1 of 3
A fine-tuning row is stored as JSONL. What does each line hold?
Why it matters
Supervised fine-tuning (SFT) shows the model many pairs of "input, ideal output" and nudges its weights toward the outputs. The model only knows text. Roles such as system, user and assistant are turned into special tokens by the model's chat template. If your data uses a different layout from the template, the tuned model meets a format at inference time that it never trained on.
The shape of a row
Most tools, including OpenAI's fine-tuning API and Hugging Face TRL, read JSONL: one JSON object per line, each with a messages list.
{"messages": [
{"role": "system", "content": "You triage support tickets. Reply with JSON only."},
{"role": "user", "content": "My invoice shows two charges for March."},
{"role": "assistant", "content": "{\"queue\": \"billing\", \"priority\": \"normal\"}"}
]}
Tool-using models add tool turns and assistant tool calls. The same rule holds: copy the shape the model will see live.
How to do it
- Use the production system prompt. Train with the same instructions you will send later, or none if you will send none.
- Apply the model's own chat template (
tokenizer.apply_chat_template) instead of hand-building strings with### User:markers. - Mask the loss. Training on the system and user tokens teaches the model to write user messages. Most trainers can compute loss on assistant tokens only (in TRL this is the
assistant_only_lossoption). Check that your trainer and template support it. - End each assistant turn properly. The end-of-turn token must be in the target, or the model never learns to stop.
- Check ten rendered rows by eye before you spend a GPU hour.
Before and after
Weak: a row where the assistant turn contains the label and chatter, and the role is misspelled.
{"messages": [
{"role": "human", "content": "Two charges for March."},
{"role": "assistant", "content": "Sure! Here is the answer: billing"}
]}
Fixed: valid roles, the production system prompt, and only the target output the model should produce.
{"messages": [
{"role": "system", "content": "You triage support tickets. Reply with JSON only."},
{"role": "user", "content": "Two charges for March."},
{"role": "assistant", "content": "{\"queue\": \"billing\", \"priority\": \"normal\"}"}
]}
What goes wrong
- Template mismatch. Data built for one model family is fed to another, so special tokens are wrong.
- Loss on everything. The model starts imitating the user or the system prompt.
- Chatty targets. "Sure! Here is..." in the data means "Sure! Here is..." in every production answer.
- Silent truncation. Rows longer than the context limit are cut, often removing the assistant answer entirely.
Examples
Spotting a leaky row
A row has role: "human" instead of user, and the assistant turn starts with "Sure! Here is the answer:". The first would be rejected or mis-templated by most trainers. The second silently teaches a filler opening on every reply. Fix the role and strip the filler from the target.
Rendering before training
Call tokenizer.apply_chat_template(messages, tokenize=False) on three rows and read the output. You should see the model's special tokens around each turn and the end-of-turn token after the assistant answer. If you see raw ### User: text, the template was bypassed.
Key points
- Each row should look exactly like a production conversation, including the production system prompt.
- Render rows with the target model's own chat template, not hand-built strings.
- Compute loss on assistant tokens only, so the model does not learn to write user turns.
- Whatever the assistant turn contains, including filler and the end-of-turn token, is what the model will produce.
- Print and read a few fully rendered rows, and count truncated ones, before training.
Common mistakes
- Training with a different system prompt from the one used in production, so the tuned behaviour does not fire live.
- Copying a dataset made for another model family without re-rendering it through the target model's template.
- Leaving loss on system and user tokens, which spends capacity on text the model should never generate.
- Never checking how many rows exceed the context length and get truncated.
Check yourself · 2 of 3
Why mask the loss to assistant tokens during SFT?
Check yourself · 3 of 3
A tuned model keeps starting every answer with "Sure! Here is the result:". What most likely caused it?
Practice items, not counted towards your mastery.
Start here
- OpenAI: Supervised fine-tuning guide (opens in a new tab)DocsFree
How a hosted provider expects training data to be formatted, sized and evaluated.
- Hugging Face TRL: SFT Trainer (opens in a new tab)DocsFree
How an open-source trainer reads conversational data and masks the loss.
- Chat templates (Hugging Face Transformers) (opens in a new tab)DocsFree
Shows how a model's chat template turns messages into the exact training text.