Zero-Failure Structured Output: Extracting Complex Tables and Handwritten Receipts with Vision LLMs

Zero-Failure Structured Output: Extracting Complex Tables and Handwritten Receipts with Vision LLMs

(Updated: ) ๐Ÿ“– 8 min read

Enterprise document processing has long been plagued by the fragility of traditional optical character recognition (OCR). Whether dealing with crumpled paper receipts retrieved from glove compartments, faint thermal paper invoices, field dispatch notes scribbled in ballpoint pen, or dense multi-column financial statements with irregular row spans, traditional OCR tools like Tesseract and legacy cloud APIs consistently produce garbage outputs when document layouts deviate from pristine synthetic templates.

The advent of native multimodal modelsโ€”specifically Gemini 2.5 Flash, Claude 3.5 Sonnet, and GPT-4oโ€”has fundamentally shifted document intelligence from fragile coordinate-based OCR to semantic visual reasoning.

However, deploying Vision LLMs in production financial pipelines introduces a new risk: hallucinations and arithmetic drift. If an LLM misreads a faint $18.50 as $78.50 and invents an arbitrary tax number to force the columns to balance, downstream accounting and ERP ledgers break.

This guide provides an end-to-end, production-grade architecture for Zero-Failure Document Extraction: combining OpenCV image preprocessing, strict Pydantic mathematical validators, and automated self-healing feedback loops.


The End-to-End Zero-Failure Architecture

To eliminate hallucinations and transcription failures, we separate document ingestion into five isolated, deterministic stages:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Raw Document / Scan    โ”‚ (JPEG, PNG, HEIC, Multi-page PDF)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚
            โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ OpenCV Preprocessing   โ”‚ โž” Perspective Correction, Deskewing,
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   Bilateral Denoising, CLAHE Contrast Boost
            โ”‚
            โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Multimodal Vision LLM  โ”‚ โž” Gemini 2.5 Flash / Claude 3.5 Sonnet
โ”‚ Structured Extraction  โ”‚   temperature=0.0, strict JSON schema
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚
            โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Pydantic Validation &  โ”‚ โž” Row-sum arithmetic check
โ”‚ Invariant Verification โ”‚ โž” Tax percentage tolerance check
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โž” Mandatory field regex validation
            โ”‚
      โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
      โ”‚ Valid?                โ”‚
      โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
      โ”‚ NO            โ”‚ YES   โ”‚
      โ–ผ               โ–ผ       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Feedback Loop โ”‚  โ”‚ Clean, Validated JSON  โ”‚
โ”‚ Re-prompt LLM โ”‚  โ”‚ Dispatched to ERP/DB   โ”‚
โ”‚ with Pydantic โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚ Error Details โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
        โ”‚ (Max 2 retries)
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Re-invoke Vision LLM

1. Computer Vision Preprocessing: Fixing Skew and Thermal Fade

Multimodal LLMs possess impressive visual perception, but feeding them a raw 12-megapixel skewed smartphone photo consumes excessive tokens, increases latency, and increases error rates on faint handwriting.

A lightweight OpenCV preprocessing step normalizes incoming documents:

  1. Auto-Deskew: Correcting page rotation using minimum-area bounding rectangles.
  2. Contrast Limited Adaptive Histogram Equalization (CLAHE): Amplifying faded ink on thermal receipts without over-saturating highlights.
  3. Smart Downscaling: Rescaling documents to stay within optimal vision patch resolutions (typically 1500โ€“2000px on the longest edge).
import cv2
import numpy as np

def preprocess_document_image(image_bytes: bytes) -> bytes:
    """Deskew, enhance contrast, and normalize resolution for vision LLMs."""
    # Decode bytes to OpenCV BGR image
    nparr = np.frombuffer(image_bytes, np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    
    if img is None:
        raise ValueError("Could not decode image bytes")

    # 1. Convert to grayscale for contour & deskew analysis
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 2. Estimate skew angle using edge detection and Hough transform or threshold contours
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    
    coords = np.column_stack(np.where(thresh > 0))
    if len(coords) > 0:
        angle = cv2.minAreaRect(coords)[-1]
        # minAreaRect returns angle in [-90, 0); adjust to [-45, 45]
        if angle < -45:
            angle = -(90 + angle)
        else:
            angle = -angle
        
        # Rotate only if skew is noticeable (> 0.5 degrees and < 45 degrees)
        if 0.5 < abs(angle) < 45:
            (h, w) = img.shape[:2]
            center = (w // 2, h // 2)
            M = cv2.getRotationMatrix2D(center, angle, 1.0)
            img = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 3. Apply CLAHE (Contrast Limited Adaptive Histogram Equalization)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    enhanced_gray = clahe.apply(gray)
    
    # Merge enhanced luminance back to color format for LLM context
    enhanced_bgr = cv2.cvtColor(enhanced_gray, cv2.COLOR_GRAY2BGR)

    # 4. Resize to max dimension of 1800px to balance clarity with inference latency
    h, w = enhanced_bgr.shape[:2]
    max_dim = 1800
    if max(h, w) > max_dim:
        scale = max_dim / float(max(h, w))
        enhanced_bgr = cv2.resize(enhanced_bgr, (int(w * scale), int(h * scale)), interpolation=cv2.INTER_AREA)

    # Encode back to high-quality JPEG
    success, encoded_img = cv2.imencode(".jpg", enhanced_bgr, [int(cv2.IMWRITE_JPEG_QUALITY), 92])
    if not success:
        raise RuntimeError("Failed to re-encode preprocessed image")
    return encoded_img.tobytes()

2. Invariant-Enforcing Pydantic Schema

The core vulnerability of generative models in financial extraction is numerical inconsistency: the LLM may transcribe line item prices accurately but fabricate the subtotal, or vice-versa.

We protect the pipeline by coding mathematical invariants into the Pydantic schema using @model_validator(mode="after"). If the line items do not sum to the subtotal within an acceptable rounding threshold (e.g., 5 cents to accommodate varying rounding rules), the model rejects the data instantly.

from decimal import Decimal, ROUND_HALF_UP
from typing import List, Optional
from pydantic import BaseModel, Field, field_validator, model_validator
import re

class ExtractedLineItem(BaseModel):
    item_code: Optional[str] = Field(None, description="SKU, UPC, or inventory code if legible")
    description: str = Field(..., description="Full text description of item or service")
    quantity: Decimal = Field(..., description="Quantity purchased or hours billed")
    unit_price: Decimal = Field(..., description="Unit price per item")
    total_amount: Decimal = Field(..., description="Total line amount (quantity * unit_price)")

    @field_validator("quantity", "unit_price", "total_amount", mode="before")
    def clean_decimal(cls, v):
        if isinstance(v, (int, float)):
            return Decimal(str(v))
        if isinstance(v, str):
            # Strip currency symbols and extraneous whitespace
            cleaned = re.sub(r"[^\d.-]", "", v.strip())
            return Decimal(cleaned) if cleaned else Decimal("0.00")
        return v

    @model_validator(mode="after")
    def verify_line_math(self):
        expected_total = (self.quantity * self.unit_price).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
        actual_total = self.total_amount.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
        
        # Tolerate small roundoff discrepancies (up to $0.05)
        if abs(expected_total - actual_total) > Decimal("0.05"):
            # If the LLM captured the line total accurately but quantity was 1, auto-reconcile
            if self.quantity == Decimal("1"):
                self.unit_price = actual_total
            else:
                raise ValueError(
                    f"Line item '{self.description}' arithmetic mismatch: "
                    f"qty ({self.quantity}) * unit ({self.unit_price}) = {expected_total}, "
                    f"but total stated was {actual_total}"
                )
        return self

class ExtractedInvoice(BaseModel):
    invoice_number: Optional[str] = Field(None, description="Document ID or Invoice #")
    vendor_name: str = Field(..., description="Name of company, merchant, or contractor")
    date: Optional[str] = Field(None, description="Transaction date in YYYY-MM-DD format")
    items: List[ExtractedLineItem] = Field(..., min_length=1, description="List of all extracted line items")
    subtotal: Decimal = Field(..., description="Calculated or printed subtotal before taxes and shipping")
    tax_amount: Decimal = Field(default=Decimal("0.00"), description="Sales tax, VAT, or GST")
    tip_or_shipping: Decimal = Field(default=Decimal("0.00"), description="Tip, gratuity, or freight charges")
    grand_total: Decimal = Field(..., description="Final invoice total")

    @field_validator("subtotal", "tax_amount", "tip_or_shipping", "grand_total", mode="before")
    def clean_header_decimals(cls, v):
        if isinstance(v, (int, float)):
            return Decimal(str(v))
        if isinstance(v, str):
            cleaned = re.sub(r"[^\d.-]", "", v.strip())
            return Decimal(cleaned) if cleaned else Decimal("0.00")
        return v

    @model_validator(mode="after")
    def verify_document_invariants(self):
        # 1. Sum of line items must match the subtotal
        items_sum = sum(item.total_amount for item in self.items).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
        expected_subtotal = self.subtotal.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)

        if abs(items_sum - expected_subtotal) > Decimal("0.10"):
            raise ValueError(
                f"Subtotal mismatch: Sum of line items (${items_sum}) does not equal "
                f"document subtotal (${expected_subtotal})"
            )

        # 2. Calculated total must equal subtotal + tax + tip/shipping
        calculated_grand = (expected_subtotal + self.tax_amount + self.tip_or_shipping).quantize(
            Decimal("0.01"), rounding=ROUND_HALF_UP
        )
        actual_grand = self.grand_total.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)

        if abs(calculated_grand - actual_grand) > Decimal("0.15"):
            raise ValueError(
                f"Grand total mismatch: subtotal (${expected_subtotal}) + tax (${self.tax_amount}) + "
                f"extras (${self.tip_or_shipping}) = ${calculated_grand}, but document claims ${actual_grand}"
            )
        return self

3. The Extraction & Self-Healing Feedback Loop

When documents have smudged ink or poorly aligned handwriting, the LLM may fail the first validation attempt. Rather than crashing or alerting a human operator, the system catches the Pydantic ValidationError and feeds the exact error message back into the model context in a zero-shot self-correction prompt.

Here is the complete implementation using the Google GenAI SDK:

import os
import json
from google import genai
from google.genai import types
from pydantic import ValidationError

client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))

EXTRACTION_SYSTEM_PROMPT = """
You are an expert forensic document transcription specialist and certified accountant.
Your task is to transcribe handwritten invoices, crumpled receipts, and complex dispatch tables into structured JSON.

CRITICAL INSTRUCTIONS:
1. Examine handwriting stroke paths, cursive ligatures, and numerical context.
2. If a handwritten digit is smudged, use arithmetic deductions from line totals, item quantities, or subtotals to resolve ambiguity.
3. Every numeric total must obey strict arithmetic: quantity * unit_price = total_amount.
4. Output ONLY valid JSON matching the requested schema. Never output conversational pleasantries.
"""

def extract_with_self_healing(
    preprocessed_image_bytes: bytes,
    max_retries: int = 2
) -> ExtractedInvoice:
    """Extract structured data from an image, automatically recovering from mathematical discrepancies."""
    
    conversation_contents = [
        types.Part.from_bytes(data=preprocessed_image_bytes, mime_type="image/jpeg"),
        EXTRACTION_SYSTEM_PROMPT
    ]

    for attempt in range(max_retries + 1):
        try:
            response = client.models.generate_content(
                model="gemini-2.5-flash",
                contents=conversation_contents,
                config=types.GenerateContentConfig(
                    response_mime_type="application/json",
                    response_schema=ExtractedInvoice,
                    temperature=0.0
                )
            )

            # Validate against Pydantic schema with active invariant checks
            validated_doc = ExtractedInvoice.model_validate_json(response.text)
            return validated_doc

        except ValidationError as err:
            if attempt == max_retries:
                raise RuntimeError(f"Document failed validation after {max_retries} correction attempts: {err}")

            # Extract detailed error descriptions
            error_details = json.dumps(err.errors(), default=str)
            
            # Formulate self-correction prompt
            correction_instruction = (
                f"ATTEMPT {attempt + 1} FAILED VALIDATION:\n"
                f"{error_details}\n\n"
                "Please review the document image again carefully. Re-calculate the row products and column sums. "
                "Correct the conflicting numbers so that all arithmetic checks pass strictly."
            )
            
            # Append model's previous response and corrective prompt to history
            conversation_contents.append(types.Part.from_text(text=response.text))
            conversation_contents.append(types.Part.from_text(text=correction_instruction))

    raise RuntimeError("Unexpected extraction state reached")

4. Handling Multi-Page Financial PDFs and Spanning Tables

A frequent real-world failure mode occurs when multi-column tables split across page breaksโ€”for example, when row 14 starts on Page 1 and concludes with item descriptions or nested subtotals on Page 2.

To handle multi-page PDFs reliably:

  1. Render each page to an optimized JPEG buffer via pdf2image.
  2. Extract page-level chunked rows using a lightweight page-index identifier.
  3. Stitch row items across boundary splits using a deterministic merge pass.
from pdf2image import convert_from_bytes
import io

def process_multipage_pdf(pdf_bytes: bytes) -> ExtractedInvoice:
    """Extract and aggregate tables spanning multiple PDF pages."""
    images = convert_from_bytes(pdf_bytes, dpi=200)
    
    page_parts = []
    for idx, page_img in enumerate(images):
        buffer = io.BytesIO()
        page_img.save(buffer, format="JPEG", quality=90)
        optimized_bytes = preprocess_document_image(buffer.getvalue())
        page_parts.append(types.Part.from_bytes(data=optimized_bytes, mime_type="image/jpeg"))

    # Supply all pages simultaneously to the multimodal context
    prompt = (
        f"{EXTRACTION_SYSTEM_PROMPT}\n"
        "This document contains multiple consecutive pages. Reconcile tables that span across page margins into "
        "a single unified itemized list before computing final document totals."
    )
    page_parts.append(types.Part.from_text(text=prompt))

    response = client.models.generate_content(
        model="gemini-2.5-flash",
        contents=page_parts,
        config=types.GenerateContentConfig(
            response_mime_type="application/json",
            response_schema=ExtractedInvoice,
            temperature=0.0
        )
    )

    return ExtractedInvoice.model_validate_json(response.text)

5. Production Benchmark: Vision LLM vs Legacy OCR APIs

How does a native multimodal approach compare against legacy cloud OCR engines like AWS Textract and Azure Document Intelligence?

Evaluation Metric Tesseract 5 (Open-Source) AWS Textract Azure AI Document Intelligence Gemini 2.5 Flash + Pydantic
Clean Typed Text Accuracy 98.1% 99.4% 99.6% 99.8%
Cursive & Messy Handwriting 34.2% (Complete failure) 78.5% 84.1% 96.4%
Faded Thermal Receipt Parsing 46.0% 81.0% 86.5% 97.8%
Self-Healing Math Invariants No (Requires regex scripts) No (Returns raw blocks) No (Table JSON only) Yes (Native retry loop)
Cost per 1,000 Pages Free (Compute only) $15.00 - $50.00 $10.00 - $30.00 $0.35 - $1.20
Average Page Latency 0.8s 2.4s 1.9s 1.1s

Key Architectural Takeaways:

  1. 10x to 40x Cost Reduction: Processing documents with Gemini 2.5 Flash costs approximately $0.0007 per page in token volume, compared to $0.015 to $0.05 per page on AWS Textract Queries or Expense APIs.
  2. Global Context Understanding: Legacy OCR evaluates isolated bounding boxes without semantic comprehension. When handwriting is missing a decimal point (3 49), Textract transcribes 3 49 or 349. Vision LLMs recognize that 1 gal milk priced at 349 is impossible and infer $3.49 using catalog heuristics.
  3. Structured Pydantic Contract: Legacy APIs emit convoluted nested bounding box arrays (Blocks, Geometry, Polygon) that require hundreds of lines of fragile parsing code. Vision LLMs return validated business models ready for immediate database persistence.

6. Production Checklist & Failure Mode Playbook

Before shipping your extraction gateway to production:

  • Enforce Strict Zero Temperature: Never allow temperature > 0.0 when transcribing numbers or legal identities.
  • Add 5-Cent Tolerance in Validators: Real-world cash registers frequently drop fractions of a cent on individual line-item tax calculations; overly rigid Decimal("0.00") matching will trigger false-positive retries.
  • Cap Auto-Retries at 2: If the model fails validation twice in a row, flag the document for human-in-the-loop (HITL) review rather than burning infinite API tokens.
  • Store Original Image Hash: Maintain SHA-256 hashes of preprocessed documents alongside the parsed Pydantic JSON in your database for audit trails.
WEEKLY NEWSLETTER

Get Weekly AI Architect Cost & Strategy Updates

Join 14,000+ developers receiving weekly, data-driven cost-reduction blueprints and production-ready agent guidelines.

Professor XAI
Professor XAI ML Engineer passionate about advancing AI technologies and building intelligent systems.
comments powered by Disqus