Sub topic 3
Adding Metadata and Context to Chunks
Assume you know basic Python, embeddings, vector similarity search, and the retrieve-then-generate pattern in RAG. Chunking determines what the retriever can find, while metadata helps the generator understand what it found.
Adding Metadata and Context to Chunks
A chunk should remain understandable when retrieved by itself. Store fields such as:
document_idand source URL- heading path, such as
["Installation", "Linux"] - page number, section name, or publication date
- access permissions and document version
You can also prepend a short context line to the embedded text:
def prepare_chunk(text, headings, source):
context = f"Source: {source}\nSection: {' > '.join(headings)}\n"
return {
"text": context + text,
"metadata": {"source": source, "headings": headings},
}Keep metadata used for filtering separate from text used for embedding when your vector store supports structured filters. Do not add so much repeated context that it crowds out the useful content.
Small Chunks for Search, Larger Parents for Context
Small chunks often match a focused query better. However, a small result may omit definitions or surrounding steps. Parent-child retrieval stores a searchable child chunk with a link to a larger parent section. Search the child, then pass the parent to the language model.
This improves context but increases storage and generation tokens. Use it when documents have meaningful sections and isolated snippets are frequently ambiguous.
Semantic Chunking
Semantic chunking splits where meaning changes instead of using only character or sentence counts. One approach embeds consecutive sentences and starts a new chunk when similarity drops below a threshold.
It can preserve topics better, but it requires extra embedding calls and threshold tuning. It is most useful for irregular prose. It is usually unnecessary for well-structured manuals, where headings and paragraphs already provide strong boundaries.
Measuring Chunking Quality with Recall@k
Create test questions with known relevant chunk or document IDs. Recall@k measures how many relevant items appear in the first k results:
def recall_at_k(retrieved_ids, relevant_ids, k=5):
if not relevant_ids:
return 0.0
return len(set(retrieved_ids[:k]) & set(relevant_ids)) / len(relevant_ids)Compare chunking configurations using the same questions, retriever, and k. Also inspect answer quality, because high recall can still return noisy or incomplete context.
Choosing a Strategy and Avoiding Pitfalls
Start with structure-aware or recursive splitting. Add overlap when boundaries often separate needed sentences. Consider parent-child or semantic splitting only when evaluation shows a problem.
Common pitfalls include excessive overlap, chunks that mix unrelated headings, lost table headers, code split from its explanation, and metadata that is missing or incorrect. Remove duplicates and preserve access controls during indexing.
Exercise
A retrieved paragraph answers only half of a question because its definition is in the preceding section. What should you try first?
Show answer
Add heading-aware context or use parent-child retrieval so the larger parent section accompanies the matching child.