Sub topic 3
Design the test case schema
This chapter assumes you can read and write JSON and JSONL in Python. It also assumes you have already written down what your app does and what counts as success. The schema turns that definition into fields you can check on each case.
Why a schema matters
A schema is the fixed list of fields every test case carries. Graders read the input and reference. Slicing reads the tags. Splitting and versioning read the id. If cases have inconsistent fields, each of those steps needs special-case code, and mistakes there are hard to spot.
One case per line
Store the dataset as JSONL, which is one JSON object per line. Git shows a changed case as a changed line, you can append without rewriting the file, and you can stream large files.
{"id": "refund-001", "input": "Can I return a laptop after 45 days?", "reference": "No. Returns are accepted within 30 days of delivery.", "tags": ["returns", "policy"], "source": "production_log", "difficulty": "easy"}The fields
| Field | Purpose |
|---|---|
| `id` | Stable, unique name for the case. Used for splits, diffs, and tracking results over time. |
| `input` | Exactly what the app receives. |
| `reference` | The expected answer, or a pointer to a rubric when no single answer exists. |
| `tags` | Slice labels such as topic or input type. You report scores per tag. |
| `source` | Provenance: `production_log`, `expert`, or `synthetic`. |
| `difficulty` | A coarse label that helps you read results. |
Some fields need extra care:
- Input: If your app takes more than one string, such as a conversation history or retrieved documents, make
inputan object with those parts. Do not flatten them into one string you have to parse later. - Reference: Keep the app's output out of the case file. Outputs change on every run, so store them in a separate results file keyed by case id.
- Source: Record provenance now. Later you may need to know whether synthetic cases score differently from real ones, and you cannot recover that information afterward.
Validate on load
Check the schema in code so a malformed case fails loudly instead of quietly skewing a score.
import json
REQUIRED = {"id": str, "input": str, "reference": str, "tags": list, "source": str}
ALLOWED_SOURCES = {"production_log", "expert", "synthetic"}
ALLOWED_TAGS = {"returns", "policy", "shipping", "adversarial"}
def validate_case(case):
errors = []
for field, kind in REQUIRED.items():
if field not in case:
errors.append(f"missing {field}")
elif not isinstance(case[field], kind):
errors.append(f"{field} must be {kind.__name__}")
if errors:
return errors
if case["source"] not in ALLOWED_SOURCES:
errors.append(f"unknown source {case['source']!r}")
if not case["tags"]:
errors.append("tags is empty")
unknown = set(case["tags"]) - ALLOWED_TAGS
if unknown:
errors.append(f"unknown tags {sorted(unknown)}")
return errors
def validate_file(path):
seen, problems = set(), []
with open(path, encoding="utf-8") as f:
for n, line in enumerate(f, 1):
if not line.strip():
continue
case = json.loads(line)
for err in validate_case(case):
problems.append(f"line {n}: {err}")
if case.get("id") in seen:
problems.append(f"line {n}: duplicate id {case['id']!r}")
seen.add(case.get("id"))
return problemsThe fixed tag list is deliberate. It catches a typo like return for returns before it splits one slice into two.
Common errors
- Ids built from line numbers. Deleting a case then renumbers the rest, and old results no longer match. Assign an id once and never reuse it.
- Free-text tags. Spelling variants make per-slice scores meaningless. Use a controlled vocabulary.
- Missing provenance. Without
source, you cannot separate real traffic from generated cases. - Too many fields. Add a field only when something reads it. Unused fields drift out of date.
Exercise
A teammate adds a case whose tags are ["Return", "policy"], and your validator rejects it. What is the most likely reason, and what should the teammate do?
Show answer
"Return" is not in the allowed tag set. The set contains "returns", and tag matching is case-sensitive. The teammate should use the existing "returns" tag. They should extend the allowed set only if a new slice is really needed.