Yeda AI Tips · #044

Garbage Format In, Garbage Answers Out

You fine-tune a chat model, run inference, and get broken text sprinkled with stray tokens or run-on turns that never end. The instinct is to blame the data — more examples, cleaner examples, a different learning rate. Often the real problem is upstream of all of that: the training examples were never formatted the way the base model expects to see a conversation.

What a chat template actually is

Every chat-tuned base model was itself trained on conversations wrapped in a specific format: special tokens that mark where a message begins, who is speaking (system, user, or assistant), and where the turn ends. That format — the chat template — isn't a convention you can improvise. It's baked into how the model learned to read structure in the first place, during its own supervised fine-tuning.

If you fine-tune further without reproducing that exact structure, the model has no reliable signal for where one turn stops and the next starts. It doesn't fail loudly — it just learns a slightly wrong version of "when to stop talking" or "whose turn it is," which shows up later as run-on generations, garbled turn boundaries, or the model echoing role markers back at you as literal text.

How to apply it correctly

The fix isn't to hand-write the template yourself. Every base model ships with a way to retrieve its own chat template, usually as a single method exposed by the tokenizer that takes a list of role-tagged messages and returns the correctly formatted string — special tokens, role markers, and all. The workflow is:

  1. Load the tokenizer for your exact base model — not a similar one, the exact checkpoint you're fine-tuning.
  2. Structure each training example as a list of role/content pairs (system, user, assistant) rather than a single blob of text.
  3. Call the tokenizer's chat-template function on each example to produce the final formatted string, with the model's own special tokens in the right places.
  4. Feed the templated output into training — not your raw text.

This applies whether you're starting from a raw two-column dataset (prompt/response) or converting from another format: the templating step comes right before training, applied uniformly to every example.

tokenizer.apply_chat_template(example)

Pitfalls

The one habit that catches this early

Before you hit train, print one fully formatted example — after templating — and read it. You're checking that the special tokens are where you expect, that roles are labeled correctly, and that nothing got truncated or duplicated. This one check catches the vast majority of chat-template bugs before they cost you a training run.

Resources

Fine-tuning a model for production? Yeda AI designs, audits, and ships production LLM systems.

Talk to us · Read the blog