Courses / RAG Systems / Module 2
Module 2: Document parsing: PDF layout, tables, OCR, scanned documents
12 h · theory 4 · hands-on 5 · project 2 · revision 1
You will be able to
- Choose a parser by document type (digital PDF, scanned, HTML, slides)
- Keep tables, headings and reading order intact
- Run OCR on scanned pages and check its quality
- Store page numbers and positions for citations
Capstone increment: Ingestion step that turns the corpus into structured elements with page references.
Job-description skills: Document parsing, OCR · Lab tools: docling, unstructured, pymupdf, tesseract
1. Garbage in: most RAG failures start at ingestion
When a RAG answer is wrong, look at the chunk it retrieved before you blame the model. Very often the text is broken: two columns interleaved line by line, a table flattened into a stream of numbers, the same header on every page, or a scanned page with no text at all. No embedding model or prompt fixes text that was destroyed during parsing.
Parsing turns files into typed elements (titles, paragraphs, list items, tables, figures) in reading order, each with its page number and position. Everything later depends on it: chunking needs the structure, citations need the page numbers, and permissions need to know which document each element came from.
2. Digital PDFs: reading order and tables
A PDF stores glyphs at coordinates, not paragraphs. Extracting text means rebuilding lines, blocks, columns and reading order from positions. Fast libraries like PyMuPDF give you text blocks with bounding boxes; layout-aware parsers like Docling run a layout model to label regions and a table-structure model to rebuild rows and columns. Marker and MinerU are strong alternatives, especially for academic and Chinese-language documents.
Keep tables as tables: export them to Markdown or HTML and keep the header row with every block of rows you chunk. A table row that has lost its header (“Metro | 2,500”) is unanswerable. Treat figures as elements too: keep the caption and page, and decide in Module 12 whether to describe them or retrieve the page image.
3. Scanned documents and OCR
A scanned page is an image. OCR (optical character recognition) turns it into text and boxes. Tesseract is the standard open-source engine and has trained data for Hindi, Tamil, Telugu, Bengali and many other scripts; Docling can run Tesseract, EasyOCR or RapidOCR for you. Render or scan at about 300 dpi, deskew, and pass the right language codes (for example hin+eng for bilingual government forms).
Measure OCR quality instead of trusting it. Type out the ground truth for ten representative pages and compute the character error rate (CER). Under about 2% is good for clean print; handwritten notes, stamps and low-quality photocopies can be far worse. Route pages with low engine confidence or a high garble score to human review rather than into the index.
4. Keep provenance with every element
Store each element with its document id, source URI, page number, bounding box, element type, section path (for example “Leave policy › Sick leave”) and a content hash. Citations in Module 9 link to the page, access control in Module 10 needs the document id, and incremental re-indexing in Modules 14 and 16 uses the hash to skip unchanged content.
Save the parsed output (for example Docling’s JSON) next to the raw file. Re-chunking with a new strategy then takes seconds instead of re-parsing thousands of PDFs, and you can diff parser versions when an upgrade changes results.
5. Choose a parser with your own documents
Collect 20 documents that represent your corpus: digital PDFs, scans, slide decks, spreadsheets exported to PDF, HTML pages. Parse them with two or three tools and grade each output on reading order, table fidelity, headings and speed. The best general benchmark result is no substitute for your contracts, invoices or circulars.
Add automatic quality checks to the pipeline: empty pages, pages that are mostly symbols, repeated header and footer lines, tables with no header, and documents whose text length is far below what the page count suggests. Each check should log, count and route, not silently drop.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 2.1: Parser bake-off
Parse 10 of your sample documents with Docling and with PyMuPDF. For each, record time per page and grade reading order, headings and tables from 1 to 3.
Expected: A table of scores and times per document, and a one-paragraph recommendation.
Lab 2.2: OCR a bilingual scan
Take a scanned Hindi-English form, OCR it with Tesseract (hin+eng) at 150 and 300 dpi, type the ground truth for one page and compute CER for both.
Expected: CER for each setting and a note on which errors matter for retrieval (numbers, names, dates).
Lab 2.3: Ingestion step (capstone increment)
Add src/ingest/parse.py to the capstone: parse every file in data/raw into elements with doc id, page, bbox, type, section path and hash, save JSON per document, and write a quality report.
Expected: data/parsed/*.json plus reports/parse-quality.md listing empty, garbled and table-heavy pages.
Go deeper
Videos
- What Is Docling? Transforming Unstructured Data for RAG and AI · IBM Technology · ~8 min. What Docling does (layout, tables, OCR) and why structured parsing matters for RAG.
- Why RAG Solutions Fail with Complex Documents & Vector Databases · IBM Technology · ~8 min. The failure modes complex documents cause downstream, explained without hype.
- How Docling turns documents into usable AI data · Red Hat · ~12 min. A closer look at Docling’s pipeline and output format from the team that supports it.
Further reading
- Paper Docling Technical Report · IBM Research, 2024. How a modern parser does layout analysis and table structure recognition.
- Docs Unstructured documentation · Unstructured. Element types, partitioning strategies and chunking by title.
- Docs PyMuPDF documentation · PyMuPDF. Fast text, block and table extraction from digital PDFs, with coordinates for provenance.
- Docs Tesseract documentation · Tesseract OCR. Page segmentation modes, language packs (including Indic scripts) and quality tips.
GitHub repositories
- docling-project/docling: docs/examples: conversion to JSON and Markdown, OCR options, and table export. Read the DoclingDocument data model.
- pymupdf/PyMuPDF: page.get_text('dict') and 'blocks' output: how text, spans and bounding boxes are represented.
- tesseract-ocr/tesseract: The docs on page segmentation modes (--psm) and language data; try psm 4 and 6 on forms.
- opendatalab/MinerU: Its layout and formula handling, and the benchmark comparison in the README, as a second opinion to Docling.
Checkpoint quiz
Interview questions
- How would you ingest 50,000 mixed PDFs, some scanned, into a RAG system?
- How do you handle tables in RAG? Walk through one that spans three pages.
- How would you measure parsing quality before you have any user questions?
- What metadata do you keep per chunk, and which later feature uses each field?
- A government circular is a scan in Hindi and English. What do you do differently?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).