

Schema [#schema]

<SchemaViewer
  schema={{
"type": "object",
"title": "Invoice Schema",
"description": "Schema for extracting structured data from invoice documents",
"properties": {
  "invoice_number": { 
    "type": "string",
    "description": "Unique identifier for the invoice"
  },
  "vendor": { 
    "type": "string",
    "description": "Company or individual issuing the invoice"
  },
  "invoice_date": { 
    "type": "string",
    "format": "date",
    "description": "Date the invoice was issued"
  },
  "due_date": { 
    "type": "string",
    "format": "date",
    "description": "Payment due date"
  },
  "currency": { 
    "type": "string",
    "enum": ["USD", "EUR", "GBP", "JPY"],
    "description": "Currency code for all monetary values"
  },
  "line_items": {
    "type": "array",
    "description": "List of items or services billed",
    "items": {
      "type": "object",
      "properties": {
        "description": { 
          "type": "string",
          "description": "Description of the line item"
        },
        "quantity": { 
          "type": "number",
          "minimum": 0,
          "description": "Quantity of items"
        },
        "unit_price": { 
          "type": "number",
          "minimum": 0,
          "description": "Price per unit"
        },
        "total": { 
          "type": "number",
          "minimum": 0,
          "description": "Total amount for this line item"
        }
      },
      "required": ["description", "total"]
    }
  },
  "subtotal": { 
    "type": "number",
    "minimum": 0,
    "description": "Sum of all line item totals before tax"
  },
  "tax": { 
    "type": "number",
    "minimum": 0,
    "description": "Tax amount applied to the subtotal"
  },
  "total": { 
    "type": "number",
    "minimum": 0,
    "description": "Final amount due (subtotal + tax)"
  }
},
"required": ["invoice_number", "vendor", "total"]
}}
/>

CLI approach [#cli-approach]

Single invoice:

```bash
struktur --input invoice.pdf \
  --schema invoice-schema.json \
  --model openai/gpt-4o-mini
```

With embedded images (for invoices with stamps, logos, or handwritten amounts):

```bash
struktur --input invoice.pdf --images \
  --schema invoice-schema.json \
  --model openai/gpt-4o
```

Multiple invoices:

```bash
for file in invoices/*.pdf; do
  struktur --input "$file" \
    --schema invoice-schema.json \
    --model openai/gpt-4o-mini \
    --output "outputs/$(basename $file .pdf).json"
done
```

SDK [#sdk]

Small invoices (1-3 pages):

```js
import { extract, simple, parse } from "@struktur/sdk";
import { openai } from "@ai-sdk/openai";

const artifacts = await parse(
  { kind: "file", path: "invoice.pdf" },
  { includeImages: true }
);

const result = await extract({
  artifacts,
  schema: invoiceSchema,
  strategy: simple({ model: openai("gpt-4o-mini") }),
});
```

Multi-page invoices with many line items:

```js
import { extract, sequentialAutoMerge, parse } from "@struktur/sdk";
import { openai } from "@ai-sdk/openai";

const artifacts = await parse({ kind: "file", path: "invoice.pdf" });

const result = await extract({
  artifacts,
  schema: invoiceSchema,
  strategy: sequentialAutoMerge({
    model: openai("gpt-4o-mini"),
    dedupeModel: openai("gpt-4o-mini"),
    chunkSize: 8000,
  }),
});
```

Strategy choice [#strategy-choice]

| Invoice type                         | Strategy              |
| ------------------------------------ | --------------------- |
| 1-3 pages                            | `simple`              |
| Multi-page, line items may duplicate | `sequentialAutoMerge` |
| Many invoices in parallel            | `parallelAutoMerge`   |

Expected output [#expected-output]

```json
{
  "invoice_number": "1042",
  "vendor": "Acme Corp",
  "invoice_date": "2024-03-01",
  "due_date": "2024-04-01",
  "currency": "USD",
  "line_items": [
    { "description": "Widget A", "quantity": 10, "unit_price": 50, "total": 500 },
    { "description": "Widget B", "quantity": 5, "unit_price": 200, "total": 1000 }
  ],
  "subtotal": 1500,
  "tax": 150,
  "total": 1650
}
```

See also [#see-also]

* [Extraction Strategies](/docs/explanation/strategies) — strategy reference
* [Process a Directory of Files](/docs/examples/process-directory) — batch processing
