How to Automate PDF Data Extraction With LlamaParse and Python

October 12, 2026

Single-file parsing is the demo. The job is usually a folder of 500 invoices that needs to become rows in a table. This walks through batch parsing with LlamaParse, extracting typed fields, and catching bad rows before they poison your data.

Quick answer: Loop the files with parser.load_data, ask the parser for Markdown, then run a Pydantic extraction prompt to get typed fields like invoice_total: float. Validate every row and log failures instead of guessing.

The mistake I see most: trusting the parser output as ground truth. It is very good, not perfect. You need a verification step, and it is cheap to add.

How do you batch-parse a folder?

import glob
from llama_parse import LlamaParse

parser = LlamaParse(result_type="markdown", num_workers=4)

results = {}
for path in glob.glob("invoices/*.pdf"):
    docs = parser.load_data(path)
    results[path] = docs[0].text
print(len(results), "files parsed")

Keep raw Markdown on disk. If extraction goes wrong later you can re-run the cheap step without paying to parse the PDFs twice.

How do you extract typed fields?

Pydantic models give the model a strict shape to fill. This beats prompt-only extraction because the type annotations do the enforcing.

from pydantic import BaseModel
from llama_index.core.program import LLMTextCompletionProgram

class Invoice(BaseModel):
    invoice_number: str
    vendor: str
    total: float
    due_date: str

program = LLMTextCompletionProgram.from_defaults(
    output_cls=Invoice,
    prompt_template_str="Extract invoice fields from:\n{doc}",
)
inv = program(doc=results["invoices/a.pdf"])

If a field does not exist in the document, expect a hallucinated value rather than an error. This is why validation is not optional.

How do you validate before saving?

  • Sum the line items and compare to the stated total. Mismatch means the table parsed wrong.
  • Check the invoice number matches a regex pattern your vendor uses.
  • Compare the extracted date to the document’s own text as a sanity check.
  • Log every file that fails validation to a queue for a human, do not drop it silently.

What does batch parsing cost?

Pricing is per page. A 3-page invoice batch of 500 files is about 1,500 pages per full run. On the free tier that takes more than one day; a paid plan makes it a single job. Add the LLM extraction cost on top — a few cents per document with a mid-tier model.

When should you not use this pattern?

If your invoices come from three vendors, write a template parser instead. LlamaParse shines on unknown layouts. For uniform documents, a fixed coordinate-based extractor is faster, free, and exact.

How do you keep it maintainable?

Separate parse, extract, and validate into three functions. Swap the parser later without touching extraction. Version your Pydantic model so an old batch can be replayed. And store the raw text alongside the parsed fields — the debugging value is worth the disk space. Drop the results into a vector store for search, or hand them to LangChain if you want a retrieval layer over historical invoices.

Who this suits: ops and data teams drowning in documents with no fixed template. Who should skip it: if a regex or a spreadsheet import already works, you do not need vision parsing at all.

Quick Summary

  • Batch with parser.load_data and keep raw Markdown for cheap re-runs.
  • Use Pydantic models to force typed output from extraction prompts.
  • Always validate totals and IDs; the parser is accurate but not infallible.
  • Cost is per page plus your model provider for extraction.
  • Template parsers beat vision parsing for uniform, repeating documents.