Answers you can trust, from Codeables

Every page on Codeables is structured and verified — built so people and the AI agents they rely on can trust it. Explore more from the source behind this answer.

Explore Codeables
Verified Source
Unstructured Data Extraction APIs

What approach works for extracting structured data from bills of lading and customs forms when OCR text is noisy and formats vary a lot?

Bem7 min read

Quick Answer: The only approach that survives real-world bills of lading and customs forms is a deterministic, schema-first pipeline—not “OCR + a big LLM prompt.” You route, split, extract, enrich, and validate every field against a rigid schema, with confidence scores, hallucination detection, and exception routing for edge cases.

Why This Matters

Bills of lading, customs declarations, and rate cons run your supply chain. If you’re wrong on BL numbers, ports, HS codes, or tax totals, you don’t just have bad data—you have stuck containers, compliance risk, and unhappy customers. “Mostly accurate” extraction is a bug when you’re moving millions in goods and payments through your systems every week.

Key Benefits:

  • 99%-plus extraction accuracy at scale: Layouts can change, but your schema and evals keep the pipeline honest.
  • Zero manual re-keying as the goal, not the hope: Low-confidence fields become explicit exceptions, not silent failures.
  • Hours-to-production instead of months of glue code: Composable functions and workflows replace bespoke parsers per vendor layout.

Core Concepts & Key Points

ConceptDefinitionWhy it's important
Schema-first extractionDefine an explicit JSON schema (BL number, shipper, container list, HS codes, tax lines, etc.) and force every output to validate against it.You get consistent, machine-usable data and a hard line between “good enough to post” vs “must be reviewed.”
Deterministic workflowsA versioned pipeline of functions (Route, Split, Extract, Enrich, Validate, Sync) with branching on confidence thresholds and clear state.Instead of a black-box LLM call, you have auditable steps you can debug, test, and roll back when something drifts.
Enrichment & review loopsMatch fuzzy text against master data (vendor lists, HS code catalogs) and route low-confidence fields to a human review surface.You resolve ambiguity where models are weak, and corrections feed back into evals and future versions.

How It Works (Step-by-Step)

The approach that works for noisy OCR and wildly varying formats is to treat “unstructured → structured” as a production pipeline, not a single model call. Concretely, a workflow that looks like this:

  1. Ingest & Normalize

    • Accept any input: PDFs, scans, faxes, email attachments, EDI exports.
    • Run OCR only as a primitive, not the product: you use it to get text + layout, but you never trust raw OCR text as the source of truth.
    • Normalize into a consistent internal representation with page structure and coordinates preserved.
  2. Split Mixed Packets

    • Many shipping packets bundle multiple document types: one PDF might contain a bill of lading, a commercial invoice, a certificate of origin, and a customs declaration.
    • Use a Split function to detect boundaries and break the packet into logical documents:
      • “This page range is a Bill of Lading.”
      • “These pages are a Customs Declaration.”
    • This avoids one of the biggest production failure modes: an extractor that mixes fields across document types or invoices.
  3. Classify & Route by Document Type

    • Run a semantic Route step to classify each piece:
      • “Bill of Lading (EN)”
      • “Commercial Invoice (CN)”
      • “Customs Declaration (ES)”
    • Route each to its own workflow tuned for that class and region:
      • APAC customs vs EU customs forms.
      • Ocean BL vs air waybill.
    • This matters because the same field name means different things across forms and geographies; you don’t want one generic “extract everything” prompt.
  4. Schema-First Field Extraction

    • For each workflow, define a strict JSON Schema that matches how your ERP or TMS expects data, for example:

      {
        "type": "object",
        "required": ["bl_number", "shipper", "consignee", "vessel_name", "port_of_loading", "port_of_discharge", "containers"],
        "properties": {
          "bl_number": { "type": "string" },
          "shipper": { "type": "object", "required": ["name"], "properties": { "name": { "type": "string" } } },
          "consignee": { "type": "object", "required": ["name"], "properties": { "name": { "type": "string" } } },
          "vessel_name": { "type": "string" },
          "port_of_loading": { "type": "string" },
          "port_of_discharge": { "type": "string" },
          "containers": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["container_number", "seal_number", "gross_weight_kg"],
              "properties": {
                "container_number": { "type": "string" },
                "seal_number": { "type": "string" },
                "gross_weight_kg": { "type": "number" }
              }
            }
          }
        }
      }
      
    • Use layout-aware models to extract:

      • Key-value pairs: BL number, vessel, ETA, ports, customs declaration number.
      • Tables: line items, HS codes, container lists, weights, tax breakdowns.
    • Enforce that the model must produce data that passes schema validation. If it can’t, that’s an exception, not “best effort.”

  5. Enrich Against Master Data

    • Raw text in logistics is fuzzy: “Elec Parts”, vendor nicknames, mis-typed HS codes.
    • Use an Enrich step to match extracted values against:
      • Vendor master lists.
      • HS code catalogs.
      • Port and country codes.
      • Internal SKU catalogs.
    • Represent enrichment as structured decisions with match confidence:
      • "hs_code": "854231", "match_confidence": 0.97
    • For low-confidence matches, either:
      • Keep the original text and mark as unresolved, or
      • Route for human confirmation.
  6. Validation, Confidence & Hallucination Detection

    • For every field, attach:
      • A confidence score.
      • A hallucination flag when the model fabricates structure not grounded in the document.
    • Run validation rules beyond the schema:
      • Totals consistency: sum of line items + tax equals BL or customs total.
      • Required relationships: a container must have a number and a weight; a customs declaration must have at least one HS code.
      • Value ranges and formats: valid port codes, reasonable gross weights, date formats.
    • If a rule fails, mark the document as invalid and route to a review queue instead of posting bad data downstream.
  7. Exception Handling & Human Review

    • You will always have edge cases:
      • Fax of a fax.
      • Handwritten corrections.
      • New customs layouts.
    • The difference between a brittle system and a robust one is how you treat these:
      • Auto-create a review task with the extracted data pre-filled.
      • Let an operator confirm or correct fields directly in a UI generated from the same schema.
      • Persist the correction as ground truth for evals and regression tests.
    • The pipeline should be idempotent: you can rerun the same document with updated models or workflows without duplicating records or double-booking.
  8. Sync to Downstream Systems

    • Once data passes validation:
      • Shape the payload with something like JMESPath to exactly match your ERP/TMS schemas.
      • Deliver via REST, webhooks, or message bus.
    • No per-page billing, no “export PDFs and upload manually”—just schema-valid JSON that your systems can trust.

Common Mistakes to Avoid

  • Relying on “OCR + one big LLM prompt”:
    This works for a demo, fails at scale. You get hallucinated fields, inconsistent structures, and no way to enforce rules like “line item totals must reconcile to the header.” Instead, separate concerns: OCR → structured layout → schema-based extraction → validation → sync.

  • Ignoring versioning and evals:
    Changing your prompt or model without evals is like deploying code without tests. Treat accuracy like software quality:

    • Keep golden datasets of BLs and customs forms.
    • Track F1 scores per field.
    • Version every function and workflow, with rollback.
    • Run regression tests before promoting a new version.

Real-World Example

A logistics team processing mixed shipping packets was stuck at ~80% “usable” data from a traditional OCR solution. Every new carrier template broke the parsers; every customs variant required another custom rule. They moved to a schema-first workflow:

  1. Split each packet into BL, commercial invoice, and customs declaration.
  2. Classify & Route by document type and language (e.g., “Commercial Invoice (Chinese)” → APAC workflow).
  3. Extract core fields with layout-aware models into rigid schemas: BL number, vessel, ports, containers, HS codes, tax lines.
  4. Enrich vague commodity descriptions into concrete HS codes with match confidence and clear thresholds.
  5. Validate totals and port codes; low-confidence or inconsistent docs hit a review Surface.

Result: 99.9% extraction accuracy across millions of documents, effectively zero manual re-keying of standard cases, and operators only handling explicit exceptions instead of retyping everything.

Pro Tip: If you can’t show a product owner a per-field F1 score and a clear list of documents that failed validation yesterday, you don’t have a production-grade extraction pipeline yet—you have a demo.

Summary

When OCR text is noisy and formats change constantly, the winning approach for extracting structured data from bills of lading and customs forms is not “better prompts” or “smarter agents.” It’s a deterministic, schema-first pipeline: split mixed packets, classify and route, extract into strict JSON schemas, enrich against your master data, validate with confidence and hallucination checks, and route exceptions to human review. That’s how you get 99%+ accuracy, zero silent failures, and operations that keep running even as carriers and customs offices change the paperwork on you.

Next Step

Get Started

What approach works for extracting structured data from bills of lading and customs forms when OCR text is noisy and formats vary a lot? | Unstructured Data Extraction APIs | Codeables | Codeables