Test Dataset Generators

AI Evaluation & Safety advanced Building with AI 12 min read

Overview

Evaluating a RAG pipeline requires a dataset of questions, expected answers, and the context chunks that should be retrieved. Manually creating hundreds of these question-context-answer (QCA) triples is prohibitively expensive—often taking weeks of domain expert time. Test dataset generators solve this by automatically synthesizing diverse, high-quality evaluation datasets directly from the raw corpus that your RAG system indexes.

The core idea is elegantly simple: run RAG in reverse. Instead of starting with a question and retrieving relevant documents, you start with documents and generate questions that those documents can answer. An LLM reads each chunk, produces questions of varying difficulty, generates ground-truth answers grounded in the source text, and extracts the specific passages that support each answer.

This approach can reduce developer time in the evaluation data creation process by up to 90%, while producing datasets that cover edge cases and question types that humans often overlook. Synthetic test datasets have become the standard way to evaluate RAG systems at scale, used by frameworks like RAGAS, DeepEval, and LangSmith.

How It Works

The Generation Pipeline

Test dataset generation follows a structured pipeline that transforms raw documents into evaluation-ready QCA triples:

  1. Document Loading: Ingest the same corpus your RAG system indexes—PDFs, web pages, internal wikis, etc.
  2. Knowledge Graph Construction: Parse documents into nodes and relationships, identifying key entities, concepts, and their connections.
  3. Question Seed Generation: An LLM reads each document chunk and generates initial “simple” questions that can be answered from that chunk alone.
  4. Question Evolution: Seed questions are systematically transformed into harder variants through reasoning enhancement, conditioning, and multi-context synthesis.
  5. Answer Generation: A separate LLM call generates ground-truth answers, explicitly grounded in the source passages.
  6. Quality Filtering: Critique agents or automated checks filter out low-quality, ambiguous, or unanswerable questions.
Key Concept

Synthetic test generation is essentially “RAG in reverse”: instead of question → retrieval → answer, you go document → question generation → answer extraction. This ensures every test case has a verifiable ground truth anchored in your actual corpus.

Question Evolution Strategies

Simple questions generated directly from a single chunk are useful but insufficient—they don’t test the hard cases where RAG systems actually fail. Evolution strategies systematically increase question difficulty:

RAGAS allows you to specify the distribution of question types. A typical evaluation dataset might target 40% simple, 20% reasoning, 20% conditioning, and 20% multi-context questions:

from ragas.testset import TestsetGenerator
from ragas.llms import LangchainLLMWrapper
from langchain_openai import ChatOpenAI

# Configure generator with LLM
generator_llm = LangchainLLMWrapper(ChatOpenAI(model="gpt-4"))
generator = TestsetGenerator(llm=generator_llm)

# Load your documents (same corpus as your RAG system)
documents = load_documents_from_directory("./knowledge_base/")

# Generate testset with specified distribution
testset = generator.generate_with_langchain_docs(
    documents=documents,
    testset_size=100,
    distributions={
        "simple": 0.4,
        "reasoning": 0.2,
        "conditioning": 0.2,
        "multi_context": 0.2
    }
)

# Export as pandas DataFrame
df = testset.to_pandas()
print(f"Generated {len(df)} test cases")
print(df[["question", "ground_truth", "evolution_type"]].head())

Knowledge Graph-Based Generation

Modern generators like RAGAS v0.2+ use a two-phase approach. First, they build a knowledge graph from the documents, identifying entities, relationships, and thematic clusters. Then, questions are generated by traversing this graph—simple questions come from single nodes, while multi-context questions are generated by connecting nodes across different document sections. This produces more diverse and realistic questions than pure chunk-based generation.

Avoiding Self-Enhancement Bias

A critical pitfall in synthetic dataset generation is self-enhancement bias: if you use the same LLM to generate test questions and to run your RAG pipeline, the evaluation becomes circular. The model may score well simply because it generated questions in its own “style” rather than because the RAG system truly works well.

Self-Enhancement Bias

Always use a different LLM for test dataset generation than the one powering your RAG pipeline’s generation step. For example, generate test data with GPT-4 but run your RAG pipeline with Claude, or vice versa. This prevents the evaluation from being contaminated by model-specific biases.

Quality Assurance Mechanisms

Raw synthetic datasets contain noise. Multiple quality assurance layers are used to clean them:

Practical Applications

Cold-Start Evaluation

When building a new RAG system, you have no historical user queries to test against. Synthetic generation creates an initial evaluation dataset within hours rather than weeks, letting you benchmark retrieval quality before the system goes live. This is the most common use case—nearly every RAG project starts with synthetic test data.

Regression Testing

As you update your chunking strategy, embedding model, or reranker, you need to verify that retrieval quality hasn’t degraded. A stable synthetic test dataset serves as a regression suite—run it after every pipeline change and compare RAGAS scores. Teams typically maintain datasets of 200–500 QCA triples that cover the most critical document types in their corpus.

Edge Case Coverage

Synthetic generators excel at producing question types that humans rarely write manually: multi-hop questions spanning distant document sections, negation questions (“Which features does the product NOT support?”), and temporal questions (“What changed between version 2.0 and 3.0?”). These edge cases are where RAG systems most commonly fail in production.

A/B Testing Retrieval Strategies

When comparing hybrid search vs. pure semantic search, or evaluating different reranker models, a consistent synthetic test dataset provides an apples-to-apples comparison. Generate the dataset once, then run both pipeline variants against it and compare context precision, recall, and faithfulness scores.

Key Takeaways

Further Reading