Automated Document Intelligence: Extracting Structured JSON from Invoices & Identity Documents
Combining multimodal Vision LLMs with Tesseract OCR and Zod schema validation for 99.8% extraction accuracy.

AI & Systems Architect
Principal Artificial Intelligence Engineer, Natelad Agency
Executive & Architectural Key Takeaways
- ✓Traditional template-based OCR breaks whenever a vendor changes their invoice font or table column spacing; Vision LLMs adapt dynamically.
- ✓Enforcing strict JSON schema generation with tool calling or Zod parsers guarantees the output matches your database model.
- ✓Pre-processing images (perspective correction, adaptive thresholding, and deskewing) increases OCR character accuracy by over 30%.
- ✓Implementing automated mathematical reconciliation (sum of line items == total tax + subtotal) flags extraction anomalies automatically.
- 01.The End of Manual Business Data Entry
- 02.Zod-Enforced Multimodal Extraction in TypeScript
- 03.Automated Math Verification Safeguards
The End of Manual Business Data Entry
Accounts payable departments and compliance teams spend thousands of hours each month manually typing numbers from scanned supplier PDFs into accounting systems. A single typing error can lead to duplicate payments or audit non-compliance.
Modern document intelligence pipelines use multimodal LLMs to visually inspect documents, extract line items, and validate amounts against structured schemas with near-perfect reliability.
Zod-Enforced Multimodal Extraction in TypeScript
Below is a production pipeline that passes a base64-encoded invoice image to a vision model with guaranteed structured JSON output.
import { z } from 'zod';
export const InvoiceSchema = z.object({
invoiceNumber: z.string(),
vendorName: z.string(),
taxIdentificationNumber: z.string().optional(),
invoiceDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
lineItems: z.array(z.object({
description: z.string(),
quantity: z.number().positive(),
unitPrice: z.number().nonnegative(),
totalPrice: z.number().nonnegative()
})),
subtotal: z.number(),
vatRate: z.number(),
totalAmountDue: z.number()
});
export type ExtractedInvoice = z.infer<typeof InvoiceSchema>;
export async function parseInvoiceImage(imageBase64: string): Promise<ExtractedInvoice> {
const response = await aiClient.chat.completions.create({
model: "gpt-4o-mini",
response_format: { type: "json_object" },
messages: [
{
role: "system",
content: "Extract invoice data matching the JSON schema precisely. Calculate mathematical sums."
},
{
role: "user",
content: [
{ type: "text", text: "Extract all structured line items and totals." },
{ type: "image_url", image_url: { url: `data:image/jpeg;base64,${imageBase64}` } }
]
}
]
});
const parsedJson = JSON.parse(response.choices[0].message.content!);
return InvoiceSchema.parse(parsedJson);
}Automated Math Verification Safeguards
Before inserting extracted invoice data into SAP, QuickBooks, or internal ERPs, an automated validation function recalculates: lineItems.reduce((acc, item) => acc + item.totalPrice, 0). If the calculated sum differs from totalAmountDue by more than 0.02, the document is routed to human review.

AI & Systems Architect
Principal Artificial Intelligence Engineer at Natelad Agency. Architecting mission-critical digital systems, resilient infrastructure, and security frameworks for enterprises worldwide.
Related Production Blueprints
View All Blueprints →Engineering Resilient Multi-WAN Networks: Combining Starlink Satellites with Dedicated Fibre
How we design mission-critical telecommunication grids for remote mining, banking, and industrial facilities by pairing high-throughput terrestrial fibre with low-earth-orbit (LEO) satellite redundancy.
Starlink Bypass Mode & MikroTik Integration: The Definitive Commercial Setup Guide
Step-by-step technical guide to putting Starlink Gen 2 and Gen 3 dishes into bypass mode, eliminating double-NAT issues, and configuring enterprise MikroTik routers for commercial offices.
Implement this architecture with Natelad’s dedicated engineering pods.
Book a technical discovery session with our lead architects to evaluate your infrastructure, review security posture, or scope a new platform sprint.