RAGEngineering Hub
Chapter 3 · concept 2 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

Parsing, layout and OCR

How text, reading order and structure are recovered from PDFs and scans, the typical ways parsing goes wrong, and how to check a parser before trusting it.

A real question

Why does the text from my PDF read like nonsense?

Often the parser read straight across two columns, kept page headers, or flattened a table. Parsing has to recover the characters, their order and their structure.

The problem

Why this matters

A PDF-to-text call works: text comes out, so parsing is done.
The belief

A PDF-to-text call works: text comes out, so parsing is done.

Text coming out is not the same as text coming out right. Mixed columns, repeated footers and lost tables all still look like text.
What really happens

Text coming out is not the same as text coming out right. Mixed columns, repeated footers and lost tables all still look like text.

Why not the simpler way

The simple way is a one-line PDF to text call. On a clean single column report it works. On real documents it merges columns, repeats page headers and footers into every chunk, drops tables or flattens them into a line of numbers, and returns nothing at all for scanned pages. All of this looks like normal text to the next step, so nobody notices until answers go wrong.

The idea

The idea in plain words

Parsing has three jobs. Extract the characters (from the file, or with OCR from an image). Order them the way a person reads, across columns and around figures. Structure them into headings, paragraphs, lists and tables. Each job can fail on its own.

A parser is a transcriber reading a page aloud to someone who cannot see it. If they read across two columns, or read the page number every time, the listener gets the wrong story.

How it works

Step by step

  1. From the file, or for scans with OCR, which also gives a confidence score per word.
    1

    Extract the characters

    From the file, or for scans with OCR, which also gives a confidence score per word.

  2. Down the first column, then the second. Skip headers, footers and page numbers that repeat on every page.
    2

    Put them in reading order

    Down the first column, then the second. Skip headers, footers and page numbers that repeat on every page.

  3. Font size and position reveal headings; aligned cells reveal tables. Keep the page number for citations.
    3

    Recover the structure

    Font size and position reveal headings; aligned cells reveal tables. Keep the page number for citations.

In detail

Digital PDFs store characters with coordinates and fonts. A layout parser groups them into lines and blocks, detects columns, and uses font size and weight to guess headings. Scans first go through OCR, which outputs words with bounding boxes and a confidence score, and then the same layout step. Tables need their own detection: cell borders or aligned columns become rows and cells. Headers, footers and page numbers repeat on every page and should be removed or kept as metadata.

The code

Try it in Python

def strip_repeating_lines(pages: list[list[str]], min_share: float = 0.6) -> list[list[str]]:
    """Remove lines (headers, footers) that appear on most pages."""
    seen: dict[str, int] = {}
    for page in pages:
        for line in set(page):
            seen[line] = seen.get(line, 0) + 1
    repeated = {l for l, n in seen.items() if n >= min_share * len(pages)}
    return [[l for l in page if l not in repeated] for page in pages]

pages = [["Polder Bank Report 2025", "Revenue grew.", "Page 1"], ["Polder Bank Report 2025", "Costs fell.", "Page 2"]]
print(strip_repeating_lines(pages))  # page numbers differ, so they stay; the header goes

See it

What we measured

Not measured yet · parser comparison on real documents

Not measured yet. The Polder corpus is markdown, so it has no parsing step to test.

Not measured yet. Planned: export 3 Polder documents to PDF (one with two columns, one with the FiberBox error table, one scanned), parse them with two parsers, and compare evidence recall@3 against the markdown originals.

Try it on a real example

Two short columns from a manual page, read two ways

Read

Illustration built from real FiberBox sentences

Do not reset a router that shows a solid red power light. A reset cannot fix a hardware fault. | Contact support when any of these apply: the power light stays solid red after one restart.

Each sentence stays whole, so it can be chunked and found.

Choosing

Use it when, avoid it when

Use it when
  • A layout-aware parser for PDFs with columns and tables
  • OCR with confidence scores for scans, plus review of low-confidence pages
Avoid it when
  • Plain text extraction on complex PDFs
  • Choosing a parser from its demo instead of your hardest pages

What it costs · Layout parsing and OCR are slow and sometimes paid per page. Run them once per document version and cache the result.

In production

Choose a parser by testing it on your own documents, not on its demo. Pick 10 to 20 pages that cover your hardest cases (two columns, tables, scans, footnotes) and compare the parsed text with the page. Track OCR confidence and flag low-confidence pages for review. Store the page number and position with each block, so a citation can point to the right page.

Watch out

Common mistakes

  • If text comes out, parsing worked. Scrambled order and lost tables still produce text.
  • OCR is solved. It is good on clean scans and still makes errors on stamps, handwriting and low resolution.
  • One parser fits all formats. Test per document type.

Check yourself

Remember this

Remember these

  • Parsing has three jobs: characters, reading order, structure.
  • Common failures: mixed columns, repeated footers, flattened tables, empty scans.
  • Choose a parser by testing your own hardest pages, side by side with the original.
  • Not measured on our corpus yet: it is markdown, so it has no parsing step.

Say it in an interview

  1. 01Parsing has three jobs: extract characters, order them, and recover structure.
  2. 02Common failures are merged columns, repeated headers, flattened tables and empty scans.
  3. 03OCR gives words with positions and confidence, and I review low-confidence pages.
  4. 04I choose a parser by testing my 10 to 20 hardest pages side by side.
  5. 05I store page and position per block so citations point to the right page.

Quick quiz

Three questions

Question 1

Q1

A two column PDF is read straight across the page. What happens to the text?

Question 2

Q2

What does OCR produce?

Question 3

Q3 · from RAG-07

What should a parser check list include first?

Saved on this device. No account needed.