Sub topic 4
Where cases come from
This chapter assumes you can read and write JSONL, you know the case schema from the previous chapter (id, input, reference, tags, source), and you have an LLM app that is running or about to run.
A dataset is only as useful as the inputs it contains. If the cases do not look like what users send, a high score tells you little. This chapter covers the main places to get cases and what goes wrong with each.
The four main sources
| Source | Strength | Typical weakness |
|---|---|---|
| Production logs | Real wording, real mix of requests | Skews toward common cases. Contains personal data. |
| Expert-written cases | Targets rules, edge cases, and known risks | Slow to write. Reflects the author's view of users. |
| Support tickets and bug reports | Concentrated on cases where the app failed or confused people | Over-represents failures, so the pass rate looks worse than reality. |
| Synthetic generation | Cheap, fast, fills gaps | Repetitive and too clean. Covered in a later chapter. |
Public benchmarks are a fifth option. They rarely match your task, so use them only as a sanity check, not as your dataset.
Start with production logs
If the app is live, logs are the best starting point because they show what users actually ask. Pull a random sample, not the first N rows. The first rows come from one time window and may reflect a single campaign or outage.
Before a log line becomes a case, do three things:
- Remove personal data such as emails and phone numbers.
- Deduplicate, so popular questions do not dominate.
- Record
source: "production_log"so you can later compare score by origin.
import hashlib
import json
import random
import re
EMAIL = re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+")
PHONE = re.compile(r"\+?\d[\d\s().-]{7,}\d")
def scrub(text):
text = EMAIL.sub("[EMAIL]", text)
return PHONE.sub("[PHONE]", text)
def candidates_from_logs(log_path, n, seed=0):
seen = set()
texts = []
with open(log_path, encoding="utf-8") as f:
for line in f:
if not line.strip():
continue
text = scrub(json.loads(line)["input"]).strip()
key = text.lower()
if key and key not in seen:
seen.add(key)
texts.append(text)
random.Random(seed).shuffle(texts)
cases = []
for text in texts[:n]:
digest = hashlib.sha1(text.encode("utf-8")).hexdigest()[:8]
cases.append({
"id": f"log-{digest}",
"input": text,
"reference": None,
"tags": [],
"source": "production_log",
})
return casesThe id comes from a hash of the text, so the same input always gets the same id, even if you rerun the sampling later. The reference is None because labeling is a separate step. Regex scrubbing catches only obvious patterns, so have a person skim the sample before you share it.
Fill the gaps with expert-written cases
Logs rarely contain rare or high-risk inputs. Ask a domain expert, such as a support lead or a policy owner, to write cases for the following:
- Rules with exceptions, such as "returns within 30 days, except opened software".
- Inputs the app must refuse or escalate.
- Ambiguous requests where asking a clarifying question is the right answer.
Give the expert the schema and tell them to fill in source: "expert". Ten careful cases often reveal more than a hundred random ones.
Mine failures
Tickets, thumbs-down feedback, and bug reports point to inputs that already caused trouble. Add them, but tag them (for example source: "ticket"). If you mix them in untagged, the overall score drops and you cannot tell whether the app got worse or the dataset got harder.
Common errors
- Sampling only what is easy to find. Logs hide users who gave up and never wrote a second message.
- Leaving personal data in. Cases get shared with annotators and judge models, so scrub first.
- Copying test inputs into a prompt. If you paste a log line into your prompt as a few-shot example, that case no longer measures anything. The splits chapter returns to this.
- Losing provenance. Without
source, you cannot ask whether the app does worse on tickets than on logs.
Exercise
Your logs are 90% order-status questions, but the business cares most about refund disputes, which are 2% of traffic. What should you do when sourcing cases, and which field keeps the evidence of your choice?
Show answer
Do not rely on a random log sample alone. Sample the logs, then add refund-dispute cases from tickets and ones written by an expert, so the slice has enough cases to score. Record the origin of each case in the source field (for example production_log, ticket, expert) so you can report scores by origin and explain why the mix differs from real traffic.