← Back to blog

6 Stage LLM Data Extraction Pipeline for Developers With Typed Errors

September 7, 2026
6 Stage LLM Data Extraction Pipeline for Developers With Typed Errors

Use a schema-first, provider-structured-output pipeline plus semantic post-validation for reliable LLM data extraction. Generate JSON with a strict schema via function-calling or structured-output APIs, then treat every response as fallible: check for truncation, refusals, and field-level consistency before it touches your database. The real work isn't the extraction call. It's the pipeline of OCR normalization, schema enforcement, and validation wrapped around it.


TL;DR:

  • Enforcing schema validation at the API level and handling explicit failure modes are essential for reliable production data extraction workflows.
  • Structured-output APIs and fresh validation layers significantly increase accuracy and reduce errors compared to prompt-only approaches, especially on complex documents.
  • Limiting schema size, segmenting long documents, and caching repeated tasks cut costs and improve throughput at scale.
  • Converting web pages to markdown stabilizes extraction, and typed responses improve downstream handling and error identification.
  • Collecting source evidence for each field and setting strict review thresholds help catch errors early and support compliance requirements.

Gyrence
gyrence.com
Build More Reliable Web Extraction
Gyrence turns web pages into structured, agent-ready data with markdown, schema-based extraction, and typed failure responses.
Explore Gyrence

Table of Contents

What Is LLM Data Extraction, and Why Does the Old Approach Fail?

LLM data extraction is the process of turning unstructured or semi-structured text (PDFs, emails, contracts, web pages, scanned forms) into typed, structured records a downstream system can consume. It replaces regex, brittle CSS selectors, and rule-based parsers with a model that reads context the way a human does, then outputs JSON that matches a schema you define.

The old approach, prompt-only extraction with "return this as JSON" instructions, fails in predictable ways: models wrap output in markdown fences, invent fields that don't exist, drop nested arrays, or truncate mid-object on long documents. None of that is a hallucination problem exactly. It's a formatting-reliability problem, and it's why every production-grade system now leans on provider-level enforcement instead of hoping the model behaves.

Three engineering choices separate a demo from something that survives contact with real documents:

  • Schema enforcement at the API level, not just in the prompt, using JSON Schema or function-calling parameters.
  • Explicit failure handling for every response: truncation, refusal, malformed JSON, and semantic mismatches all need their own code path.
  • A validation layer that checks the extracted data against the source text, not just against the schema's shape.

The rest of this guide walks through each layer in the order you'd build it.

Prompt-Only vs. Structured Outputs vs. Post-Processing: Which Approach Fits?

Four engineering approaches dominate real deployments, and picking the wrong one for your stage of development is the most common early mistake teams make.

Prompt-only extraction asks the model to output JSON inside a text response, with no schema enforcement. It's fast to prototype and works fine for a demo with five sample documents. It falls apart at scale because nothing forces the model to respect field names, types, or nesting, and you end up writing recovery logic for every possible malformed shape it produces.

Provider structured outputs (OpenAI's JSON Schema mode, Anthropic and Google's equivalents) constrain the model's output at the token-sampling level so the response is guaranteed to match your schema. OpenAI's structured outputs feature eliminates most retry logic tied to malformed JSON, and SDK helpers built on Pydantic (Python) or Zod (TypeScript) parse the response straight into typed objects. This is the right default for anything shipping to production.

Constrained decoding goes a step further, restricting the model's token choices during generation itself rather than just validating the shape afterward. It's more rigid and harder to tune, and it's usually only worth the engineering cost when you're running open-weight models where you control the inference stack directly.

Post-processing models, sometimes called SLOT-style pipelines, take an unstructured or loosely structured first-pass output and run it through a second, lighter model whose only job is reshaping it into strict schema compliance. Research on this pattern shows a lightweight post-processor can push schema accuracy close to perfect even when the first-pass extraction was messy, which matters when you're stuck with a model that doesn't support native structured outputs.

The trade-off across all four comes down to speed versus reliability. Prompt-only is fastest to build and least reliable. Structured outputs cost a little more setup time and buy you most of the reliability gain. Constrained decoding and post-processing models are for teams hitting a wall that structured outputs alone can't solve, usually large or deeply nested schemas.

Layout matters too. Text-only strategies work for clean, linear documents. Anything with tables, multi-column layouts, or scanned pages needs a layout-aware step (bounding boxes, reading order detection) before extraction even starts, or the model receives text in an order that scrambles the meaning.

How Do You Build an End-to-End Extraction Pipeline?

A production pipeline has six stages, and skipping any one of them is where teams get burned. Here's the sequence, in order.

  1. File-type detection and routing. Not every document needs OCR. A native PDF with an embedded text layer should skip straight to cleaning; a scanned image or fax needs OCR first. Detect this automatically rather than assuming.
  2. OCR and confidence scoring. When OCR runs, capture its confidence score per page or per block. Anything below your threshold (commonly 80 to 90 percent, depending on document quality) should route to manual review instead of silently feeding garbage into the model.
  3. Text cleaning and markdown conversion. Strip boilerplate, normalize whitespace, and convert the page into markdown so headers, tables, and lists retain their structure instead of collapsing into a wall of text. This step stabilizes the model's context window and is worth its own developer guide if you're building it from scratch.
  4. Schema generation and compatibility testing. Define your target shape in Pydantic or Zod, then verify it against your provider's actual constraints. Not every JSON Schema feature (recursive references, certain enum patterns) is supported identically across OpenAI, Anthropic, and Google's structured-output implementations. Test this before you build a pipeline around an unsupported schema shape.
  5. Structured-output extraction calls. Send the cleaned document plus schema to the model. Handle three failure modes explicitly: a refusal (the model declines to answer), a truncation (the response hits a token limit mid-object), and a malformed response despite schema constraints (rare, but it happens with edge-case inputs).
  6. Parsing, validation, and storage. Parse the typed response, run schema validation, then run semantic validation, checks that go beyond "is this a string" to "does this string make sense given the source document." Only after both pass does the record move to storage.

That sixth stage deserves more attention than most teams give it. Schema validation confirms the shape is correct. Semantic validation confirms the content is plausible: does an extracted invoice total roughly match the sum of line items? Does a contract end date fall after its start date? Does an extracted phone number have the right digit count for its stated country? These are cheap checks that catch a large share of the errors schema validation alone misses.

Evidence linking closes the loop. For every extracted field, capture the source span (a page number, a character offset, or a quoted snippet) it came from. When a human reviewer or downstream system questions a value, evidence linking turns "the model said so" into "here's exactly where in the document that number appears." Skip this and every audit becomes a manual document search.

Pro Tip: Build your review gate around confidence, not correctness assumptions. Route any field the model marks with low certainty, or any field that fails a semantic check, to a human queue automatically. Don't wait for a customer complaint to discover the model has been quietly guessing on a specific field type for weeks.

Four operational primitives round out a pipeline that survives production load rather than just a demo:

  • Idempotency keys on every extraction job so a retried request doesn't create duplicate records.
  • Retry policies with backoff that distinguish transient errors (rate limits, timeouts) from permanent ones (malformed input, unsupported file type) so you're not retrying something that will never succeed.
  • Observability on every stage: OCR confidence distributions, schema validation failure rates, and semantic-check failure rates, tracked over time so a silent model or provider change shows up as a metric shift instead of a support ticket.
  • Spending caps at the workflow level, because a malformed batch job that retries indefinitely against a per-call-billed API is exactly the kind of failure that turns into an unpleasant invoice.

How Should You Design Schemas and Prompts for Extraction?

Schema design decisions have more influence on extraction accuracy than most teams expect, and a handful of concrete rules cover most of the ground.

Nulls versus omission. Decide once, apply everywhere: a field with no value in the source document should return null, never be omitted from the response and never return an empty string. Omission breaks strict schema validation, and empty strings get confused with legitimately blank text fields. Practitioner guides on structured extraction consistently land on explicit null handling as the safest default.

Notes fields absorb ambiguity. Add an optional notes or extraction_confidence_note field to schemas covering messy source material. When the model has somewhere to flag "the date is illegible in the source" instead of guessing, it usually does, and that flag becomes your review-queue trigger.

Enums beat free text wherever the value space is closed. If a document field can only be one of five categories, define it as an enum, not a string. This eliminates an entire class of downstream normalization work and reduces the chance the model invents a sixth category that doesn't exist in your system.

Full schema versus pruned subschema. For small, flat schemas (under maybe 20 to 30 fields), send the whole thing every time. For large or deeply nested schemas covering many document types, send only the relevant subschema for the document at hand. This pattern, sometimes called SchemaRAG, retrieves and prunes the schema before the extraction call rather than asking the model to navigate a schema with hundreds of fields when only fifteen apply to this specific document. The gains aren't marginal: research on this pruning approach reports an 8.8 percent improvement in extraction accuracy alongside roughly 47 percent lower latency and 48 percent lower token cost compared to sending the full schema every time.

Batching has real limits. Don't send ten unrelated documents in a single call hoping for ten clean objects back. Batch only similar, short documents together, and cap batch size based on combined token length, not document count. Beyond a certain context length (this varies by model, but degradation is common well before you hit the stated context window), extraction accuracy on later documents in the batch drops noticeably.

Pro Tip: Write a separate verifier prompt that receives only the extracted JSON and the source text, then asks a narrow question: "Does this extracted value appear in or logically follow from this source?" Running this as a second, cheap call catches fabricated fields that pass schema validation but fail basic plausibility.

Three prompt shapes cover almost every use case: a single-document structured-output call for one-off extraction, a batch wrapper that iterates single-document calls rather than cramming multiple documents into one context, and a verifier prompt that runs after extraction to catch what schema validation can't see.

How Should You Design Schemas and Prompts for Extraction? — overview diagram

How Do You Control Cost and Latency at Scale?

Token cost in an extraction pipeline scales with three variables you can actually control: schema size, document length, and how many OCR or preprocessing passes each document needs before it reaches the model.

A schema with 300 fields costs more per call than one with 30, not just because the schema definition itself consumes tokens, but because the model has more surface area to reason about and more places for extraction to go wrong, which drives retries. Document length compounds this directly. A 40-page contract sent whole costs far more than the same contract segmented into relevant sections first.

Three techniques control this at scale:

  • Subschema retrieval (the SchemaRAG pattern from the previous section) cuts both latency and token spend by pruning irrelevant fields before the call, with the documented reductions running near 47 to 48 percent on latency and cost respectively.
  • Segmentation breaks long documents into logical sections (by page, by detected heading, by table boundary) and extracts each independently, rather than forcing one call to hold the entire document in context.
  • Selective verification runs the cheap plausibility check described earlier only on fields flagged as uncertain, rather than re-verifying every field on every document, which keeps your verification cost proportional to actual risk.

Open-weight models running on your own infrastructure trade a fixed compute cost for the flexibility of constrained decoding and full control over the inference stack. Hosted APIs trade that control for zero infrastructure overhead and provide access to provider-level structured-output enforcement out of the box. Most teams start hosted and only move to open-weight models once volume makes the economics favor owning the inference layer.

Caching matters more than it gets credit for. If your pipeline reprocesses documents (a nightly re-crawl, a webhook-triggered re-check), hash the source content and skip extraction entirely when nothing has changed. Incremental extraction, only reprocessing the sections of a document that changed since the last pass, saves real money on any workflow that touches the same source repeatedly.

Finally, put a number on your monitoring: track spend per document type, sample a fixed percentage of outputs for manual QA weekly, and set a hard spending cap at the workflow level so a runaway batch job can't turn into a surprise invoice at the end of the month.

What Metrics Actually Prove an Extraction System Is Ready?

Four metrics matter, and most teams only check one of them before shipping.

Schema accuracy measures whether the output matches the schema's shape: right types, right nesting, no missing required fields. This is the easiest metric to hit and the least informative on its own, since a response can be perfectly shaped and still completely wrong in content.

Token-level F1 compares extracted field values against ground truth at the token level, catching partial matches (extracting "123 Main St" when the correct value is "123 Main Street, Apt 4"). Useful for free-text fields, less useful for categorical or numeric ones where you want exact matches.

Document-level validity (sometimes labeled doc_micro) measures the percentage of entire documents that extract with zero errors across every field. This is the number that predicts production pain, because a system with 95 percent field-level accuracy can still have a shockingly low document-level validity if errors are spread across different documents rather than concentrated in a few.

Array alignment checks whether repeated structures (line items, table rows, multiple parties in a contract) extract in the correct order and count, a failure mode that field-level metrics miss entirely because they don't account for array length mismatches.

The schema-size cliff: ExtractBench, a benchmark for PDF-to-JSON structured extraction, found frontier models produced no valid output on a very large financial schema, even though the same models performed well on smaller schemas. Document-level validity doesn't degrade gradually as schemas grow. It falls off a cliff past a certain complexity threshold, which is the strongest argument for schema pruning you'll find in the research.

Run acceptance tests per field type, not as one blanket pass/fail. Numeric and date fields should use exact matching; free-text fields (descriptions, notes) should tolerate reasonable rephrasing. Test against a sample large enough to catch rare document variants, not just the five clean examples you used to build the prompt.

Automated verifiers add a layer schema validation can't provide. Confidence-scoring approaches for structured LLM outputs assign per-field trust scores that flag likely errors before a human ever sees the record, which lets you route only the flagged fraction to manual review rather than spot-checking everything or nothing.

  • Set your human-review threshold based on document-level validity, not field-level accuracy.
  • Re-run benchmarks whenever you change providers or model versions; structured-output behavior isn't guaranteed stable across model updates.
  • Treat any schema over roughly 100 fields as a candidate for pruning before you even start measuring accuracy.

How Do Fetch-to-Markdown and Typed Responses Fit the Pipeline?

Web-sourced documents add a wrinkle native PDFs and scanned forms don't have: the raw HTML is full of navigation menus, ads, and script tags that have nothing to do with the content you're trying to extract. Converting the page to clean markdown before it reaches the model isn't a cosmetic step. It's what keeps the context window focused on content instead of noise, and it's detailed in Gyrence's developer guide on fetch-to-markdown conversion.

The second piece worth building into any extraction system: typed, discriminated-union responses. Instead of a single response shape that sometimes has data and sometimes has an error buried in a message field, every API call should return one of a fixed set of typed outcomes: success with data, validation failure with specifics, timeout, or refusal. An agent or downstream service can then branch on the response type directly rather than parsing error strings to figure out what went wrong. This matters more than it sounds, because "guess what happened from a string" is exactly the kind of fragile logic that breaks silently in production.

A minimal flow looks like this: Fetch the source (a URL or uploaded file) and normalize it to markdown, OCR any embedded images or scanned regions that need it, Extract against a defined schema using structured outputs, then insert a semantic validator between extraction and storage. That validator is where evidence checks, date arithmetic sanity checks, and table-extraction verification belong, not bolted on after the fact.

A few operational checks are easy to skip and expensive to skip:

  • Evidence checks: does the extracted value actually appear in, or logically derive from, the source text you fetched?
  • Date arithmetic: extracted date ranges should be validated for logical order (start before end) and plausibility (no contract dated 300 years in the future because of an OCR misread digit).
  • Table extraction: verify row counts and column alignment separately from individual cell accuracy, since a shifted column is a different failure mode than a wrong value in one cell.

Pro Tip: When extracting from web pages with tables, fetch the page to markdown first and check that table structure survives the conversion before you even attempt extraction. A table that collapses into a run-on paragraph during markdown conversion will produce garbage extraction results no matter how good your schema is. Structured JSON extraction from web content gets meaningfully harder once table structure is lost, which is exactly what a dedicated structured-extraction guide walks through in more depth.

How Does Extraction Fit Into Existing Data Systems?

An extraction pipeline is only as useful as its integration points, and bolting one onto an existing data stack raises questions that a standalone prototype never has to answer.

The first is schema ownership. If your extracted data feeds a warehouse or a CRM, the extraction schema needs to map cleanly onto existing table structures, not invent a parallel data model your team then has to reconcile by hand. Define the extraction schema as a direct subset or transformation of your destination schema, not as an independent shape you translate later.

The second is workflow triggers. Extraction rarely runs in isolation. It's usually one step in a larger flow: a document lands in a queue, gets extracted, gets validated, and either writes to a production table or routes to a review queue. Building extraction as a discrete service with a clear input contract and typed output, rather than embedding it inline in a larger script, makes it far easier to swap models or providers later without rewriting the surrounding workflow.

Idempotency matters again here specifically because integration points tend to retry. A webhook that fires twice, a queue message that gets redelivered, a batch job that restarts after a partial failure. Every one of these should produce the same result the second time, not a duplicate record.

Tools built for organizing tabular output, like ExpressSheet's spreadsheet automation features, can sit downstream of extraction to handle the last-mile transformation into the formats finance, operations, or analytics teams actually work in day to day, which is often a faster path than building custom export logic for every destination.

What Privacy and Security Steps Does Sensitive Extraction Require?

Extraction pipelines touching personal, financial, or health data carry obligations that a generic document pipeline doesn't. Get this wrong and the accuracy of your extraction is beside the point.

Data minimization comes first: only send the fields you actually need extracted to the model, and redact or mask anything irrelevant to the extraction task before the document leaves your system. If you're extracting invoice totals, there's no reason a customer's full medical history needs to transit through a third-party API call alongside it.

Provider data-handling terms deserve a careful read, not a skim. Some providers retain input data for model improvement by default unless you opt out; others offer zero-retention agreements for enterprise tiers. Know which policy applies to your account before you send anything sensitive.

Domain-specific work, electronic health records being the clearest example, needs stricter controls than general document extraction. Evaluation of LLMs on EHR data found that reliable extraction is achievable but demands careful evidence checks, conservative acceptance thresholds, and domain-aware validation rather than the same acceptance criteria you'd apply to, say, extracting vendor names from invoices.

Encryption in transit and at rest is table stakes, but access control around the extracted output matters just as much as protecting the source document. A pipeline that carefully redacts input but then dumps unrestricted extracted PII into a shared database has solved the wrong half of the problem.

Audit trails close the loop. Every extraction job touching sensitive data should log what was extracted, when, by which pipeline version, and who accessed the result afterward. When (not if) a regulator or customer asks how a specific piece of data was handled, that log is the difference between a five-minute answer and a multi-week forensic investigation.

How Do You Scale an Extraction System Without Breaking It?

Performance problems in extraction pipelines rarely show up as "the model is slow." They show up as queue backlogs, timeout cascades, and cost curves that bend upward faster than volume does.

Concurrency limits are the first lever. Most providers cap requests per minute and tokens per minute per account tier, and a pipeline that fires requests faster than that ceiling just accumulates rate-limit errors instead of throughput. Build a request queue with backpressure so your system slows down gracefully under load instead of retrying into a wall.

Horizontal scaling works cleanly for extraction because each document is independent. Unlike a stateful service, extraction jobs parallelize trivially across workers as long as your storage layer and rate limits can absorb the concurrent load. The bottleneck almost always shifts to the provider's rate limit or your database write throughput before it shifts to compute.

Caching reduces load in a way that's easy to overlook: if the same document type recurs (a standard invoice template, a recurring form), cache the schema-compatibility check and any preprocessing steps that don't change between instances, even when the actual extracted values differ each time.

Graceful degradation matters under load. When your system approaches a rate limit or cost ceiling, it should shed load predictably, deferring lower-priority documents to a later batch, rather than failing unpredictably across the whole queue. Spending caps enforced at the workflow level give you a hard backstop here: a runaway retry loop hits the cap and stops, instead of running until someone notices the bill.

Monitoring throughput alongside accuracy prevents the common trap of optimizing one at the expense of the other. A pipeline tuned purely for speed that quietly drops document-level validity from 92 percent to 78 percent has not actually gotten faster in any way that matters.

What Teams Get Wrong About Rolling This Out

Most teams treat LLM extraction as a binary: either the model handles a document type or it doesn't. The more useful framing is a gradient. Clean, templated documents (standard invoices, structured forms) are reliable territory for full automation today. Messy, high-variance documents (handwritten notes, inconsistent contracts, low-quality scans) still need a human in the loop, and pretending otherwise just moves the error discovery from your QA process to your customer's inbox.

The rollout pattern that actually works is pilot, sample, iterate. Run extraction against a real batch, pull a genuine random sample (not the five easiest documents) for manual review, and set an explicit service-level agreement for how fast that review happens. Teams that skip the sampling step and go straight to "it looked right in the demo" are the ones who find out about schema drift three months in, from a customer complaint instead of a dashboard.

If there's one technical priority worth defending against scope pressure, it's evidence capture. A field extracted without a traceable link back to its source is a liability the moment someone questions it. Pair that with typed failure handling, and you've solved the two problems that actually determine whether a pilot becomes a production system or a cautionary tale in next quarter's retro.

— Glen

How Gyrence Fits This Pipeline

Gyrence handles the fetch and extraction layers of this pipeline as one connected system instead of two things you have to glue together yourself. Its five primitives, Search, Traverse, Fetch, Extract, and Map, cover the ingestion and extraction stages directly: Fetch normalizes a page to clean markdown, Extract runs schema-guided LLM extraction against it, and every call returns a typed, discriminated-union response, including the failure cases, so your code branches on what actually happened instead of parsing error strings.

Gyrence

That matters most at the ingestion boundary, the part of the pipeline where web-sourced documents cause the most silent failures. A minimal flow: point Fetch at a source URL to get stable markdown, run Traverse if you need a whole site rather than one page, then call Extract with your schema to get typed JSON back, with spending caps set at the workspace level so a runaway batch job never turns into a surprise invoice. If you're building the pipeline described in this guide and want the fetch and extraction layers handled without stitching together OCR libraries, markdown converters, and retry logic yourself, the Gyrence console is worth a trial run against your actual documents.

Where to Go Deeper on Structured Extraction

Six resources worth bookmarking as you build this out: OpenAI's structured outputs documentation for implementation details and SDK parsing helpers; ExtractBench for realistic expectations on large schemas; the SchemaRAG paper for subschema pruning strategy; the SLOT paper for post-processing patterns; and ContextGem as an open-source starting point for prototyping before you build production infrastructure around it.

Sources

FAQ

Which LLM Is Best for Data Extraction?

No single model wins across every schema and document type. Models supporting native structured outputs with JSON Schema enforcement, currently offered by the major providers, consistently outperform prompt-only approaches on formatting reliability, and the bigger differentiator is often your schema design and pruning strategy rather than model choice alone.

Is Google's LangExtract Free?

LangExtract is an open-source framework, so the software itself carries no license cost, though you still pay for whatever underlying model API you connect it to. It's a solid prototyping tool but leaves production concerns like observability and typed failure handling for you to build.

Can an LLM Extract Data From a PDF?

Yes, but native PDFs with an embedded text layer extract far more reliably than scanned PDFs, which need an OCR pass with confidence scoring before the text ever reaches the model. Complex layouts (multi-column pages, tables) also need layout-aware preprocessing to avoid scrambling reading order.

Which AI Tool Is Best for Data Extraction?

The right tool depends on whether you're prototyping or shipping to production. Open-source frameworks like ContextGem work well for fast prototyping, while a managed pipeline covering fetch, schema-guided extraction, and typed failure handling reduces the integration work needed to move from a demo to something reliable at scale.

How Do You Handle Missing or Ambiguous Fields in Extraction?

Return null for genuinely missing fields rather than omitting them or returning an empty string, and add an optional notes field so the model can flag ambiguity instead of guessing. This keeps schema validation consistent and gives you a direct signal for which records need human review.