AI & Automation8 min readSeptember 09, 2026

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

AI & Systems Architect

Principal Artificial Intelligence Engineer, Natelad Agency

Automated Document Intelligence: Extracting Structured JSON from Invoices & Identity Documents

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.
Blueprint Table of Contents
  1. 01.The End of Manual Business Data Entry
  2. 02.Zod-Enforced Multimodal Extraction in TypeScript
  3. 03.Automated Math Verification Safeguards
01

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.

02

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.

Type-Safe Invoice Parser with Zod and Multimodal Vision API
typescript
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);
}
03

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.

ARCHITECTURAL TAGS:#Document Intelligence#OCR#Vision LLMs#Zod#Data Automation
AI & Systems Architect
Written By Lead Architect

AI & Systems Architect

Principal Artificial Intelligence Engineer at Natelad Agency. Architecting mission-critical digital systems, resilient infrastructure, and security frameworks for enterprises worldwide.

Consult Architect →
Engineering Consultation

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.