Chunking and Cleaning Documents for RAG
Good chunks keep meaning together and carry the context needed to cite them.
Hello. In this lesson I will take you through 4 ideas, one at a time.
Why it matters: Good chunks keep meaning together and carry the context needed to cite them. After each idea I will ask you one quick question.
In this lessonFollows you as you read
Clean first
The short version: Extract text carefully: keep headings, lists and tables, remove repeated headers, footers and page numbers, and record where each piece came from.
Extract text carefully: keep headings, lists and tables, remove repeated headers, footers and page numbers, and record where each piece came from. Scanned PDFs need OCR and a quality check. Duplicate and outdated versions should be removed or marked, or retrieval will surface old policies.
Keep metadata with every document: title, version, date, owner, access group and source link.
Why prefix chunks with title and section?
Chunk by structure
The short version: Split documents along their own structure, such as sections and subsections, rather than fixed character counts.
Split documents along their own structure, such as sections and subsections, rather than fixed character counts. A chunk should hold one idea with enough context to stand alone. Very short chunks lose meaning; very long ones dilute relevance and waste context space.
Prefix each chunk with its document title and section heading, so a chunk about notice periods still says which contract and which clause it comes from.
What should happen to old versions of a document?
Tables and lists
The short version: Tables are common in business documents and easy to break.
Tables are common in business documents and easy to break. Keep a table with its caption and header row; for large tables, chunk by row groups and repeat the header in each chunk. Convert simple tables to text lines such as plan: Growth; price: 150 USD; users: 10, which models read more reliably.
Lists should stay with the sentence that introduces them.
How should large tables be chunked?
Re-ingest on change
The short version: Documents change.
Documents change. Track versions and re-ingest changed documents automatically, removing old chunks so retrieval cannot return superseded text. Show the version and date in citations so users can see how current the source is.
Log ingestion errors and review them; a silently failed PDF is a gap users will notice before you do.
- Clean text and keep metadata with every document
- Chunk along headings and keep one idea per chunk
- Prefix chunks with title and section
- Keep tables with their headers
- Re-ingest on change and remove old chunks
Take ten real documents, clean and chunk them by structure with title and section prefixes, and inspect 20 random chunks to check that each makes sense on its own.
Mark it complete to track your progress on this device.