Read an invoice
Use the model to read line items. Keep arithmetic and approval limits in code.
Configure the reader
Install the SDK and set NULLPROTOCOL_KEY as in the quickstart. Create an invoice-reader agent with instructions: “Read invoice lines exactly as supplied. Do not calculate missing prices or invent products.” This example receives text; extracting text from a PDF belongs to your application.
Extract prices and calculate the total
invoice.py
from decimal import Decimal, ROUND_HALF_UP
from nullprotocol import NullProtocol
with NullProtocol() as client:
agent = client.load("invoice-reader")
result = agent.extract(
"Invoice INV-42. Blue mug: qty 2 at EUR 12.50. Tea tin: qty 1 at EUR 8.00.",
{
"type": "object",
"properties": {
"invoiceId": {"type": "string"},
"items": {
"type": "array", "minItems": 1,
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"quantity": {"type": "integer", "minimum": 1},
"unitPrice": {"type": "number", "minimum": 0},
},
"required": ["name", "quantity", "unitPrice"],
"additionalProperties": False,
},
},
},
"required": ["invoiceId", "items"],
"additionalProperties": False,
},
)
if not result.success:
raise RuntimeError(result.error)
total = sum(
item["quantity"] * Decimal(str(item["unitPrice"])).quantize(
Decimal("0.01"), rounding=ROUND_HALF_UP,
)
for item in result.data["items"]
)
print(result.data["invoiceId"], total)The supplied lines total EUR 33.00. This example uses two-decimal prices and integer quantities. Apply your accounting system’s rounding, tax and currency rules for real invoices.
Review before posting
Check the invoice ID against duplicates, verify vendor details and route amounts over your approval limit to a person. A structurally valid extraction is not permission to pay.