Lesson 2 of 5 · 30 min

Chunking and Cleaning Documents for RAG

Good chunks keep meaning together and carry the context needed to cite them.

XDQ Tutor · AI guide

Hello. In this lesson I will take you through 4 ideas, one at a time.

  1. Clean first
  2. Chunk by structure
  3. Tables and lists
  4. Re-ingest on change

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.

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.

Quick check

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.

python · illustrative
def chunk(doc):
    for section in doc.sections:            # split on headings first
        for part in split_long(section.text, max_tokens=400, overlap=50):
            yield {
                "text": f"{doc.title} > {section.heading}\n{part}",
                "doc_id": doc.id, "section": section.heading,
                "version": doc.version, "acl": doc.access_group,
            }
Quick check

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.

Quick check

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.

Key takeaways
  • 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
Try it

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.

That is the lesson.
Mark it complete to track your progress on this device.
← PreviousWhat RAG Is and When to Use ItNext lesson →Hybrid Search and Reranking for Better Retrieval