Table of contents #
Structured output with per-field confidence scores through the Nutrient Data Extraction API.
AI data extraction is the use of machine learning models to pull structured data — such as invoice numbers, dates, totals, and line items — out of unstructured documents like PDFs, scans, and photos. You get JSON or a database row back instead of a page of text, and an API like Nutrient Data Extraction API also tells you where on the page each value came from.
There are three common ways to extract data from documents: templates and rules, trained machine learning (ML) models, and schema extraction with large language models (LLMs). Only the last two use AI, but templates are the baseline they’re compared against. All three can read an invoice. They differ in setup time, cost per page, what breaks them, and what happens when they get a value wrong. Below, we compare the three and then walk through schema extraction in Python and Node.js.
The three approaches at a glance #
| Approach | How it works | Setup per document type | What breaks it | How you find out it’s wrong |
|---|---|---|---|---|
| Templates and rules | Fixed page zones, keywords, and regular expressions on optical character recognition (OCR) text | A template per layout | A sender changes its layout | You don’t; a wrong zone still returns text |
| Trained ML models | A model learns field locations from labeled examples | Hundreds of labeled documents | New document types, or documents that change over time | A model score, if it exposes one |
| LLM schema extraction | A model maps the document to fields described in a JSON Schema | A schema, written in minutes | Fields that aren’t in the document, which can lead to made-up values | Citations and confidence per field, if the API returns them |
Templates and rules #
Templates are the oldest approach and still the most common. You draw boxes on a known layout and read whatever text lands inside them, or you search the OCR text with keywords and regular expressions. For example, find “Invoice no.” and take the next token.
On a document that never changes, this works well. It’s fast and cheap, and anyone can read the rules. The trouble starts when a supplier redesigns its invoice and the template doesn’t know anything changed. It reads whatever now sits in the old box, maybe the tax line, and passes it on as the total. Nobody notices until the numbers don’t reconcile. Multiply that by a few hundred suppliers, and keeping templates working becomes someone’s full-time job.
If most of your volume comes from a handful of layouts you control, like your own forms, templates are hard to beat.
Trained machine learning models #
The next step up is a model trained on labeled examples. Layout-aware models look at both the text and where it sits on the page, so they learn that the total is usually near the bottom right, under a column of amounts, even when it moves around from one supplier to the next.
The tradeoff is training data. Someone has to label hundreds of documents for each document type, and when a new type shows up or the existing ones slowly change, they label again and retrain. On the plus side, most of these models give you a score per field, so you have something to route on.
It’s a good fit if you already have labeled data for a few high-volume document types and someone on the team who can retrain the models.
LLM schema extraction #
Schema extraction skips both the templates and the labeling. You describe the fields you want in a JSON Schema, and a model finds them on the page, wherever they are. A new field is one more line in the schema. A new document type is a new schema.
The risk is that a generative model returns an answer even when the document doesn’t contain one. Ask for a due date on an invoice that doesn’t have one, and you may get a believable date in valid JSON. Nothing in the output looks wrong. Our post on catching hallucinated values in document extraction explains why this happens.
When you evaluate an LLM extraction API, check what evidence comes back with each value: where on the page it was found, how closely it matched the source text, and how confident the model is. With that, your code can let clean values through and send doubtful ones to a person.
Extract data with Nutrient Data Extraction API in Python and Node.js #
Nutrient Data Extraction API has an extract endpoint that takes a document and a JSON Schema and returns typed data. Both samples below set includeCitations, so each value comes with its page number, bounding box, and match label, and usually a confidence score as well. You’ll need an API key from the Nutrient dashboard, stored in the NUTRIENT_API_KEY environment variable.
Both samples send the same minimal invoice schema: an invoice number, an invoice date, and a total amount, with the number and the total required.
Python
The Python version needs the requests package (pip install requests):
Node.js
This version uses fs.openAsBlob, which needs Node.js 19.8 or later. It also uses top-level await, so save it as extract.mjs:
Reading the result
Both scripts print one line per field: the field name, its value, the match label, the confidence, and the page number. For the second field below, that line is total_amount 1547.5 fuzzy_match 0.76 1.
The values are under output.data, and each one has a citation under output.metadata. Here’s a trimmed response for a sample invoice:
match says how the value was tied to the page. The main labels are id_match (an exact match with the source text), fuzzy_match (close but not identical), and not_found (the API couldn’t find the value anywhere on the page). The guide also lists id_match_multiblock, for values assembled from several blocks, and id_match_partial, for values only partly matched. A sensible starting rule is to send every fuzzy_match, id_match_partial, and not_found to a person. In this response, total_amount is a fuzzy_match at 0.76, so that rule would send it to review.
confidence is a score between 0 and 1. Higher means the value is more likely to be right, but it isn’t a guarantee. Read it alongside match, and test any threshold on a labeled sample of your own documents before you rely on it. A missing confidence value means no score was available, not that the score is low.
The citations and confidence guide lists every field and match label, and our post on confidence scores covers how to set thresholds. For a closer look at what a single extracted field should contain, see our post on intelligent data extraction.
Run the same extraction from Claude or Codex with Nutrient MCP #
If you already work in Claude, Claude Code, or Codex, you don’t have to write the request yourself. Nutrient MCP is a hosted Model Context Protocol server that lets your assistant extract fields, parse documents to Markdown, and classify documents. There’s nothing to install and no API key to manage. You add the endpoint and sign in with your Nutrient account. In Claude Code, run:
Then type /mcp, choose nutrient, and select Authenticate. After that, you can ask in plain words, for example: “Extract the invoice number, date, and total from invoice.pdf, with citations.” Requests use your organization’s Nutrient access and plan. The Nutrient MCP setup guide covers Codex as well.
How AI data extraction pricing works #
Templates are nearly free to run. What they cost is the time to build them and keep them working. Training a model takes hours of labeling up front, and running and retraining it takes servers.
APIs charge per page. On Nutrient Data Extraction API, each page costs a parse charge that depends on the processing mode, plus a charge for the extraction itself:
| Processing mode | Typical use |
|---|---|
text |
Born-digital PDFs with a clean text layer |
structure |
OCR-backed structured extraction |
understand (default) |
Tables, forms, and scans |
agentic |
Difficult layouts that need a vision language model |
The modes are listed from cheapest to most expensive. For a PDF exported straight from accounting software, text mode is usually enough, but scans and documents with dense tables usually need understand. The free plan includes usage that refreshes every month, so you can try it on your own files before paying. Current rates are in the pricing guide.
If your documents can’t leave your own infrastructure, Nutrient’s Data Extraction SDK runs on your servers. It’s available today in the Nutrient Python SDK and Java SDK.
Which approach should you use? #
Most teams use more than one. As a rough guide:
- If a few fixed layouts make up most of your volume, templates are still the cheapest and most predictable choice.
- If you have a few document types, labeled data for them, and an ML team to look after it, a trained model makes sense.
- If documents come from many senders, layouts keep changing, or new document types arrive every quarter, schema extraction adapts without retraining, and per-field citations show you which values to check.
A common setup is rules for the documents you control, schema extraction for everything else, and a review queue for anything with low confidence.
FAQ #
AI data extraction uses machine learning models to pull structured data — such as names, dates, totals, and line items — out of unstructured documents like PDFs, scans, and photos. The result is JSON or a database row instead of a page of text. Nutrient Data Extraction API does this from a JSON Schema and returns a citation for every value.
OCR turns an image of text into characters. AI data extraction works out which of those characters are the invoice number or the total, and it returns them as named, typed fields. For scanned documents, most extraction pipelines run OCR first.
LLMs handle varied layouts well, but they can return a plausible value that isn’t in the document. Use an API that ties each value to the source — with a page location, a match label, and a confidence score — and send anything ungrounded or low-confidence to review.
Schema extraction doesn’t need training data. You describe the fields in a JSON Schema, and the model finds them. Trained ML models do need labeled examples for every document type.
Yes. Claude and Codex can extract document data through an MCP server that connects them to an extraction API. Nutrient MCP adds extraction, parsing, and classification to Claude, Claude Code, and Codex, and it returns fields with citations and confidence from Nutrient Data Extraction API.