Document processing for RAG: parsing, OCR and structure-aware chunks
Most RAG failures start before retrieval: PDFs that extract as soup, scanned pages with no text, tables split in half. The parsing pipeline (text extraction, OCR, layout and table detection, vision-language parsing), choosing tools, structure-aware chunking with heading paths and table-row sentences, metadata, and a runnable comparison.
By Sandip Maity, Applied AI Engineer
Retrieval can only find what parsing produced. If a PDF extracts as a jumble of columns, a scanned contract has no text at all, or a pricing table is split across three chunks without its header, no embedding model or reranker will save you. This lesson covers the pipeline from raw files to good chunks, and shows with runnable code how much structure-aware chunking helps on a table question.
#The pipeline
- 1FilesPDF, DOCX, HTML, slides, scans, email
- 2Extracttext layer, or OCR for images
- 3Layoutreading order, headings, tables, figures
- 4NormaliseMarkdown or JSON with structure kept
- 5Chunkby structure, with heading paths
- 6Enrichmetadata, table-row sentences, captions
- 7Indexembeddings plus keyword index
#Step 1: extract text, or detect that there is none
- Digital PDFs and Office files have a text layer. Extract it with a PDF or document library, keeping positions if the tool offers them.
- Scanned PDFs and images have no text layer. Detect them (very few characters per page) and send them to OCR.
- HTML is already structured; strip navigation, scripts and boilerplate, keep headings, lists and tables.
Check a sample of every source type by eye before indexing thousands of files. Common surprises: two-column layouts read across columns, headers and footers repeated on every page, hyphenated line breaks, and ligatures that break search.
#Step 2: recover layout and tables
Reading order and tables are where simple extractors fail. Your options, from cheapest to most capable:
| Approach | Good at | Weak at |
|---|---|---|
| Text-layer extraction with heuristics | Clean single-column digital documents | Columns, tables, scans |
| OCR engines | Scanned text | Tables and reading order on complex pages |
| Layout-aware parsers (layout detection plus table structure models) | Headings, multi-column pages, tables; output as Markdown or JSON | Unusual layouts, handwriting |
| Vision-language models as parsers | Messy layouts, charts, forms, handwriting; can describe figures | Cost per page, occasional hallucinated cells |
Open-source layout-aware converters (for example Docling and Marker, among others) turn PDFs into Markdown or JSON with headings and tables, and can run locally. Small vision-language models trained for document OCR are increasingly good, and a general VLM can read a chart or a form. A practical pattern: a fast parser for everything, and a VLM only for pages the fast parser flags as hard (low text, many tables, images).
Whatever you use, validate tables: check row counts and that numeric columns parse as numbers. A VLM that invents a cell is worse than one that leaves it blank.
Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- Step 3: chunk by structure
- Step 4: enrich with metadata
- Keeping the index fresh
- Common mistakes
- Exercises
You will be able to answer
- A PDF returns no text when you extract it. What is the likely reason?
- Why do fixed-size chunks hurt questions about tables?
- What does prefixing each chunk with its heading path ('Pricing > Plans > Team plan') achieve?
- When is a vision-language model a good document parser?
- Which metadata is most useful to store with every chunk?
Articles that build on this lesson
- RAG vs fine-tuning: which does your problem need? (7 min read)