Sub topic 3
Building an evaluation dataset
This chapter assumes you can write basic Python, have called an LLM through an SDK, and have already written down success criteria for your application. The dataset turns those criteria into concrete cases that you can run again and again.
What a case contains
A case is one input plus what you need to judge the output. Keep the format plain so any scorer can read it. A good case has these fields:
id: a stable identifier, so you can compare results across versions.input: exactly what the application receives, including any context or user metadata.expected: a reference answer, or a short grading criterion when many answers are valid.tags: labels such asrefund,long_input, oradversarial, so you can report scores per slice.
Store cases as JSON Lines, with one JSON object per line. The files diff cleanly in version control.
Where cases come from
Use several sources and record which one each case came from:
- Production logs. Real user inputs are the most representative. Remove personal data before saving them.
- Hand-written cases. Write these for important behaviors that logs rarely show, such as refusals or rare formats.
- Failures. Every bug report becomes a case, so the same bug is caught next time.
- Synthetic cases. An LLM can generate variations of existing cases. Review them by hand, because generated cases tend to be easier and more uniform than real traffic.
How many cases, and how varied
Start with 30 to 100 cases. That is enough to find large problems, and small enough to label carefully. Coverage matters more than count. Include typical inputs, edge cases (empty, very long, mixed languages), and inputs the application should decline. Tag each case, then check that no important slice has only one or two examples. Small slices produce noisy scores, which a later chapter covers.
Keeping the dataset clean
Check the file mechanically each time you load it. Duplicate ids make results ambiguous. Near-identical inputs inflate your score on one behavior.
import json
REQUIRED = {"id", "input", "expected", "tags"}
def load_cases(path):
cases, seen_ids, seen_inputs = [], set(), set()
with open(path, encoding="utf-8") as f:
for line_no, line in enumerate(f, start=1):
if not line.strip():
continue
case = json.loads(line)
missing = REQUIRED - case.keys()
if missing:
raise ValueError(f"line {line_no}: missing {sorted(missing)}")
if case["id"] in seen_ids:
raise ValueError(f"line {line_no}: duplicate id {case['id']}")
key = case["input"].strip().lower()
if key in seen_inputs:
raise ValueError(f"line {line_no}: duplicate input")
seen_ids.add(case["id"])
seen_inputs.add(key)
cases.append(case)
return casesAlso hold out part of the dataset. If you tune prompts against every case, your score measures how well you fitted those cases, not how well the application generalizes. Tune on the development set and check the held-out set only occasionally.
Common mistakes
- Writing expected answers by running the current application and accepting its output. This bakes in existing errors.
- Letting one person label everything without a second review of the ambiguous cases.
- Changing cases silently. Version the file, and note changes so scores from different versions are not compared by mistake.
Exercise
Your dataset has 200 cases, 190 of them simple questions and 10 tagged adversarial. The overall pass rate is 94%. Why can this number mislead, and what two steps would you take?
Show answer
The 10 adversarial cases barely affect the overall rate, so a poor result on them is hidden. Report pass rate per tag, and add more adversarial cases so that slice is large enough to give a stable score.