Topic 4
When the template is wrong
A wrong template rarely crashes anything. AI Engineering (Ch. 5, "System Prompt and User Prompt") warns that template errors "cause silent failures—the model will do something reasonable even if the template is wrong", and that even "an extra new line" can significantly change behaviour.
The reason: a fine-tuned model has seen its markers in exactly one arrangement. Anything else is a sequence it was never trained on, even when it reads the same to you.
Predict first
Same chat, slightly wrong template
Token-by-token comparison with Llama 3's format · deterministic
One extra character. The reference format has a blank line (↵↵) after every header; three newlines (↵↵↵) are a different token.
<|begin_of_text|><|start_header_id|>system<|end_header_id|>↵ ↵ ↵ Translate the text below into French<|eot_id|><|start_header_id|>user<|end_header_id|>↵ ↵ How are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|>↵ ↵
Loading the tokenizer…
What the lab shows, for the two-message chat:
- Extra newline: same 25 tokens, but token #4 is a different ID.
- No assistant header: 21 tokens ending on the user's turn. Hugging Face's guide says the model may then continue the user's message instead of replying.
- Begin-of-text twice: 26 tokens. The guide warns that adding special tokens again on top of a template is "often incorrect or duplicated, hurting model performance".
- Another model's template: Zephyr's markers mean nothing to Llama 3.
The fix is mechanical: use the template that ships with the model (in Hugging Face, apply_chat_template), and test the token IDs, not just the printed string.
Quick check