Sub topic 9
Preparing training data
This chapter assumes you can call a chat-style API, read Python, and know what a context window is. It also assumes you have an evaluation set and baseline from earlier in the tutorial. Fine-tuning learns only from the examples you give it, so the data sets the ceiling on quality.
What training data looks like
Fine-tuning data is a list of example conversations. Each one pairs an input with the exact output you want the model to produce. Most chat-model providers accept JSONL, which is one JSON object per line.
{"messages": [{"role": "system", "content": "You write release notes in our house style."}, {"role": "user", "content": "Fixed crash when exporting empty reports."}, {"role": "assistant", "content": "Fix: exporting an empty report no longer crashes the app."}]}The model learns to imitate the assistant message. Whatever style, length, and format those messages share, the tuned model will reproduce.
Quality over quantity
A few dozen to a few hundred clean examples often change behavior noticeably. Thousands of noisy examples teach the model the noise. Check each example against four points:
- Correct: the output is what you would accept in production.
- Consistent: similar inputs get similar outputs. If half your release notes start with "Fix:" and half with "Fixed", the model will mix them.
- Representative: inputs look like real traffic, including short, messy, and awkward cases.
- Matched to the task: the system prompt in training should match the one you use at inference time.
Do not put facts you expect the model to memorize into this data. Tuning is unreliable for storing knowledge. Use RAG for that.
Keeping the evaluation set separate
Your evaluation set measures whether tuning helped. If any of its examples leak into training, your scores will look better than the real result. Split before you do anything else, and remove duplicates across the split.
import json
import random
def load_jsonl(path):
with open(path, encoding="utf-8") as f:
return [json.loads(line) for line in f if line.strip()]
def user_text(example):
return next(m["content"] for m in example["messages"] if m["role"] == "user")
def split_data(examples, eval_fraction=0.2, seed=7):
seen = set()
unique = []
for ex in examples:
key = user_text(ex).strip().lower()
if key not in seen:
seen.add(key)
unique.append(ex)
random.Random(seed).shuffle(unique)
cut = int(len(unique) * eval_fraction)
return unique[cut:], unique[:cut] # train, eval
def validate(example):
roles = [m["role"] for m in example["messages"]]
if roles[-1] != "assistant":
return False
return all(m["content"].strip() for m in example["messages"])Run validate on every example and drop or fix failures. Then write the training split to a new file. Never edit the evaluation split after you freeze it.
A preparation workflow
Common errors
| Problem | Effect | Fix |
|---|---|---|
| Outputs written by a model and not reviewed | Tuned model copies its mistakes | Have a person review a sample of every batch |
| Inconsistent labels or style | Unstable outputs | Write a short style guide and apply it |
| Eval examples also in training | Inflated scores | Deduplicate before splitting |
| Only easy examples | Fails on messy real inputs | Add edge cases from logs |
| Personal or secret data included | The model may repeat it | Remove or mask it before training |
Exercise
Your tuned model scores 98% on your evaluation set but fails often in production. The evaluation set was built by sampling from the same file used for training, without deduplication. What is the likely cause, and how do you fix it?
Show answer
Evaluation examples leaked into training, so the model was scored on data it had already seen. Deduplicate the full set, split into train and eval before training, freeze the eval set, retrain on the train split only, and rebuild the evaluation set with fresh examples from production traffic.