Skip to content

Chatbots & AI Agents

Indexing complex PDFs in a RAG system without breaking tables

When a RAG assistant quotes the wrong price or clause, examine your PDFs before blaming the model. Conversion, chunking, checks: how to keep tables and contracts intact.

By Lenobot Engineering

  • 11 min read
Indexing complex PDFs in a RAG system without breaking tables

For a RAG assistant to quote the right price or the right clause, your PDFs need to be converted into structured text, chunked along their own structure (an article, a table, a row) rather than every N words, then enriched with the context each chunk lacks: column headers, article title, version. Semantic chunking will not repair a badly extracted table, and for price lists, a database remains safer than searching through text.

In short

  • A PDF describes characters on a page, not tables: naive extraction mixes up columns and separates figures from their headers.
  • Chunking should follow the document's structure: one article per chunk for a contract, a whole table or one row with its headers for a price list.
  • Every chunk must make sense on its own: title, headers, unit, version and effective date repeated inside it.
  • Semantic chunking suits long text without headings, much less contracts and price lists.
  • Prices and calculations are better served from a database queried by code, and every extracted figure is checked before indexing.

Why complex PDFs distort RAG answers

A RAG assistant (retrieval-augmented generation) does not reread your documents for each question: it retrieves from an index a few chunks that seem to answer it, and the language model writes its reply from those excerpts alone. Our article on RAG for internal documentation explains how this works. An answer is therefore only as good as its chunks.

Yet a PDF describes a page to be printed, characters placed at coordinates: it generally has no idea that a series of numbers forms a column. Basic extraction therefore produces predictable defects:

  • two-column text read straight across, with paragraphs interleaved;
  • table cells reduced to a string of values with no headers;
  • tables spanning several pages delivered in pieces;
  • notes ("excl. VAT", "fuel surcharge not included") far from the values they qualify;
  • on a scan, character recognition (OCR) errors: a 1 read as a 7, a lost decimal separator.

Size-based chunking, the default in many tools, makes things worse by separating a row from its headers or a clause from its exceptions.

Take a hypothetical distributor indexing its shipping rate grid: one row per zone, one column per weight band. A sales rep asks for the rate for an 8 kg parcel to zone 3. The retrieved chunk contains "Zone 3" followed by four amounts with no headers. The model picks one, a plausible one, and states it with confidence: a wrong answer that looks exactly like a right one.

Convert each PDF into structured text

There are four families of extraction tools, often combined within the same project.

ApproachStrengthsLimitationsRecommended for
Raw text (standard PDF libraries)Fast, inexpensiveMixes up columns, loses tablesDocuments without tables
Layout analysis (open-source Docling, or a cloud service)Headings, reading order and table structure; Markdown, HTML or JSON exportMerged cells still fragile; in the cloud, documents handed to a third partyMost contracts, catalogs and technical data sheets
Multimodal model that reads the page as an imageScans, irregular layoutsSlower, more expensive, can miscopy a figureDifficult pages flagged by the checks
Export from the source system (spreadsheet, ERP)Exact data, already structuredThe source must exist and be accessiblePrice lists

On a native PDF (generated by software, not scanned), a tool like Docling, in its standard pipeline, infers the table structure but keeps the characters from the file itself. A multimodal model, by contrast, copies every value: better at handling chaotic pages, it can turn 14.10 into 14.70 without flagging it. Structure can be inferred; figures must not be. So keep these models for the pages that conventional tools cannot handle, and check the values they return.

Test candidates on your hardest documents, not on demos, and for an online service, check where files are hosted, how long they are kept, and the data processing agreement (Article 28 of the GDPR).

Chunk by structure, not by length

Chunking sets the unit that search will be able to retrieve. Here are the common methods, judged by how they handle tables and contracts.

MethodPrincipleSuited toRisk for tables and contracts
Fixed sizeCuts every N wordsPrototypes, uniform proseSeparates values from headers, cuts clauses
RecursiveCuts at paragraphs, then at sentencesProcedures, documentationIgnores table structure
SemanticCuts when the topic changesLong text without headingsBlind to tables, more expensive indexing
StructuralFollows headings, articles and table rowsContracts, terms and conditions, price listsRequires reliable extraction

What semantic chunking actually delivers

In the strict sense, semantic chunking computes an embedding for each sentence (a series of numbers representing its meaning) and cuts where two consecutive sentences drift apart. It has two limitations here: a row of figures carries little usable meaning, and a contract already has explicit boundaries, its numbered articles. A 2024 research paper, "Is Semantic Chunking Worth the Computational Cost?" (Qu, Tu and Bao), concludes that, on the tasks tested, its gains over fixed-size chunking are too inconsistent to justify its computational cost. Our position: start from the structure, and keep semantic chunking for long texts that have none.

Contracts: one article, one clause

  • One chunk per article, or per sub-clause if the article is long, with the article title repeated.
  • The numbering ("article 7.2") stays in the chunk: the assistant will cite it, the user will check it.
  • Cross-references ("subject to article 9") and defined terms are recorded as metadata, so the referenced article and the definition are retrieved along with the clause.
  • An amendment is linked to the article it modifies, and the previous version is marked as superseded.

Price lists: the whole table or one row with its headers

A small table fits in a single chunk, with its title, unit and validity period. A large table is split by row, repeating the headers and the applicable notes, after stitching its pages back together if it spans several. Applied to the grid from our example:

Before (raw extraction):
Zone 3   12.40   14.10   15.80   18.90   *

After (self-contained chunk):
Shipping rate grid, version 4, prices in euros excl. VAT
Zone 3: 0 to 5 kg = 12.40; 5 to 10 kg = 14.10; 10 to 20 kg = 15.80; over 20 kg = 18.90
Note: rates exclude fuel surcharge

Make every chunk self-contained

A chunk retrieved on its own must carry what it needs to be understood and cited. Put a breadcrumb at the top ("Supplier framework agreement X, amendment 2, article 7, late delivery penalties") and store filterable metadata: document type, customer or product line, version, effective date, status (in force, superseded, expired), page and article number. Filtering on status keeps the assistant from quoting last year's price list, even if it looks a lot like the new one.

A model can also write, for each chunk, one or two sentences that place it within its document. Anthropic described this technique in September 2024 under the name "contextual retrieval" and reports, on its own test sets, 35% fewer retrieval failures (the right passage missing from the top twenty results), and 49% fewer when combined with keyword search. These figures hold for their data: the effect on yours has to be measured, and each chunk costs one model call at indexing time.

Take the figures out of RAG when you can

For a question like "how much do 250 units of item X cost, delivered to zone 3?", even a perfect chunk leaves the model a quantity tier to identify, a multiplication and an addition. It can do it, but with no guarantee: we advise against handing it that calculation.

It is better to store the price list as data (a database table, or the source itself) and give the assistant a tool: a function in code that it calls with parameters (item, quantity, zone). The code reads the right cell, applies the discounts and returns the result; the model picks the tool and writes the reply. This mechanism, called tool calling (or function calling), makes the calculation exact and traceable, and keeps the answer current as soon as the source changes. The risk shifts to the parameters the model passes (zone, item): log them and test them.

RAG still has its place for the text around the figures: terms and conditions, exclusions, delivery terms. The price comes from the database, the condition from the document, and the answer cites both. This is the separation we apply in our projects for AI assistants connected to your documents. When the PDF is the only source, such as a supplier's price list, extract it with each new version, have a person validate it, then store it as a table.

Check before indexing, then test with numeric questions

An extraction is checked before it enters the index, using automated rules:

  • row and column counts consistent with the previous version of the grid;
  • every value parses as a number, with its unit and currency;
  • totals and subtotals can be recomputed from the rows;
  • no table gets indexed without a header row;
  • pages with a low OCR confidence score go to human review.

Complement this with spot checks, original page and extracted text side by side, starting with signed contracts and current price lists.

Then build a set of questions whose answer sits in one precise place: a cell, a note under a table, a row after a page break, a clause modified by an amendment. Add questions with no answer in the documents: the assistant must say so. Measure separately whether the right chunk is retrieved, whether the figure is exact to the character, and whether the cited source is the right one. How to build such a reference set is covered in our article on evaluating the quality of an AI agent. Rerun it whenever you change the extraction tool, the chunking or the model.

Frequently asked questions

No. Start with the documents that carry figures or commitments: price lists in force, active contracts, technical data sheets. Procedures and internal notes usually do fine with standard extraction and chunking.

Markdown is enough for a simple table: one header row, no merged cells. Beyond that, switch to HTML, which preserves merged cells, or flatten the headers into compound names ("Price excl. VAT, 100 to 499 units"). What matters is that every value reaches the model together with its headers.

Yes, with OCR or a multimodal model, but errors on figures are more frequent. First ask for the original file (a supplier can often send its price list as a spreadsheet), and have a person review any value the company will be bound by.

No. Fine-tuning adjusts a model's style or behavior without guaranteeing that it will reproduce an exact price, and it would have to be redone at every price change. Prices that change belong in a database or an index that you can update and cite.

Conclusion

When an assistant gets a price or a clause wrong, examine the document pipeline before blaming the model: PDFs that lost their structure during extraction, blind chunking or figures left to search rather than to a database are enough to cause the error. Extract with layout awareness, chunk along articles and rows, make every chunk self-contained, check, then test: these steps are decided at the scoping stage, not after the first wrong price sent to a customer.

If your assistant struggles with price lists, contracts or technical data sheets, let's talk about your context.

Written by

Lenobot Engineering

We design, build and maintain websites, business applications and AI systems. Our articles share the methods we apply on our projects.

About the studio