Enterprise DNA

Omni by Enterprise DNA

Enterprise DNA Resources

Step-by-step how-tos. Practical AI operating-system thinking for owners, operators, and teams doing real work.

220k+

Data professionals

Omni

AI agents and apps

Audit

Map the manual work

Guide Intermediate General

AI PDF Extraction in Python: A Working Guide

A practical walkthrough of AI PDF extraction in Python covering setup, structured extraction with LLMs, key settings, and a production workflow pattern.

Sam McKay |
AI PDF Extraction in Python: A Working Guide

What AI PDF Extraction Actually Is

AI PDF extraction is the practice of pulling structured, machine-readable information out of PDF documents using language models rather than rule-based parsers. Traditional PDF libraries like pypdf or pdfplumber give you raw text and bounding boxes. They are excellent at what they do, but they stop at characters and rectangles. They cannot tell you that “Acme Corp” on page 3 is the vendor name, that the number next to “Total Due” is the invoice amount, or that the table on page 7 represents quarterly revenue by region.

AI PDF extraction layers a model on top of that raw output. The model reads the extracted text, the rendered page images, or both, and returns structured fields you define. In practice this means you describe a schema (vendor, date, line items, total) and the model fills it in. The model is doing the work a human reader would do, except it runs in a few seconds per page and costs fractions of a cent per document.

There are two broad technical approaches. The first is text-first, where you run a library like pdfplumber to get the text and then send that text to a language model with a structured-output prompt. The second is vision-first, where you render pages as images and send them to a multimodal model like GPT-4o, Claude, or Gemini. Vision-first is more expensive but handles scanned documents, complex layouts, and tables that text extraction mangles.

A third category sits between these. Dedicated document AI services like Landing AI’s ADE, Azure Document Intelligence, AWS Textract, and Google Document AI combine OCR, layout analysis, and learned extraction in one API. LlamaParse and Unstructured.io are similar services aimed at LLM pipelines. These are usually the right starting point for production work because they handle the messy plumbing for you.

Setup and Authentication

You will need a Python environment, a couple of libraries, and an API key. I recommend a fresh virtual environment so library versions do not collide with anything else on your machine.

The base libraries cover PDF reading and image rendering. pypdf handles text extraction and metadata. pdfplumber gives you layout-aware text and table extraction. PyMuPDF, imported as fitz, renders pages to images at any DPI. For the model side, the openai and anthropic SDKs are the standard clients, and most other providers follow the same pattern.

Install the core stack with pip. The exact versions move over time, so pin to ranges you know work together rather than chasing the latest release.

Authentication depends on the provider. For OpenAI, Anthropic, and Google, you set an environment variable and the SDK picks it up. For Azure Document Intelligence and AWS Textract, you need an endpoint URL plus a key or an IAM role. For Landing AI and LlamaParse, you sign up on their site and get a key the same way.

A common mistake is hardcoding keys in scripts. Use a .env file with python-dotenv, or set the variable in your shell before running. Keys leaking into git is the single most common way these setups break in production, and it is the first thing to check when something stops working for no obvious reason.

First Working Example

Here is a minimal end-to-end run that extracts structured fields from a single invoice PDF. It uses pdfplumber for text and the OpenAI SDK for structured extraction. The same pattern works with Anthropic or any provider that supports JSON mode or tool use.

The script does four things. It opens the PDF, walks the pages and collects text, builds a prompt that describes the schema you want, and calls the model with a JSON response format. The result is a dictionary you can write to a database or pass to another pipeline.

For scanned PDFs where pdfplumber returns empty strings, swap the text step for a render step using PyMuPDF. Save each page as a PNG, base64-encode it, and send it to a multimodal model. The prompt stays the same, you just attach images instead of text.

Run the script on a real invoice and you should get back something like a vendor name, invoice number, date, line items, and total. If a field is missing, the model usually returns null rather than guessing, which is what you want for downstream validation.

A useful first check is to print the raw model response and confirm the JSON parses cleanly. If it does not, the issue is almost always the prompt asking for fields the document does not contain, or a context window that is too small for the input. Both are easy to diagnose once you see the raw output.

Key Settings That Matter

The dials most people skip are the ones that determine whether your pipeline is reliable or noisy.

Page selection matters more than model choice. Sending the whole document when you only need pages 2 through 4 wastes tokens and dilutes attention. Most production systems extract a table of contents first, identify the relevant pages, and only feed those to the model. This single change often cuts cost in half and improves accuracy at the same time.

Resolution on rendered images is the next dial. 150 DPI is fine for clean digital PDFs. 300 DPI is the floor for scanned documents with small text. Going above 400 DPI rarely helps and slows things down. The trade-off is token cost, since higher resolution means more image tokens.

The schema definition is where most of the quality lives. Be specific about field names, units, and what null means. “Total amount in USD as a number, or null if not present” outperforms “total” every time. If you have a few real documents, include one as a worked example in the prompt. Few-shot examples consistently beat clever instructions.

Temperature should be zero or very close to it. Extraction is not a creative task. You want the most probable answer, not a varied one. Most providers default to a higher temperature for chat, so set it explicitly in every call.

Chunking strategy matters when documents exceed the context window. The naive approach is to split by page count, which breaks tables and sections mid-flow. Better approaches split on headings or use semantic chunking that keeps related content together. For very long documents, extract per section and merge results with a second pass.

Finally, validation. Always run a check pass that verifies required fields are present, totals add up, dates parse, and values fall in expected ranges. The model will sometimes return plausible-looking garbage. A simple validator catches most of it before it reaches your database.

Where It Shines

AI PDF extraction genuinely excels at three categories of document.

Semi-structured business documents are the sweet spot. Invoices, purchase orders, receipts, bills of lading, and contracts all have a consistent shape with some variation. The model handles the variation cleanly because it has seen thousands of similar documents during training. A rule-based parser would need hundreds of rules to cover the same ground.

Tables with merged cells, nested headers, and inconsistent column counts are another strong case. pdfplumber can extract simple tables well, but anything beyond a basic grid starts to break. Vision models read tables the way a human does, by following the visual structure. For financial statements, this is often the only reliable approach.

Multi-language and mixed-language documents work surprisingly well. A model trained on multilingual data can extract English fields from a document that also contains German, Mandarin, or Arabic. Traditional OCR pipelines need per-language configuration and often fail on language switches within a page.

Where It Fails

Honest list of where this approach breaks down.

Very large documents are expensive. A 500-page contract sent page-by-page to a vision model can cost real money and take minutes. For documents this size, you need a hybrid approach. Extract structure first with a cheap method, identify relevant sections, then use AI only on those.

Handwriting recognition is uneven. Modern vision models handle printed handwriting reasonably well, but cursive, poor scans, and forms filled out in pencil still produce errors. For high-stakes handwriting extraction, a dedicated handwriting OCR model is more reliable than a general-purpose multimodal model.

Highly stylized layouts defeat the schema. If every page in a brochure has a unique layout, the model has nothing to generalize from. AI extraction assumes some consistency. For one-off creative documents, manual extraction or a custom-trained model is faster.

Cost adds up at scale. A few cents per document is fine for a hundred documents a month. At a hundred thousand documents a month, you need to think about batching, caching, and whether a dedicated document AI service is cheaper than a general-purpose model.

Hallucination is real. The model will sometimes invent field values that look correct but are not in the document. This is why validation matters and why you should never use extracted values for compliance or legal decisions without a human review step.

Practical Workflow Pattern

The setup that actually works in production looks like this.

Stage one is intake. Drop PDFs into a watched folder or an S3 bucket. A small worker picks them up, runs a quick metadata check, and queues them for processing. This stage also runs a fast OCR pass if the document is scanned, so you know what you are dealing with before committing to a more expensive extraction call.

Stage two is extraction. Based on document type, route to the right pipeline. Invoices go through one schema, contracts through another, financial statements through a third. Each pipeline has its own prompt, its own validation rules, and its own model choice. The routing logic is usually a simple classifier that reads the first page and picks a category.

Stage three is validation. Run the schema check, the arithmetic check, and any business rules. Flag anything suspicious for human review. Store the validated result in your database with a link back to the source PDF and the page number for each field, so a reviewer can find the source of any value in one click.

Stage four is feedback. When a human corrects an extraction, log the correction. Periodically review the corrections and update the prompts or examples. This is how the system gets better over time without retraining a model.

A few habits make this work. Always store the raw extraction alongside the validated result so you can replay changes. Keep prompts in version control. Track cost per document so you know when the economics shift. And run a small sample through manual review every week, even when the system looks healthy.

The right mental model is that AI PDF extraction is a fast, flexible first pass, not a replacement for review. The value is in collapsing the 80 percent of documents that are routine so a human can focus on the 20 percent that actually need attention.

Enterprise DNA put together a free field guide on exactly this: the full Claude ecosystem, Claude Code, and how to roll agents out without breaking things. Get the guide.