Extract data from documents

Intermediate · 7 min · Pull structured fields from documents with traceable, spot-checked values.

  • data extraction
  • verification

Extraction is not summarizing. You do not want readable prose about the contract — you want one row per contract with the same four values in the same four columns, each traceable to the sentence it came from. Where a summary compresses language, extraction copies values and refuses to interpret them.

The extraction contract

  • Name the columns before anything else: Supplier | Term end | Notice period | Renewal type.
  • One row per item — one contract, one invoice, one form. Not “the agreements”.
  • Copy, never compute. “Copy dates and periods verbatim; do not calculate or convert anything.” Model arithmetic is not a calculator; a normalized date is an invented date.
  • A quote column. Require the exact sentence each row’s values come from. Now every value can be checked with a glance, and values that cannot be quoted come back not stated.
  • Output only the rows. No commentary around the table — the structured-output rule — so it can be pasted straight into a sheet.

Why the quote column changes everything

An extraction without sources asks you to trust the machine on exactly the numbers that matter. With a source quote per row, verification shrinks from “re-read every contract” to “read six sentences”. It is the grounding pattern from the grounding lesson, applied to table building.

Then do the sampling pass

Extractions deserve a spot-check, not faith. Open the source for five rows — mix the first and last, not the cleanest ones — and compare against the quote. If one value drifted, stop and re-run with a stricter instruction; one wrong row means the batch is untrustworthy. And if you asked for totals: never accept a model-computed total. Extract the raw numbers and let the spreadsheet add them — it is a calculator.

Long documents

For a folder, work in chunks: one document per request (or a batch of similar ones), same columns every time, collecting rows you control. The workflow is the same one from working with long input, and your collected rows are the memory the chat does not have.

A sizing tip from practice: keep each extraction batch small enough to sample in five minutes — six contracts, a dozen invoices. Beyond that, the spot-check quietly turns into a second job, and unverified tables are exactly the ones that get trusted by accident. Small, verified batches beat one big unchecked pull.

A model returns a converted date in the Term end column and a computed total row at the bottom. What does the discipline say?

Practice

Assemble a document-extraction prompt

Six supplier contracts sit in a folder as plain text. Finance needs renewal and termination details in a spreadsheet by Friday — and every value must be traceable to the sentence it came from.

Fill every field to assemble a prompt that extracts one row per contract — then compare with a strong example.

Your prompt, assembled

 
Hint

Which column proves a value instead of asking you to trust it?

What rule stops the model from computing a date you did not ask it to compute?

Transfer

  • Contracts: renewal dates, notice periods, obligations — with quotes.
  • Invoices and receipts: amounts and dates into the expense tracker.
  • Forms and applications: fields into a comparison sheet, not stated where the form is silent.

Key takeaways

  • Name the columns first and demand one row per item, so the output has the shape you need.
  • Copy, never compute: a normalized date is an invented date, and totals belong to the spreadsheet.
  • Require a source quote per row and spot-check five rows; one drifted value condemns the batch.

Next

Next: Classify and triage text — when the pile of text needs sorting rather than fields.

Finished this lesson?