ParentDocumentRetriever<T>
Models & Types · AiDotNet.RetrievalAugmentedGeneration.Retrievers
Retrieves precise small chunks for matching but returns complete parent documents for comprehensive context.
Inheritance: RetrieverBase<T> → ParentDocumentRetriever<T>
For Beginners
Think of this like a two-step library search:
The Problem:
- Small chunks (paragraphs): Easy to match precisely, BUT not enough context
- Large chunks (whole pages): Plenty of context, BUT hard to match precisely
The Solution - Parent Document Retrieval:
- Search using SMALL chunks for precision
- Return the LARGE parent document for context
Real-world example: Query: “How does photosynthesis work?”
Small chunk matches: “…chlorophyll absorbs light energy…” (paragraph 3) Returns: Full page containing introduction + detailed process + diagram
var documentStore = new InMemoryDocumentStore<double>(vectorDimension: 768);
var embeddingModel = new StubEmbeddingModel<double>(embeddingDimension: 768);
// The full section, and a small chunk of it tagged with its parent's id.
var parent = new Document<double>("entanglement-section",
"Quantum entanglement links particles so that measuring one fixes the other's state, however far apart.");
var chunk = new Document<double>("entanglement-section-chunk-0",
"Measuring one entangled particle fixes the other's state.",
new Dictionary<string, object> { ["parent_id"] = "entanglement-section", ["chunk_index"] = 0 });
documentStore.Add(new VectorDocument<double>(parent, embeddingModel.Embed(parent.Content)));
documentStore.Add(new VectorDocument<double>(chunk, embeddingModel.Embed(chunk.Content)));
var retriever = new ParentDocumentRetriever<double>(
documentStore,
embeddingModel,
chunkSize: 256, // Small chunks for precision
parentSize: 2048, // Large parents for context
includeNeighboringChunks: true // Add nearby chunks too
);
var results = retriever.Retrieve("explain quantum entanglement", topK: 3);
// Finds precise paragraphs but returns full sections with complete explanation
Why use ParentDocumentRetriever:
- Best of both worlds: precise matching + complete context
- Ideal for technical documentation and research papers
- Reduces LLM hallucinations (more context = better answers)
- Works great with structured content (headings, sections, chapters)
When NOT to use it:
- Very short documents (chunks = parents already)
- Documents with redundant content (wastes context window)
- When you need ONLY the matching excerpt (use regular retrieval)
- Memory-constrained systems (returns more content per match)
How It Works
ParentDocumentRetriever solves the “chunk size dilemma” in RAG systems: small chunks enable precise matching but lack context, while large chunks provide context but reduce precision. This retriever uses a two-tier approach—search against small chunks (e.g., paragraphs) for accuracy, then return their larger parent documents (e.g., full sections or pages) for complete context. This ensures the LLM receives sufficient information to generate accurate answers while maintaining high retrieval precision. The retriever can optionally include neighboring chunks to expand context boundaries. This pattern is particularly effective for structured content (technical docs, research papers, legal documents) where individual paragraphs are meaningful but answers require broader context.
Constructors
| Constructor | Summary |
|---|---|
ParentDocumentRetriever(IDocumentStore<T> documentStore, IEmbeddingModel<T> embeddingModel, int chunkSize, int parentSize, bool includeNeighboringChunks) |
Initializes a new instance of the ParentDocumentRetriever class. |