Turn invoices, receipts and forms into clean JSON with a local AI
A folder of PDFs and phone photos goes in; validated JSON and a spreadsheet come out, with every document that doesn't add up flagged for a human. Ollama's schema-constrained output makes the model fill in your exact fields, Pydantic and a few lines of business rules catch what it gets wrong, and a scorer tells you how accurate it really is. Nothing leaves your PC.
- Spreadsheet export with a needs_review column, so a person checks only the documents that need it
- Real numbers from a laptop CPU: 205 of 205 fields right on clean documents, and how the checks behave on rough scans
- Ollama with a vision model:
qwen3.5:4b(3.3GB) for accuracy,gemma4:e2b(4.6GB) for speed. Run AI on your PC - uv from docs.astral.sh/uv ↗: every script installs its own packages
- 16GB of RAM. A graphics card turns a minute per document into a few seconds
- Related: Sort thousands of records with a local AI labels rows; this mission pulls fields out of documents
Why a local model, and why schemas
Three layers make extraction trustworthy: a schema the model must follow, validation, and rules the model can't fake.
The jobsupplier, invoice number, dates, every line item and the totals from a pile of invoices; merchant, items and total from receipt photos; the answers from filled-in forms. People pay for this by the hour, and accounts software charges per document. A 4B model on your own PC does it free, and client paperwork never goes near a cloud service.
Layer 1: schema-constrained outputOllama's
formatparameter accepts a JSON schema, and the model's output is constrained to match it: right field names, right types, no chatty preamble. You write the schema once as a Pydantic class and sendModel.model_json_schema().Layer 2: validationThe reply is parsed with
Model.model_validate_json(). Dates must be real dates, money must be numbers, a payment method must be one of the allowed values. Anything else is an error your code sees.Layer 3: business rulesValid JSON can still be wrong. Line items must add up to the subtotal, subtotal plus VAT must equal the total, the date must be plausible. When a rule fails, the script retries once with the problems spelled out, and if it still fails, it flags the document for a human.
Get the kitdoc-extract.zip has the scripts and 12 sample documents for fictional businesses with their right answers. kit.md has the schema-writing rules and a step-by-step order for your own documents.