Turning messy documents into structured data with LLMs
How to build a reliable LLM pipeline that reads PDFs, emails and forms and writes clean, validated records into your database.
Samed Škulj
· 3 min read
Some of the most valuable AI work in a SaaS product isn't a chat window. It's a pipeline that reads something messy (a PDF contract, an email, a scanned form, a page of legislation) and writes clean fields into your database, so the rest of your app can work with it.
These pipelines are also where reliability matters most, because their output is stored and acted on. Here is how to build one that holds up.
Define the target schema first
Start from the record you want, not the document you have. Write it as a schema in your code:
import { z } from "zod";
export const Invoice = z.object({
supplierName: z.string(),
invoiceNumber: z.string(),
issueDate: z.string().date(),
currency: z.enum(["EUR", "USD", "GBP"]),
total: z.number().nonnegative(),
lines: z.array(
z.object({
description: z.string(),
quantity: z.number(),
unitPrice: z.number(),
}),
),
});
Every field needs a clear meaning, a type, and a rule for what to do when the value is missing. Decide whether a missing field is null or a failure.
Use the provider's structured output support
Modern model APIs can be told to return JSON that matches a schema, either through a dedicated structured output mode or through tool calling. Use it. It removes a whole class of parsing failures.
Then validate anyway. Parse the response with your schema and treat a validation error as a failed extraction, not a crash.
Split the work into steps
One giant prompt that reads a 40-page document and returns 60 fields is hard to debug and expensive to retry. Split it:
- Prepare: extract text from the file (PDF text layer, OCR if needed), clean it, split it into sections.
- Locate: find which sections hold which information.
- Extract: run a focused prompt per group of fields on the relevant section only.
- Validate: schema checks plus business rules (line items add up to the total, dates are plausible).
- Store: write the record with its source references and a status.
Each step is testable on its own and can be retried on its own.
Keep the evidence
For every extracted value, store where it came from: page number, section, or the exact quoted text. This lets users verify quickly, makes debugging possible, and gives you a clear answer when someone asks "why did the system think this?"
Check the numbers with code, not the model
Models are good at reading. They are less reliable at arithmetic. If the line items should sum to the total, compute the sum in code and compare. If a date must be before another date, check it in code. Mismatches go to review.
Route uncertainty to a human
Not every document will extract cleanly. Give each record a status:
- Done: passed every check.
- Needs review: a check failed or a required field is missing. Show the user the document next to the fields, with the problem highlighted.
- Failed: unreadable input. Tell the user why.
A pipeline that handles 85% automatically and routes 15% to a quick review screen is far more useful than one that claims 100% and is quietly wrong.
Make it idempotent and observable
- Re-running the pipeline on the same document should produce the same record, not a duplicate.
- Log every step with timing, tokens and outcome.
- Store the prompt version with each record, so you know which version produced which data.
Test with real documents
Build an eval set of real documents with the correct records. Include the ugly ones: scans, tables split across pages, documents in another language. Field-level accuracy on this set is your quality number.
Extraction pipelines are one of the most common AI feature builds I do, and one of the fastest to pay back. They also pair well with cost controls, since most of this work can run in batches.

Written by Samed Škulj
Senior Full-Stack Engineer since 2021. I have shipped with teams at Deel, HTEC Group (for Zoox), Ministry of Programming, and now help SaaS teams add AI features and get AI-built apps ready for production. LinkedIn