This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/dws-data-extraction/examples/build-document-extraction-pipeline.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Build a document extraction pipeline for invoices and forms

This tutorial builds a Python pipeline that extracts structured data from invoices and forms using the Data Extraction API’s spatial element output. Unlike Markdown extraction, spatial output returns typed elements — tables with cell coordinates and paragraphs with semantic roles — with bounding boxes and confidence scores. To pull named fields such as the invoice number and total, the pipeline also calls the extract endpoint with a schema.

What you’ll build

A Python script that:

  1. Sends a PDF invoice to the Data Extraction API
  2. Receives typed spatial elements with bounding boxes
  3. Extracts named fields (invoice number, date, total, etc.) with the extract endpoint and a schema
  4. Extracts table data (line items) into structured rows
  5. Outputs a clean JSON summary ready for downstream systems

When to use spatial vs. Markdown

Use caseOutput format
Invoice tables, form layoutSpatial (output.format: "spatial")
Named fields (invoice number, total)Extract endpoint with a schema
RAG pipelines, search indexingMarkdown (output.format: "markdown")
Document analysis with spatial dataSpatial
Content migrationMarkdown

This tutorial uses spatial output for tables and the extract endpoint for named fields. For Markdown-based RAG pipelines, refer to information on how to build a RAG ingestion pipeline.

Prerequisites

Project setup

Terminal window
mkdir invoice-extraction && cd invoice-extraction
python -m venv .venv && source .venv/bin/activate
pip install requests python-dotenv

Create a .env file:

Terminal window
NUTRIENT_API_KEY=your_data_extraction_api_key_here

Step 1 — Extract elements from a document

Send a PDF to the Data Extraction API with output.format: "spatial" to receive typed elements:

extract.py
import json
import os
import sys
import requests
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.environ["NUTRIENT_API_KEY"]
ENDPOINT = "https://api.nutrient.io/extraction/parse"
def extract_elements(pdf_path: str, include_words: bool = False) -> dict:
"""Extract structured elements from a PDF."""
instructions = {
"mode": "understand",
"output": {"format": "spatial", "includeWords": include_words},
}
with open(pdf_path, "rb") as f:
response = requests.post(
ENDPOINT,
headers={"Authorization": f"Bearer {API_KEY}"},
files={"file": f},
data={"instructions": json.dumps(instructions)},
)
response.raise_for_status()
return response.json()

Step 2 — Extract named fields with a schema

The parse endpoint returns document structure, not named fields. To pull specific values such as the invoice number or total, call the extract endpoint with a JSON Schema that describes the fields you want. The response returns a typed object under output.data:

EXTRACT_ENDPOINT = "https://api.nutrient.io/extraction/extract"
INVOICE_SCHEMA = {
"type": "object",
"properties": {
"invoice_number": {"type": "string", "description": "Invoice identifier"},
"date": {"type": "string", "description": "Invoice date"},
"due_date": {"type": "string", "description": "Payment due date"},
"total": {"type": "number", "description": "Total amount including tax"},
},
"required": ["invoice_number", "total"],
}
def extract_fields(pdf_path: str) -> dict:
"""Extract named invoice fields with the extract endpoint and a schema."""
outer_instructions = {
"schema": INVOICE_SCHEMA,
"parseConfig": {"mode": "understand"},
}
with open(pdf_path, "rb") as f:
response = requests.post(
EXTRACT_ENDPOINT,
headers={"Authorization": f"Bearer {API_KEY}"},
files={"file": f},
data={"instructions": json.dumps(outer_instructions)},
)
response.raise_for_status()
return response.json()["output"]["data"]

For an invoice, this returns a typed object that matches the schema:

{
"invoice_number": "INV-2024-0042",
"date": "2024-03-15",
"due_date": "2024-04-15",
"total": 1247.5
}

Step 2 is a second, separately billed request. With parseConfig.mode: "understand", it costs $0.015 per page ($0.009 for parsing plus $0.006 for extraction), on top of the $0.009 per page for Step 1. Refer to pricing for details.

To define richer schemas and ground each value with per-field citations, refer to the schema-based extraction tutorial.

Step 3 — Extract table data

Table elements include cell-level data with row/column positions, spans, and text content:

def extract_tables(elements: list[dict]) -> list[list[list[str | None]]]:
"""Convert table elements into row/column arrays, expanding spans."""
tables = []
for el in elements:
if el["type"] != "table":
continue
rows: list[list[str | None]] = [[None] * el["columnCount"] for _ in range(el["rowCount"])]
for cell in el["cells"]:
r, c = cell["row"], cell["column"]
text = cell["text"]
row_span = cell.get("rowSpan", 1)
col_span = cell.get("colSpan", 1)
for dr in range(row_span):
for dc in range(col_span):
ri, ci = r + dr, c + dc
if ri < el["rowCount"] and ci < el["columnCount"]:
rows[ri][ci] = text
tables.append(rows)
return tables

For an invoice with line items, this produces:

[
[
["Item", "Quantity", "Unit Price", "Total"],
["Widget A", "10", "$25.00", "$250.00"],
["Widget B", "5", "$99.50", "$497.50"],
["Service Fee", "1", "$500.00", "$500.00"]
]
]

Step 4 — Classify paragraphs by role

Paragraph elements include a role field that identifies titles, section headers, captions, footers, and other semantic categories:

def extract_paragraphs_by_role(
elements: list[dict],
) -> dict[str, list[str]]:
"""Group paragraph text by semantic role."""
by_role: dict[str, list[str]] = {}
for el in elements:
if el["type"] != "paragraph":
continue
role = el.get("role") or "Unknown"
by_role.setdefault(role, []).append(el["text"])
return by_role

Step 5 — Put it all together

def process_invoice(pdf_path: str) -> dict:
"""Extract structured data from an invoice PDF."""
result = extract_elements(pdf_path)
if result.get("status") != 200:
raise RuntimeError(
f"API error {result['status']}: {result.get('errorMessage')}"
)
elements = result["output"]["elements"]
return {
"fields": extract_fields(pdf_path),
"tables": extract_tables(elements),
"paragraphs": extract_paragraphs_by_role(elements),
"metrics": result["metrics"],
"element_count": len(elements),
}
if __name__ == "__main__":
pdf = sys.argv[1] if len(sys.argv) > 1 else "invoice.pdf"
data = process_invoice(pdf)
print(json.dumps(data, indent=2))

Run it:

Terminal window
python extract.py invoice.pdf

Example output:

{
"fields": {
"invoice_number": "INV-2024-0042",
"date": "2024-03-15",
"total": 1247.5
},
"tables": [
[
["Item", "Quantity", "Unit Price", "Total"],
["Widget A", "10", "$25.00", "$250.00"],
["Widget B", "5", "$99.50", "$497.50"]
]
],
"paragraphs": {
"Title": ["Invoice"],
"Text": ["Payment terms: Net 30 days."]
},
"metrics": {
"processingTimeMs": 3800,
"pagesProcessed": 1
},
"element_count": 12
}

Using word-level data

Set includeWords: true to get word-level bounding boxes inside paragraphs and table cells. This is useful for building document overlays or highlighting matched text.

Terminal window
curl -X POST https://api.nutrient.io/extraction/parse \
-H "Authorization: Bearer your_api_key_goes_here" \
-F "file=@invoice.pdf" \
-F 'instructions={"mode":"understand","output":{"format":"spatial","includeWords":true}}'

Structure mode for cost-effective processing

Use structure mode when you need spatial elements at lower cost ($0.0015 per page vs. $0.009 for understand mode). It runs OCR-based segmentation without AI augmentation:

instructions = {
"mode": "structure",
"output": {"format": "spatial"},
}

Structure mode works well for simple invoices and forms with clear layouts. Use understand mode for complex documents with multicolumn layouts, nested tables, or formulas. See processing modes for a full comparison.

Processing documents from URLs

If your documents are hosted at public URLs, skip the file upload:

def extract_from_url(url: str) -> dict:
"""Extract elements from a document at a public URL."""
response = requests.post(
ENDPOINT,
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
},
json={
"url": url,
"mode": "understand",
"output": {"format": "spatial"},
},
)
response.raise_for_status()
return response.json()

Batch processing

Process a folder of invoices and output results as JSONL:

import pathlib
def batch_process(folder: str = "invoices", output: str = "results.jsonl"):
"""Process all PDFs in a folder and write results as JSONL."""
out_path = pathlib.Path(output)
with open(out_path, "w") as out:
for pdf in sorted(pathlib.Path(folder).glob("*.pdf")):
print(f"Processing {pdf.name}...")
try:
data = process_invoice(str(pdf))
data["source"] = pdf.name
out.write(json.dumps(data) + "\n")
except Exception as e:
print(f" Error: {e}")
print(f"Results written to {out_path}")

Production considerations

  • Confidence filtering — Each element and cell includes a confidence score (0–1). Filter low-confidence elements for higher-accuracy results.
  • Spatial validation — Use bounds coordinates to verify extracted fields are in expected regions of the document (e.g. totals at the bottom, dates at the top).
  • Multilingual documents — Set options.language for non-English invoices. See multilingual extraction.

Element types

Refer to the guide on how to extract document elements for the complete element type schema, including all fields, roles, and response structure.