Kolkata ·  
AI engineering
workflows
deep · 6 min

Extraction

Pulling structured fields out of messy text, like the invoice number, amount and due date in an email.

updated 29 Sept 2026

Here's an email that lands in an accounts inbox:

Hi, invoice INV-2291 for $4,300 is due on 15 October. Please pay into the usual account and send the remittance to accounts@acme.example. Thanks, Priya (Acme Ltd)

We want one row out of it: invoice_number, amount, currency, due_date, vendor, contact_email. Pulling fields like these out of messy text is extraction, and it's about the plainest way to put a model to work inside other software.

The first version takes ten minutes. We write the shape down once as a Pydantic or Zod model, send the email with a request for structured output, and parse the reply into a typed object. Done?

Look at due_date for a second. The email says "15 October". Which year? The model will give us a perfectly valid date either way. If it guesses the year wrong, nothing errors. The row goes into the payments system and sits there being wrong.

That's the real shape of the problem. As of 2026-09, getting valid JSON out of a model is solved. Getting the right values, all of them, is where the work is. So let's go through what catches what.

Getting the JSON out

There have been three ways to do this, and we'll meet all of them in code we inherit.

Ask and parse. Describe the JSON in the prompt, parse what comes back, retry if it breaks. Libraries like Instructor wrap this: we pass a Pydantic model, it parses and validates the reply, and on an error it asks the model again, up to a retry limit. It works with 15+ providers, local models included. (The retry loop itself is schema validation.)

Force a fake tool. Before providers had native structured outputs, the trick on Claude was to define a "tool" whose inputs were the shape we wanted, force the model to call it, and read the arguments. Nothing ever runs. The arguments are the extraction. It's tool calling used for its schema, not for an action. On the newest Claude models, as of 2026-09, forcing a tool call returns a 400 error, so old code built this way breaks when we upgrade.

Native structured outputs. We send the schema and the provider guarantees the reply matches it. On Claude, JSON outputs are the feature meant for pulling data out of text or images.

What each layer catches

So now the JSON is always valid. What's left?

The schema handles shape. Some rules it can't hold: as of 2026-09, Claude's structured outputs don't enforce number ranges or string lengths, so "amount must be positive" is a check in our own code.

Then there's the bottom layer. A 2026 benchmark, LLMStructBench, ran 22 open models over about a thousand hand-checked extraction cases, and scored each output twice: is it valid, and is each value right? The prompting setup mattered more than model size for getting valid JSON. But the strictest setup, with the schema enforced and also spelled out in the prompt with an example, made small models safest at producing parseable output while giving more wrong values. Across every setup, wrong values were the biggest source of errors, ahead of missing keys and missing values. GPT-4o, added for comparison, showed no clear edge over the best open model.

So when we move to strict schemas, errors don't go away. They move from "can't parse" to "parsed fine, says the wrong thing", and the second kind is quieter. A wrong value flows into the next system with no error at all.

A caveat: that benchmark ran open models locally, through Ollama, and not the hosted strict modes. It's a reason to measure, not a verdict on those APIs.

Schema habits that help

A few choices in the schema itself cut down the quiet errors.

Let fields be empty. A model will always try to fill the shape it's given. If the email has no due date and the field is required, we may get one anyway, made up (hallucination in a tidy wrapper). So due_date should be nullable, and the prompt should say when to leave it null.

Use lists for things that repeat. For people or companies in a text, a list of objects with name, type and the surrounding context fits any number of them. person_1, person_2 doesn't.

Ask for evidence. Next to each value, a field with the exact words it came from. That gives us something to check, which is the next part.

Tie each value to where it came from

Google's LangExtract library maps every extracted item back to its exact character position in the source. That buys two things. First, a cheap check for invented values: if the text at that position isn't what the model claimed, or the quoted evidence isn't in the document at all, we reject it. Second, a person reviewing can see each value highlighted in the original, which makes checking by eye fast. It's grounding, applied to fields instead of sentences.

One catch on Claude: its built-in citations feature can't be combined with JSON outputs (the API returns an error). If we want evidence in a structured reply, we ask for it as a field.

When the email is a 200-page contract

One short email is easy. What about a long document with fifty things to pull out? A model can take a million tokens at once, but tests at that length show that finding many facts in a long input is weaker than finding one (more in long context and lost in the middle).

LangExtract's answer is to stop asking the model to do it all at once.

It splits the document into chunks, extracts from them in parallel, and makes several passes over each smaller piece. Several passes can find the same item twice, so the results get merged at the end.

Scoring it

How do we know if it's working? We score it the way we'd score a classifier, on a labelled set of real documents:

  • Recall: of the values that should have come out, how many did?
  • Precision: of the values that came out, how many are right?

Per field, not overall. An extractor that nails amount and gets due_date wrong one time in five has a due-date problem, and an average across all six fields hides it. Counting errors by kind helps too, since missing keys, missing values and wrong values each have a different fix.

And if we use a confidence score to decide what goes to a person, the spread of the scores matters more than their average. If most of them sit between 0.4 and 0.6, a small shift in where we draw the line swings precision or recall a lot, and there's no safe place to draw it.

Back to "15 October". Which layer catches the wrong year? Not the schema; it's a valid date. Not the evidence check, since "15 October" really is in the email. Only a labelled set where someone wrote down the right answer. And no public benchmark can stand in for that, because how well extraction works depends on our documents, our fields and how vague the text is. That set is the one part we have to build ourselves (evals).

Further reading

  • Structured outputs, Anthropic, undated. JSON outputs for extraction, and the list of constraints the schema won't enforce.
  • Extracting Structured JSON using Claude and Tool Use, Alex Albert (Anthropic), 2024. The fake-tool method with five examples. Worth knowing so you can read older code.
  • Define tools, Anthropic, undated. Which Claude models refuse forced tool calls, and what to use instead.
  • Instructor, Jason Liu and contributors. A Pydantic model in, a checked object out. The quick start is the whole idea in ten lines.
  • LLMStructBench, Tenckhoff, Koddenbrock and Rodner, 2026. Valid structure and correct values, scored separately, pulling apart.
  • Introducing LangExtract, Akshay Goel and Atilla Kiraly (Google), 2025. Character positions for every value, and several passes over long documents.
  • Task-Specific LLM Evals that Do & Don't Work, Eugene Yan, 2024. How to score extraction, and how good averages can hide a model you can't use.