Extracting invoice fields by hand is slow, and asking a model for free text gives you numbers you must parse again. Structured Outputs lets you send the invoice text with a schema for the fields you need, and the reply comes back as JSON your code can read. The schema guarantees the shape; you still check the arithmetic yourself.
Prerequisites
- The invoice text, as plain text.
- A JSON Schema with the fields you need: invoice number, date, line items and total.
- A calculator or a short script to check the line items add up to the total.
Steps
Step 1: Define the invoice schema
Write a schema with an invoice_number, a date, an array of line items (each with description, quantity and price) and a total. Mark the total required so the model always supplies one.
Step 2: Send the invoice text
Send the schema as the response format and the invoice text as the input. Ask for nothing else in the reply, so the JSON is the whole answer.
Step 3: Check the totals
Multiply each line item’s quantity by its price and add the lines. Compare the sum with the model’s total. If they differ, the extraction is wrong even though the JSON is well formed.
Worked example: Bluebell Bakery supplier invoice
The fictional Bluebell Bakery extracts a flour invoice with 3 line items. The model returns the JSON below.
| Line item | Quantity | Unit price | Line total |
|---|---|---|---|
| Flour, strong white, 25 kg | 12 | 4.50 | 54.00 |
| Flour, wholemeal, 25 kg | 5 | 12.00 | 60.00 |
| Yeast, dried, 500 g | 2 | 25.00 | 50.00 |
| Invoice total | 164.00 |
The line totals are 54.00, 60.00 and 50.00, which add to 164.00, matching the model’s total, so the extraction passes the arithmetic check.
Expected result. The reply is JSON matching the schema, and the line items add up to the stated total. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.
Limits
- The model can misread a number from the invoice; the arithmetic check catches some errors, not all of them.
- A scanned invoice must be converted to text first; this guide starts from plain text.
- Structured Outputs guarantees the shape, not the values.
