Lesson 5.2 · 2 of 7 · 4 min read · Reviewed 24 Nov 2026
Supervised fine-tuning
By the end, you can
- Describe supervised fine-tuning.
- Explain how a model learns to follow instructions.
The data
Each example is a conversation with a model-quality answer:
user: Summarise this email in two sentences: ...
assistant: The supplier will deliver on Friday. They need a signed PO by Wednesday.
Thousands to millions of these, covering many tasks: answering, writing, coding, refusing unsafe requests, asking clarifying questions.
What changes
| Before SFT | After SFT |
|---|---|
| Continues any text | Responds to the user’s request |
| No consistent format | Follows the assistant turn structure |
| May ramble or switch topics | Stays on task |
Why it’s not enough
SFTSupervised fine-tuning (SFT): Fine-tuning on pairs of inputs and ideal outputs. shows the model good answers, but it can only copy patterns in the examples. It’s hard to write examples for every situation, and the model doesn’t learn which of two plausible answers people prefer. Reinforcement learning from feedback (5.3) addresses that.
Fine-tuning you can do
Model providers do this stage for their assistants. Builders can also fine-tuneFine-tuning: Continuing to train a model on your own examples to change its behaviour or specialise it. some models for their own tasks, which is a different, smaller job (chapter 29).
Key points
- Supervised fine-tuning (SFT) trains a pretrained model on example conversations: a prompt and a good response.
- This teaches the assistant format: answer the question, follow instructions, use a helpful tone.
- SFT data is much smaller than pretraining data, but carefully written or selected.
Validate a batch of SFT examples
Catch the formatting and quality problems that quietly spoil a supervised fine-tuning set.
You will have: A validator that checks every example and prints a pass or fail line with reasons, plus a summary count.
Steps
- Run the starter: every example is marked as passing.
- Check that roles alternate user, assistant, user, assistant, starting with user.
- Check that the last turn is the assistant's and no turn is empty.
- Flag duplicate prompts and assistant replies longer than 120 words.
- Print one line per example and a final count of usable examples.
Done when
- ex-1 and ex-4 pass.
- ex-2 fails for ending on a user turn, ex-3 for an empty reply, ex-5 as a duplicate.
- The script prints 'usable: 2 of 5'.
Open in the Playground No API key needed.
Starter code (Python)
EXAMPLES = [
{"id": "ex-1", "turns": [("user", "Summarise: the meeting moved to Friday."), ("assistant", "The meeting is now on Friday.")]},
{"id": "ex-2", "turns": [("user", "Translate 'hello' to French."), ("assistant", "Bonjour."), ("user", "And Spanish?")]},
{"id": "ex-3", "turns": [("user", "List three prime numbers."), ("assistant", "")]},
{"id": "ex-4", "turns": [("user", "Is 17 prime?"), ("assistant", "Yes. 17 has no divisors other than 1 and itself.")]},
{"id": "ex-5", "turns": [("user", "Summarise: the meeting moved to Friday."), ("assistant", "Friday is the new date.")]},
]
def problems(ex, seen_prompts):
found = []
# TODO: roles must alternate starting with "user"
# TODO: the last turn must be "assistant"; no turn may be empty
# TODO: flag a first prompt already in seen_prompts as "duplicate prompt"
# TODO: flag assistant turns over 120 words as "reply too long"
return found
seen = set()
usable = 0
for ex in EXAMPLES:
found = problems(ex, seen)
seen.add(ex["turns"][0][1])
usable += not found
print(ex["id"], "PASS" if not found else "FAIL " + "; ".join(found))
print(f"usable: {usable} of {len(EXAMPLES)}")
Show a solution
EXAMPLES = [
{"id": "ex-1", "turns": [("user", "Summarise: the meeting moved to Friday."), ("assistant", "The meeting is now on Friday.")]},
{"id": "ex-2", "turns": [("user", "Translate 'hello' to French."), ("assistant", "Bonjour."), ("user", "And Spanish?")]},
{"id": "ex-3", "turns": [("user", "List three prime numbers."), ("assistant", "")]},
{"id": "ex-4", "turns": [("user", "Is 17 prime?"), ("assistant", "Yes. 17 has no divisors other than 1 and itself.")]},
{"id": "ex-5", "turns": [("user", "Summarise: the meeting moved to Friday."), ("assistant", "Friday is the new date.")]},
]
def problems(ex, seen_prompts):
found = []
turns = ex["turns"]
for i, (role, _) in enumerate(turns):
if role != ("user" if i % 2 == 0 else "assistant"):
found.append(f"turn {i} should not be {role}")
if turns[-1][0] != "assistant":
found.append("ends on a user turn")
if any(not text.strip() for _, text in turns):
found.append("empty turn")
if turns[0][1] in seen_prompts:
found.append("duplicate prompt")
if any(role == "assistant" and len(text.split()) > 120 for role, text in turns):
found.append("reply too long")
return found
seen = set()
usable = 0
for ex in EXAMPLES:
found = problems(ex, seen)
seen.add(ex["turns"][0][1])
usable += not found
print(ex["id"], "PASS" if not found else "FAIL " + "; ".join(found))
print(f"usable: {usable} of {len(EXAMPLES)}")