/api/ocr-structured-v4 endpoint provides the most advanced invoice extraction with comprehensive India GST support, multi-page PDF processing, and sophisticated reconciliation logic.
Endpoint
Request Body
Base64-encoded image data or data URL. Required if
pdfUrl and pdfBase64 are not provided.Formats:- Bare base64:
iVBORw0KGgoAAAANS... - Data URL:
data:image/png;base64,iVBORw0KGgo...
MIME type of the image (e.g.,
image/png, image/jpeg, image/webp)Publicly accessible URL to a PDF document. Required if
imageBase64 and pdfBase64 are not provided.Base64-encoded PDF data or data URL. Required if
imageBase64 and pdfUrl are not provided.Optional filename for PDF documents
Override the default model. Default:
google/gemini-2.0-flash if OPENROUTER_MODEL not setPass-through OpenRouter annotations to avoid re-parsing costs on subsequent requests
Override PDF parsing plugins. Advanced use only.Default:
Response Schema
The v4 schema provides comprehensive invoice data with normalized GST calculations:Document Level
Invoice header metadata
Items Array
Line items with detailed breakdowns
Header Discounts
Invoice-level discounts applied before charges and GST
Charges
Additional charges (Freight, Packing, Insurance, etc.)
TCS
Tax Collected at Source
Round-Off
Rounding adjustment (typically ±1.00)
Totals
Computed invoice totals
Printed Values
Values extracted from printed subtotals/tables
Reconciliation
Calculation verification results
Metadata
Extraction metadata
Example Requests
Example Response
Success (200) - Excerpt
Reconciliation Logic
The v4 reconciliation engine (lib/invoice_v4.ts) performs sophisticated validation:
- Price Mode Detection: Determines if printed rates are WITH_TAX or WITHOUT_TAX by comparing with GST summary
- Sequential Discounts: Applies d1 then d2:
effective = d1 + d2 - (d1 × d2) - Line Calculation:
line_ex_tax = qty × rate_ex_tax × (1 - effective_pct) - flat_discount - Header Discounts: Applied to subtotal before charges and GST
- Charges: Added with GST (inherits weighted avg rate if not specified)
- TCS: Applied after GST
- Round-Off: Applied last
- Validation: Compares against printed totals and HSN tables
- Alternate Strategies: Tests multiple calculation approaches, selects lowest error
Schema Constraints
The system prompt enforces:- GST Slabs: 0, 0.25, 3, 5, 12, 18, 28 (as percentages, not decimals)
- UQC Codes: Valid GST unit codes only (NOS, PCS, KGS, LTR, MTR, etc.)
- Date Format: YYYY-MM-DD (ISO 8601)
- Decimal Precision: 2 decimals in final JSON, higher precision internally
- Unknown Values:
nullwith reduced confidence - Round-Off: Expected |round_off| ≤ 1.00 unless explicitly printed
Advanced Features
Multi-Page PDF Processing
For PDFs, the endpoint:- Processes all pages
- Prefers HSN tax tables and summaries (often on page 2+)
- Uses tables as reconciliation anchors
- Sets
meta.pages_processedcount
Price Mode Ambiguity
When unclear if rates include GST:- Tests BOTH
WITH_TAXandWITHOUT_TAXmodes - Compares each against printed totals, HSN tables, and subtotals
- Prefers mode with lowest absolute error AND small round-off (≤ 1.00)
- Stores detected mode in
raw.price_mode_detected
Confidence Scoring
Each item includesconfidence (0.0-1.0) based on:
- OCR quality of relevant fields
- Presence of validation anchors (HSN table, subtotals)
- Calculation consistency
meta.overall_confidenceaggregates across document
OpenRouter Annotations
To avoid re-parsing costs on retries:- First request processes the PDF
- OpenRouter returns
annotationsin response metadata - Pass
annotationsin subsequent requests to reuse parsed content - Significantly faster and cheaper for large PDFs
Migration from Legacy
Upgrading from/api/ocr-structured:
| Legacy Field | V4 Equivalent | Notes |
|---|---|---|
voucher.invoice_number | doc_level.invoice_number | Direct mapping |
voucher.invoice_date | doc_level.invoice_date | Format: dd-mm-yyyy → YYYY-MM-DD |
items[].price | items[].rate_ex_tax | Same concept |
items[].tax_rate | items[].gst.rate | Percentage (18, not 0.18) |
items[].discount_rate | items[].discount.effective_pct | V4 supports multi-level |
voucher.invoice_discount | header_discounts[] | Array for sequential discounts |
voucher.additional_charges | charges[] | Enhanced structure |
party.* | Not in v4 | Extract from buyer_gstin if needed |
voucher.reconciliation.status | reconciliation.error_absolute | Numeric error instead of binary status |
Best Practices
Use PDF for Multi-Page Invoices
PDFs preserve better quality and allow processing multiple pages with tax summaries
Monitor Reconciliation Error
Accept
error_absolute ≤ 1.00 as normal rounding, investigate larger differencesLimitations
- Requires India GST format (GSTIN, HSN, CGST/SGST/IGST)
- Optimized for myBillBook-style invoices
- May struggle with highly custom layouts
- Confidence scoring requires validation anchors
- TCS support is basic (single rate, single base)
Next Steps
Reconciliation Engine
Deep dive into v4 reconciliation logic
Integration Guide
Build a complete v4 extraction workflow
UI Components
Display v4 data with pre-built components
Testing
Test your v4 integration
