All labs
Document Chunking Strategies
Implement and compare fixed-size, recursive and semantic chunking. Measure the impact on retrieval quality.
Scenario
Your RAG retrieval is poor. Before touching embeddings, experiment with chunking strategies — the #1 cause of bad RAG.
Objective
Implement three chunking strategies and measure their impact on a sample retrieval eval.
Starter code
Implement the TODOs to complete the lab.
chunking.pypython
def fixed_size_chunk(text: str, size: int = 512, overlap: int = 64) -> list[str]:
"""Split text into fixed-size chunks with overlap."""
# TODO
pass
def recursive_chunk(text: str, headers: list[str]) -> list[str]:
"""Split by markdown headers, then by size."""
# TODO
pass
def evaluate_chunking(chunks: list[str], queries: list[dict]) -> dict:
"""Measure recall@5 and context relevance for the chunking."""
# TODO
pass Solution hints
- 1Fixed-size: window with overlap
- 2Recursive: split on headers first, then size within
- 3Evaluate with the same embeddings and queries across strategies
- 4Measure both recall and chunk coherence
Validation steps
Your implementation should pass these checks.
- Fixed-size produces N chunks with given overlap
- Recursive respects markdown structure
- Eval returns recall@5 and context_relevance per strategy
- Recursive outperforms fixed-size on structured docs
Run validation
This is a simulated validation environment. In production, this would run your code against the validation steps.