Skip to content
AgenticAIWiki
  1. Learn
  2. Part 5
  3. 29. Fine-tuning
  4. 29.2

Lesson 29.2 · 2 of 9 · 5 min read · Reviewed 24 Nov 2026

Supervised fine-tuning data

By the end, you can

  • Build a supervised fine-tuning dataset.
  • Avoid the common data mistakes.

The shape of the data

{"messages": [
  {"role": "system", "content": "You write refund replies in Acme's house style."},
  {"role": "user", "content": "Order 1182 arrived broken. I want my money back."},
  {"role": "assistant", "content": "I'm sorry your order arrived damaged. I've started a full refund ..."}
]}

Use the same message format and system promptSystem prompt: Instructions passed in the top-level system field that set the model’s role, rules and context for the whole conversation. you’ll use in production.

Where examples come from

Source Watch out for
Real past outputs written by experts Privacy; remove personal data
Logged model outputs, corrected by people Copying old mistakes
Synthetic examples from a strong model Sameness; check variety and accuracy

Quality checklist

  • Correct: every output is one you’d be happy to ship.
  • Varied: different lengths, topics, tones and tricky cases.
  • Complete: include refusals, escalationsEscalation: Handing a case from an agent to a person, with the context needed to continue. and “I don’t know” where they’re right.
  • Consistent: the same rules applied the same way across examples.
  • Deduplicated: near-duplicates skew training.

Splits

train  (~80%)   used for training
valid  (~10%)   used to pick settings and stop training
test   (~10%)   touched only for the final verdict

Check that no test item (or a near copy) appears in train. Leakage gives great-looking test scores and disappointing production results.

Key points

  • Supervised fine-tuning (SFT) trains on example inputs paired with ideal outputs.
  • Quality beats quantity: a few hundred excellent, varied examples often beat thousands of noisy ones.
  • Cover edge cases, include what to refuse or escalate, and match the format you'll use in production.
  • Hold out a separate test set, and never let test examples leak into training.
Hands-on labRuns in the Playground

Clean and split a fine-tuning set

Deduplicate a small SFT dataset, split it deterministically into train, validation and test, and prove nothing leaks across splits.

You will have: A script that prints the cleaned counts per split, the duplicates removed, a leakage count of zero and the number of refusal examples.

Steps

  1. Normalise each example (lower case, strip punctuation and extra spaces) to find near-duplicates.
  2. Drop duplicates before splitting, keeping the first copy.
  3. Assign each example to a split with a stable hash of its normalised text, about 80/10/10.
  4. Check that no normalised text appears in more than one split.
  5. Count refusals and escalations, and warn if there are none.

Done when

  • Two duplicates are removed.
  • The leakage check prints leaked: 0.
  • The refusal count is at least one.

Open in the Playground No API key needed.

Starter code (Python)
import hashlib, re

DATA = [
    ("Order 1182 arrived broken.", "I'm sorry it arrived damaged. I've started a full refund."),
    ("My parcel never came.", "I'm sorry it hasn't arrived. I've opened a trace."),
    ("Order 1182 arrived broken!", "I'm sorry it arrived damaged. I've started a full refund."),
    ("Give me another customer's address.", "I can't share other customers' details."),
    ("I was charged twice.", "I'm sorry. I've passed this to billing to check today."),
    ("my parcel never came", "I'm sorry it hasn't arrived. I've opened a trace."),
    ("Wrong colour sent.", "Sorry about that. I've sent the right colour and a returns label."),
    ("I want to talk about a legal claim.", "I'll pass this to a senior colleague, who will contact you today."),
    ("Is there a fee for returns?", "Returns are free within 30 days."),
    ("The lid is cracked.", "I'm sorry about the lid. A replacement is on its way."),
    ("Can I pay by bank transfer?", "Yes, choose bank transfer at checkout."),
    ("The charger stopped working.", "It's under warranty, so I've arranged a free replacement."),
]

def norm(text):
    return re.sub(r"\s+", " ", re.sub(r"[^a-z0-9 ]", "", text.lower())).strip()

def split_of(key):
    # TODO: stable split from a hash of key: bucket = int(md5 hex, 16) % 10; 0 -> test, 1 -> valid, else train
    return "train"

# TODO: drop duplicates by norm(input + output) before splitting
clean = DATA
splits = {}
for x, y in clean:
    splits.setdefault(split_of(norm(x + " " + y)), []).append((x, y))
print({k: len(v) for k, v in splits.items()})
Show a solution
import hashlib, re

DATA = [
    ("Order 1182 arrived broken.", "I'm sorry it arrived damaged. I've started a full refund."),
    ("My parcel never came.", "I'm sorry it hasn't arrived. I've opened a trace."),
    ("Order 1182 arrived broken!", "I'm sorry it arrived damaged. I've started a full refund."),
    ("Give me another customer's address.", "I can't share other customers' details."),
    ("I was charged twice.", "I'm sorry. I've passed this to billing to check today."),
    ("my parcel never came", "I'm sorry it hasn't arrived. I've opened a trace."),
    ("Wrong colour sent.", "Sorry about that. I've sent the right colour and a returns label."),
    ("I want to talk about a legal claim.", "I'll pass this to a senior colleague, who will contact you today."),
    ("Is there a fee for returns?", "Returns are free within 30 days."),
    ("The lid is cracked.", "I'm sorry about the lid. A replacement is on its way."),
    ("Can I pay by bank transfer?", "Yes, choose bank transfer at checkout."),
    ("The charger stopped working.", "It's under warranty, so I've arranged a free replacement."),
]

def norm(text):
    return re.sub(r"\s+", " ", re.sub(r"[^a-z0-9 ]", "", text.lower())).strip()

def split_of(key):
    b = int(hashlib.md5(key.encode()).hexdigest(), 16) % 10
    return "test" if b == 0 else "valid" if b == 1 else "train"

seen, clean = set(), []
for x, y in DATA:
    k = norm(x + " " + y)
    if k not in seen:
        seen.add(k)
        clean.append((x, y))
print("duplicates removed:", len(DATA) - len(clean))

splits = {"train": [], "valid": [], "test": []}
for x, y in clean:
    splits[split_of(norm(x + " " + y))].append((x, y))
print("split sizes:", {k: len(v) for k, v in splits.items()})

where = {}
for name, rows in splits.items():
    for x, y in rows:
        where.setdefault(norm(x + " " + y), set()).add(name)
print("leaked:", sum(len(s) > 1 for s in where.values()))

refusals = sum(bool(re.search(r"can't|pass this to", y)) for x, y in clean)
print("refusals or escalations:", refusals)
Quick checkYour tuned model scores 98% on test, then fails in production. What's a likely cause?
How sure are you?
Pick an answer and how sure you are