AI Engineer Portal
Your personal operating system for career transition.
Exercise
Build a document chunking pipeline with overlap and metadata
Build a Document Chunking Pipeline with Overlap and Metadata
Before a document can be embedded and indexed for RAG, it must be split into chunks that fit within the embedding model's context window. Naive splitting on fixed character boundaries produces chunks that cut mid-sentence, lose context at chunk boundaries, and discard metadata needed for filtering. A production chunking pipeline respects sentence boundaries, adds overlap between adjacent chunks, and carries document-level metadata through to each chunk.
What to build
Implement a ChunkingPipeline class with:
-
__init__(chunk_size: int = 500, overlap: int = 50, min_chunk_size: int = 100)— configure chunk size (in characters), overlap (characters shared between adjacent chunks), and minimum chunk size (chunks smaller than this are discarded or merged with the next). -
chunk_text(text: str) -> list[str]— split text into chunks of approximatelychunk_sizecharacters. Prefer to split at sentence boundaries (.,!,?followed by whitespace) rather than mid-word. Each chunk except the first should start with the lastoverlapcharacters of the previous chunk. -
process_document(doc: dict) -> list[dict]— accept a document withid,title,body, and optionalmetadata: dict. Return a list of chunk dicts, each with:doc_id: the document'sidchunk_index: 0-based positiontext: the chunk textchar_start: character offset in the original bodymetadata: the document's metadata dict (pass through, do not modify)
-
process_batch(docs: list[dict]) -> list[dict]— process multiple documents and return a flat list of all chunks.
Why this matters
Chunk boundary placement directly affects retrieval quality. A chunk that cuts mid-sentence loses context that would have helped the embedding model represent it accurately. Overlap ensures that content near chunk boundaries appears in two adjacent chunks, reducing the chance of a relevant sentence being split across chunks with neither chunk carrying enough context.
Constraints
- If
bodyis missing or empty, return an empty list for that document. - Sentence boundary detection does not need to be perfect — a simple regex on
.!?followed by space is sufficient. - Chunks shorter than
min_chunk_sizecharacters should be dropped (not merged).
Data Transformation / medium / Step 15 of 16
Transform and summarize data safely
Think like an evaluation or trace pipeline: group clearly, preserve the data story, and make edge cases visible instead of clever.
- - Handles missing or noisy records predictably
- - Produces summary output that is easy to inspect
- - Would be safe to rerun in a script workflow
- - Do grouped metrics stay readable?
- - Would malformed rows be debuggable?
- - Did I choose names that explain the transformation?
Practice
Generate a variation
Generate a new exercise variation to deepen understanding or practice a related concept.
Attempt history
Recent submissions
Before you submit, decide what a strong answer should make obvious to the reviewer.
No attempts yet.