AI AI Toolkit
AI Newstip

用 LLM 从 PDF 中抽取表格

OpenRouter:Announcements(RSS)2026-10-29T00:00:00.000Z

OpenRouter published a hands-on tutorial showing how to call file-reading LLMs together with a strict JSON Schema to extract structured tables from PDF documents. Simply put, it hands the messy job of parsing PDFs, something that used to require a pile of brittle scripts, to a multimodal model in one step, and the pattern generalizes well beyond tables to most structured extraction from documents.

Key highlights

The method sends a PDF as a file content block directly to a model that can read files, the tutorial names Claude Haiku 5.5, GPT-6 Luna, and Gemini 3.8 Flash, alongside a strict JSON Schema describing a single table row. The model returns typed rows. Quantities come back as integers, parenthesized losses like (3,878) are automatically converted to negative 3878, and tables that span pages are merged into one list, which eliminates the most common manual cleanup steps entirely.

What happens

The field descriptions themselves do the data cleaning. By spelling out the rules inside the schema's field descriptions, you strip thousands separators, flip parentheses into negatives, drop footnote markers, and repeat merged cells across every row. For invoices, quarterly reports, and shipment logs with cross-page spans, merged cells, and tricky number formats, describing the shape in a schema is far easier to maintain than hand-written cleanup code that breaks on the next layout variation.

Technical details

The post stresses two checks against silent failure. First, reject empty results, because free text parsers can return zero rows on scans without throwing an error, so you must catch it explicitly. Second, reject any response whose finish_reason is length, which would mean a long table was cut off by the output token cap. For scans, you can add the mistral-ocr engine for optical recognition, about $2 per 1,000 pages. For purely digital PDFs the free cloudflare-ai parser is enough, which keeps cost near zero for the common case.

Versus the alternatives

Classic free libraries like pdfplumber only handle simple digital PDFs and silently mangle everything else. In the tutorial's test across five PDFs and 644 cells, GPT-6 Luna and Claude Haiku 5.5 were the most cost-effective and fully correct routes. The free cloudflare-ai parser was fully correct on digital PDFs but fully wrong on scans, which is the exact failure mode the empty-result check is designed to catch.

Industry impact and use cases

This approach suits teams in finance, legal, and supply chain that need to turn large volumes of PDFs into clean structured data. Developers can also use the Batch API to run historical backlogs asynchronously at roughly half the cost. For small and mid-sized teams, using an LLM to replace fragile parsing scripts is a low-threshold first step toward real document intelligence.

Practical Notes

Always add the two guardrails the post recommends, reject empty results and reject length-finished responses, because silent truncation is the most common way this pipeline fails in production. Keep a human spot-check on the first few hundred rows before trusting it at scale, and log which model served each batch so you can trace errors.

What to Watch

Watch the cost curve as you scale. Digital PDFs are nearly free via cloudflare-ai, but scanned documents add the mistral-ocr fee, so segment your corpus and route scans to OCR first. The Batch API can cut cost roughly in half for backlogs, which is where most of the savings live.

Takeaway

For structured extraction from messy PDFs, a multimodal model plus a carefully specified JSON Schema beats hand-rolled parsers on maintenance and accuracy, and the pattern is the new default for document ingestion.

Background

Document intelligence has cycled through OCR, layout models, and now multimodal LLMs, and each cycle promised to kill the brittle parser, but the brittle parser kept coming back because real documents are messy. The schema-driven approach matters because it finally pushes data cleaning into the model's contract rather than into post-processing code that breaks on the next layout change.

Where to Try

Start with your cleanest digital PDFs using the free cloudflare-ai parser, then route scans through mistral-ocr, and keep the two guardrails in every pipeline. Validate the first few hundred extracted rows by hand before trusting it at scale, because a silent truncation at the tail of a long table is exactly the kind of error that looks fine in a demo and fails in a quarter-end report.

Closing Thoughts

For structured extraction, the combination of a multimodal model and a strict schema is becoming the default pattern, and teams that adopt it will spend far less time maintaining parsing code and far more time using the data they finally have clean.