Sub topic 6
Generating synthetic cases safely
This chapter assumes you have read the chapters on the case schema, data sources, and coverage slicing. You should be comfortable with JSONL records and with calling an LLM to produce text.
Why synthetic cases need care
Synthetic cases are test inputs written by a model instead of taken from real users. They are cheap, and they can fill slices that production logs rarely reach, such as rare policy edge cases or adversarial phrasing.
They also fail in predictable ways:
- Narrow style. A model tends to write polite, well-formed, similar-sounding inputs. Real users are messier.
- Wrong references. The generator may invent a reference answer that is plausible but incorrect.
- Self-preference. If the same model generates the cases and runs the app, scores can look better than they are.
- Near-duplicates. Many generated cases differ only in wording and add no coverage.
Use synthetic data to fill gaps
Start from your slice table and find slices with too few real cases. Ask the generator for cases in those slices only. Give it a few real examples, the slice description, and the output format. Do not ask for "100 test cases" with no direction.
| Situation | Synthetic generation helps? |
|---|---|
| Slice with 3 real cases and a clear definition | Yes, as a draft for review |
| Common inputs already well covered by logs | No, use real cases |
| Adversarial or malformed inputs | Yes, with varied instructions |
| Final test set used to compare releases | Only after human review |
A safe generation pipeline
The code below handles the mechanical part: dropping duplicates and recording provenance. It does not call a model. The candidates list stands in for model output.
import hashlib
def normalize(text):
return " ".join(text.lower().split())
def accept_synthetic(candidates, existing_cases, generator, slice_tag):
seen = {normalize(c["input"]) for c in existing_cases}
accepted = []
for cand in candidates:
key = normalize(cand["input"])
if key in seen:
continue
seen.add(key)
digest = hashlib.sha256(key.encode("utf-8")).hexdigest()[:8]
accepted.append({
"id": f"syn-{slice_tag}-{digest}",
"input": cand["input"],
"reference": cand["reference"],
"tags": [slice_tag, "synthetic"],
"source": "synthetic",
"generator": generator,
"reviewed": False,
})
return accepted
existing = [{"input": "Can I return a laptop after 45 days?"}]
candidates = [
{"input": "can I return a laptop after 45 days? ", "reference": "No."},
{"input": "Can I return a gift without a receipt?", "reference": "Yes, for store credit."},
]
for case in accept_synthetic(candidates, existing, "model-x-2025", "returns"):
print(case["id"], case["reviewed"])The first candidate is dropped because it matches an existing case after normalization. The second keeps reviewed: False until a person checks it.
Exact matching after normalization catches only trivial copies. For larger batches, add an embedding similarity check and drop candidates above a threshold you choose by inspecting examples.
Rules that keep the dataset trustworthy
- Record provenance. Set
sourcetosyntheticand store the generator name. You can then report scores for real and synthetic cases separately. - Review before use. A human should check every reference. Unreviewed cases should not count toward reported scores.
- Use a different model family for generation when possible. This reduces self-preference.
- Keep the test split mostly real. Use synthetic cases for dev work, and add them to test only after review.
- Vary the prompts. Change persona, tone, length, and error types between batches to avoid a uniform style.
Exercise
A teammate generates 500 synthetic cases with the same model that powers the app, skips review, and reports 97% accuracy on them. Name two problems with this number and one fix for each.
Show answer
First, the generator and the app share the same biases, so the cases are easier for the app than real traffic is. Fix this by generating with a different model and mixing in real cases. Second, unreviewed references may be wrong, so the score is unreliable. Fix this by having a human check each reference and by counting only reviewed cases.