RAGEngineering Hub
Chapter 3 · concept 1 of 6 · show all
  1. 1What RAG is, and why
    1. 1.1Why models need outside knowledge, and what hallucination means
    2. 1.2What RAG is: anatomy of a request
    3. 1.3RAG vs fine tuning vs long context and prompt caching
    4. 1.4Use cases and the quality, latency and cost triangle
    5. Chapter overview
  2. 2Tokens, cost and your first RAG
    1. 2.1Tokens and context windows for RAG
    2. 2.2The RAG lifecycle and your first RAG in miniature
    3. Chapter overview
  3. 3Getting documents in
    1. 3.1Content sources and file formats
    2. 3.2Parsing, layout and OCR
    3. 3.3Tables, images and mixed content
    4. 3.4Cleaning, normalization and quality gates
    5. 3.5Metadata, document identity and versioning
    6. 3.6Updates, deletes, reindexing and ingestion pipelines
    7. Chapter overview
  4. 4Chunking
    1. 4.1Why we chunk, and the main chunking strategies
    2. 4.2Structure aware chunking and tables
    3. 4.3Chunk size, overlap and token based sizing
    4. 4.4Contextual chunking and late chunking
    5. 4.5Parent child and multi representation indexing
    6. 4.6Chunk metadata, provenance and the chunking debugging playbook
    7. Chapter overview
  5. 5Embeddings
    1. 5.1Embeddings and similarity from first principles
    2. 5.2What embeddings cannot do
    3. 5.3Choosing an embedding model: size, cost, language
    4. 5.4Embedding model migration and index rebuilds
    5. Chapter overview
  6. 6Vector stores and scores
    1. 6.1Vector databases, ANN indexes and choosing a store
    2. 6.2Score calibration and thresholds
    3. 6.3Late interaction and multivector retrieval
    4. Chapter overview
  7. 7Finding the right chunks
    1. 7.1Query understanding, rewriting and expansion
    2. 7.2Dense retrieval and keyword search with BM25
    3. 7.3Hybrid retrieval and rank fusion
    4. 7.4topK, no good match, and metadata filtering
    5. 7.5Routing, diversity, deduplication and caching
    6. 7.6Reranking: two stage, cross encoder and LLM rerankers
    7. Chapter overview
  8. 8When retrieval fails
    1. 8.1Retrieval failure analysis playbook
    2. Chapter overview
  9. 9Building the answer
    1. 9.1Context selection, packing and citations
    2. 9.2Grounding prompts and abstention
    3. 9.3Lost in the middle and context management
    4. 9.4Conflicts, staleness and freshness rules
    5. 9.5Hallucination checks and answer verification
    6. 9.6Conversational RAG, answer UX and RAG APIs
    7. Chapter overview
  10. 10Measuring
    1. 10.1Why measure, and what to measure
    2. 10.2Building a golden dataset
    3. 10.3Retrieval metrics: precision, recall, MRR, nDCG
    4. 10.4Faithfulness and answer correctness
    5. 10.5LLM as judge, and judging the judge
    6. 10.6Offline and online evals, regression tests and slices
    7. Chapter overview
  11. 11Safety and operations
    1. 11.1Prompt injection and poisoned documents
    2. 11.2Access control, privacy and governance
    3. 11.3Index lifecycle operations
    4. 11.4Observability and tracing
    5. 11.5Latency, cost, caching and model routing
    6. 11.6Reliability, degraded modes and launch readiness
    7. Chapter overview
  12. 12Scale and advanced RAG
    1. 12.1Scaling, multi tenancy and enterprise architecture
    2. 12.2Choosing an architecture: naive, advanced, agentic, graph
    3. 12.3Agentic RAG and RAG as an MCP tool
    4. 12.4GraphRAG, structured data and multimodal RAG
    5. Chapter overview

Content sources and file formats

Where RAG content comes from, what each file format keeps or loses on the way in, and why the format decides how much work ingestion takes.

A real question

My documents are PDFs, wiki pages, spreadsheets and scans. Can RAG just read them all?

Each format hides its text and structure differently. Before anything can be searched, every source has to become clean text with its structure and metadata.

The problem

Why this matters

Point a loader at the folder, and everything inside becomes searchable.
The belief

Point a loader at the folder, and everything inside becomes searchable.

Markdown and HTML say where headings and tables are. A PDF only stores letters at positions. A scan is a picture. Each needs different work, and whatever is lost here never reaches search.
What really happens

Markdown and HTML say where headings and tables are. A PDF only stores letters at positions. A scan is a picture. Each needs different work, and whatever is lost here never reaches search.

Why not the simpler way

The simple start is "point the loader at a folder". That works for clean text. Real knowledge sits in PDFs with two columns, scanned forms, wiki pages full of navigation, spreadsheets, slide decks and ticket systems. Each format hides its content differently, and whatever the loader drops never reaches search. Many "retrieval failures" are content that was never ingested properly.

The idea

The idea in plain words

Every source has to become clean text plus structure plus metadata before chunking. The format decides how hard that is. Markdown and HTML say where headings and tables are. Word files mostly do. Digital PDFs only store characters at positions, so structure has to be inferred. Scans store pixels, so even the characters have to be recognised.

It is like moving house. Labelled boxes go straight to the right room. A pile of loose items has to be sorted first, and some things get lost.

How it works

Step by step

  1. Markdown, HTML and Word files mark headings, lists and tables, so a parser can read the structure directly.
    1

    Structured text is the easy case

    Markdown, HTML and Word files mark headings, lists and tables, so a parser can read the structure directly.

  2. It stores characters at positions. Columns, headings and tables have to be worked out again.
    2

    A PDF is a page, not a document

    It stores characters at positions. Columns, headings and tables have to be worked out again.

  3. The letters must first be recognised (OCR), then the layout rebuilt. This is the most work and the most errors.
    3

    A scan is a picture

    The letters must first be recognised (OCR), then the layout rebuilt. This is the most work and the most errors.

In detail

Sort sources by how much structure they carry:

  • Structured text (markdown, HTML, wiki exports, docx): headings, lists and tables are explicit. Parse the markup.
  • Digital PDF: text exists, but reading order, columns, headers and footers and tables must be reconstructed from positions.
  • Scanned PDF and images: needs OCR first, then layout analysis.
  • Spreadsheets and databases: already structured; often better queried directly than chunked.
  • Systems (tickets, chat, CRM): need an API connector, access rules and incremental sync.

The code

Try it in Python

from pathlib import Path

LOADERS = {".md": "markdown", ".html": "html", ".docx": "docx", ".pdf": "pdf", ".png": "ocr"}

def inventory(root: str) -> dict[str, int]:
    """Count files per loader type, so you know where the parsing effort will go."""
    counts: dict[str, int] = {}
    for p in Path(root).rglob("*"):
        if p.is_file():
            kind = LOADERS.get(p.suffix.lower(), "unknown")
            counts[kind] = counts.get(kind, 0) + 1
    return counts

See it

What we measured

Measured · what the Polder corpus contains

corpus/polder, version 1, used by every lab on this site. All documents are synthetic.

English documents (markdown with frontmatter)baseline25
By source: telecom 10, policy 6, insurance 5, bank 1, other 3-25
Documents with markdown tables-2
Dutch documents (not used in Release 1)-9

Counted 2026-11-03. The corpus is deliberately clean so that the labs isolate chunking and retrieval. Parsing real PDFs is covered on RAG-08 and is not measured here.

Try it on a real example

The same part of the FiberBox manual, as two formats keep it

Format

FiberBox manual, as stored in our corpus

| Code | Name | Likely cause | First step |
| E1001 | Power unstable | Wrong or faulty adapter | Use the original 12 volt adapter |

The table structure is explicit: every value sits under a named column.

Choosing

Use it when, avoid it when

Use it when
  • An inventory of sources with format, volume, change rate and access
  • Structured exports (markdown, HTML) when a system offers them
Avoid it when
  • One generic loader for every format
  • Chunking spreadsheets that a database query would answer

What it costs · Parsing effort grows from markup to digital PDF to scans. Connectors to live systems add sync and access work.

In production

Inventory sources before building: format, volume, change rate, owner and access rules for each. Start with the source that answers the most questions with the least parsing effort. Keep the raw original next to the parsed text, so you can re-parse when your parser improves without fetching everything again.

Watch out

Common mistakes

  • A PDF is a document. It is a page layout; the document structure has to be reconstructed.
  • Ingestion is a one-off script. Sources change, so ingestion is a pipeline that runs again and again.
  • If the loader ran without errors, the content is in. Silent drops are the norm: check what came out.

Check yourself

Remember this

Remember these

  • Every source must become clean text, structure and metadata before chunking.
  • Markup formats are easy; PDFs need layout recovery; scans need OCR first.
  • Make an inventory: format, volume, how often it changes, who may see it.
  • Our course corpus is clean markdown on purpose, so the labs isolate later steps.

Say it in an interview

  1. 01Every source must become clean text, structure and metadata before chunking.
  2. 02Markup carries structure; digital PDFs need layout recovery; scans need OCR first.
  3. 03I start with an inventory: format, volume, change rate, owner and access per source.
  4. 04Spreadsheets and databases are often better queried than chunked.
  5. 05I keep the raw original so I can re-parse when the parser improves.

Quick quiz

Three questions

Question 1

Q1

Which format keeps headings and tables most reliably for chunking?

Question 2

Q2

Why did the Polder corpus use markdown with frontmatter?

Question 3

Q3 · from RAG-05

Which step of the lifecycle does the file format affect first?

Saved on this device. No account needed.