Search Tutorials
--Ads--Building a Golden Test Set
Collect real questions and generate synthetic question-answer pairs from your documents.
A golden test set is a fixed collection of questions with known good answers and evidence, used to evaluate every version of your RAG system. It is the most valuable asset in a RAG project: with it, every change can be measured; without it, every change is a guess. This lesson shows where questions come from, what each test case should contain and how to generate synthetic cases with an LLM.
What a test case contains
{
"id": "leave-007",
"question": "Can I carry unused leave into next year?",
"reference_answer": "Yes, up to 5 unused days can be carried over to the next calendar year.",
"evidence": ["Unused leave of up to 5 days can be carried over"],
"source_docs": ["leave-policy.txt"],
"category": "hr/leave",
"type": "single_fact",
"origin": "user_logs"
}
- evidence strings drive retrieval metrics and survive re-chunking.
- reference_answer drives correctness judging.
- category and type let you report results per slice and spot weak areas.
- origin records where the case came from (real users, experts, synthetic).
Where questions come from
| Source | Strengths | Weaknesses |
|---|---|---|
| Real user questions (logs, support tickets, search queries) | Realistic wording and topics | Need answers written; may contain personal data |
| Subject-matter experts | Accurate references; know the tricky cases | Slow; experts write "textbook" questions |
| Synthetic (LLM-generated from your documents) | Fast, cheap, broad coverage | Too easy and too close to the source wording unless guided |
The best sets mix all three. Start with synthetic cases to get going, then replace and extend them with real questions as soon as you have users.
Cover the question types
- Single fact: one chunk answers it.
- Multi-part: needs two or more chunks or documents.
- Conditional: "If I joined in March, ..." requires applying a rule.
- Comparison and aggregation: "Which office has more ...".
- Unanswerable: the documents do not contain the answer. Aim for 10 to 20% of cases; they test abstention.
- Ambiguous or misspelled: as real users write them.
Generating synthetic test cases
Generate questions from chunks, but instruct the generator to write like a user (not to copy the passage's wording) and to produce the evidence and reference answer at the same time.
import json
import random
from typing import Literal
from pydantic import BaseModel
import anthropic
client = anthropic.Anthropic()
class TestCase(BaseModel):
question: str
reference_answer: str
evidence: str # an exact phrase copied from the passage
type: Literal["single_fact", "conditional", "multi_part"]
class Cases(BaseModel):
cases: list[TestCase]
PROMPT = """You are creating a test set for a company policy assistant.
From the passage below, write {n} questions an employee might really ask.
- Write casually, as an employee would; do NOT reuse distinctive phrases from the passage.
- Vary the type: simple facts, conditional situations ("if I ..."), and multi-part questions.
- For each, give a short reference answer and copy an exact evidence phrase from the passage.
<passage source="{source}">
{text}
</passage>"""
def generate_cases(chunk: dict, n: int = 2) -> list[dict]:
response = client.messages.parse(
model="claude-opus-5-5", max_tokens=3000, output_config={"effort": "medium"},
messages=[{"role": "user", "content": PROMPT.format(n=n, source=chunk["source"], text=chunk["text"])}],
output_format=Cases)
out = []
for c in response.parsed_output.cases:
if c.evidence.lower() in chunk["text"].lower(): # keep only verifiable evidence
out.append({**c.model_dump(), "evidence": [c.evidence],
"source_docs": [chunk["source"]], "origin": "synthetic"})
return out
chunks = json.load(open("index/chunks.json", encoding="utf-8"))
random.seed(7)
sample = random.sample(chunks, min(40, len(chunks))) # spread across the collection
cases = [case for chunk in sample for case in generate_cases(chunk)]
with open("golden_set.jsonl", "w", encoding="utf-8") as f:
for i, case in enumerate(cases):
f.write(json.dumps({"id": f"syn-{i:04d}", **case}) + "\n")
print(len(cases), "cases written")
Unanswerable questions
Generate questions that sound plausible for your domain but are not covered by any document, and verify that retrieval really finds nothing relevant:
UNANSWERABLE = """Here are titles of a company's policy documents: {titles}.
Write {n} realistic employee questions on related topics that these documents would NOT answer
(for example, other benefits, other countries' rules, or very specific situations).
Return one question per line."""
Give them the reference answer "The documents do not contain this information." and the type "unanswerable", so that tests can check abstention separately.
Review before you trust
Synthetic cases contain mistakes: ambiguous questions, wrong references, questions answerable from several documents. Have someone who knows the content review every case (keep, fix or delete). Reviewing 100 cases takes an hour or two and is the highest-value hour in the project.
Maintain it like code
- Keep the set in version control (JSONL is easy to diff).
- Add a case for every real failure reported by users (see Feedback Loops).
- Update references when documents change, and record the document version a case was written against.
- Keep a held-out portion you do not look at while tuning, to avoid overfitting prompts to the test set.
- Size: 50 to 100 cases to start; a few hundred covers most production systems.
Key takeaways
- A golden test set holds questions, reference answers, evidence and categories.
- Mix real, expert and synthetic questions, and cover several question types, including unanswerable ones.
- Generate synthetic cases with user-like wording and verifiable evidence, then review them by hand.
- Version the set, grow it from real failures and keep a held-out portion.