CookbooksOCR Cookbooks
Extract Line Items from Invoices
Turn PDF invoices into structured JSON line items you can validate and post to accounting.
- Request
jsonoutput and read line items fromblocks - Parse descriptions, quantities, and prices into clean records
- Validate subtotal, tax, and grand total in Python
Time to complete: ~15 minutes
Prerequisites
- A
SAGEA_API_KEYexported as an environment variable - An invoice file in PDF, PNG, JPG, TIFF, or WEBP format, max 100MB, ideally 300 DPI
- Python 3.9+ with the
requestspackage installed - Set up document processing
- ARVA OCR model card
Step 1: Send the invoice to ARVA OCR
Request output_format="json" so you get text, pages, confidence, and blocks back.
Expected JSON shape:
ARVA OCR reaches 99.2% accuracy on clean scans. Standard pages cost $1.00 per 1000 pages.
Step 2: Parse blocks into line items
Filter blocks for line-item rows and convert them to dictionaries.
Step 3: Validate totals before posting
Recompute subtotal and grand total and compare against the extracted total.
Verify
| Error | Cause | Fix |
|---|---|---|
401 Unauthorized | Missing or invalid API key | Export a valid key as SAGEA_API_KEY and retry |
400 unsupported_format | File type not in PDF, PNG, JPG, TIFF, WEBP | Convert the invoice to PDF or PNG and retry |
413 Payload Too Large | File is over 100MB | Compress or split the PDF, then retry |
429 rate_limit_exceeded | Too many requests at once | Back off with retries and reduce concurrency |
