title: Tables come out garbled when you load PDFs into LangChain: why, and three fixes
description: PyPDFLoader and other text loaders flatten PDF tables into one value per line, so rows and columns are gone before the splitter runs. Three fixes with working
Python, cheapest first: detect ruled tables with PyMuPDF, keep each table in one chunk, or load with a vision parser for scans and borderless tables.
Tables come out garbled when you load PDFs into LangChain¶
Your table was gone before the splitter ran.
A PDF has no table in it, only words at positions. A text loader reads them in stream order, so one row of a loan
form arrives as Loan Amount / $162,000 / NO on three separate lines, and the splitter then cuts wherever
its character count says.
This is what plain text extraction (PyMuPDF's get_text; PyPDFLoader gives the same kind of output) returns
for the first table of a real document (the US CFPB's
sample Loan Estimate, in our repo as
loan_estimate.pdf):
Loan Terms
Can this amount increase after closing?
Loan Amount
$162,000
NO
Interest Rate
3.875%
NO
Monthly Principal & Interest
See Projected Payments below for your
Estimated Total Monthly Payment
$761.78
Nothing says which NO belongs to which row. An LLM asked "can the interest rate increase?" is guessing.
Three fixes, cheapest first.
| Fix | Works for | Cost |
|---|---|---|
| 1. Detect tables with PyMuPDF and load them as Markdown | born-digital PDFs with ruled tables | free |
| 2. Keep each table in one chunk | every loader | free |
| 3. Load with a parser that reads the page as an image | scans, borderless tables, merged cells | model cost, or a GPU for local parsers |
1. Detect the tables (free, born-digital PDFs)¶
import pymupdf # pip install pymupdf
from langchain_core.documents import Document
def table_documents(pdf_path):
"""One LangChain Document per table PyMuPDF finds, as Markdown. Needs a text layer."""
docs = []
for page in pymupdf.open(pdf_path):
for table in page.find_tables().tables:
docs.append(Document(page_content=table.to_markdown(),
metadata={"source": pdf_path, "page": page.number + 1, "type": "table"}))
return docs
On the same Loan Estimate this finds the payments table and returns it with its columns:
|Projected Payments|Col2|Col3|
|---|---|---|
|**Payment Calculation**|**Years 1-7**|**Years 8-30**|
|**Estimated Total**<br>**Monthly Payment**|$1,050|$968|
It is not magic: it found two tables on page 1 and still missed the borderless "Loan Terms" block shown above.
Ruled tables work, tables drawn with whitespace often do not, and scans have no text layer to work with.
pdfplumber and Camelot make the same trade.
2. Keep each table in one chunk¶
Half of "garbled" is the splitter cutting a table in two, with the header in one chunk and the numbers in the next.
Load tables as their own documents (as above), do not pass them through RecursiveCharacterTextSplitter, and put
the section heading or a one-line description in front of each so the retriever can find it.
3. Read the page as an image (scans, borderless tables, merged cells)¶
When there are no ruling lines or no text layer, something has to look at the page. ReadAnyDoc (open source, Apache-2.0) has a LangChain loader that reads each page with a vision model and returns tables as tables:
from readanydoc.integrations.langchain import ReadAnyDocLoader
docs = ReadAnyDocLoader("report.pdf", split="block").load() # one Document per title, paragraph, table...
tables = [d for d in docs if d.metadata["block_type"] == "Table"]
print(tables[0].page_content) # the table as HTML
print(tables[0].metadata) # source, page, block_type, bbox, confidence
split="block" gives fix 2 for free: a table is never cut. Use split="page" (the default) if you prefer one
document per page.
How good is it at tables, measured on public benchmarks against Reducto:
| Benchmark | ReadAnyDoc | Reducto |
|---|---|---|
| RD-TableBench, 1,000 tables | 88.84 | 88.85 |
| OmniDocBench, table TEDS | 88.1 | 87.9 |
| 89.8 | 92.7 |
Level on two, behind on one. Where it loses outright: it takes 20-50 seconds a page and costs about $0.003-0.005 a
page in model usage, so use fix 1 for the documents where fix 1 works.
Other parsers that read layout and plug into LangChain or
LlamaIndex:
Docling (local, MIT),
Unstructured's hi_res
strategy, LlamaParse (hosted). How they compare.
Questions¶
Why does PyPDFLoader mess up tables? It returns the page's text in the order it is stored. The row and column structure is not in that text.
Is HTML or Markdown better for tables in RAG? Markdown is shorter; HTML keeps merged cells and multi-row headers. If your tables have spans, keep HTML.
Do I need OCR? Only for scans and photos, where there is no text layer. Fix 1 cannot work there; fix 3 can.
The table continues on the next page. What then? Detection is per page, so you get two tables. Repeat the header row on the second part before you index it, or extract the rows into a schema instead: extract tables into rows.