- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2026 09:57 AM
Great observations — this is a pattern several of us have run into. The short answer is: your PyMuPDF + ai_query workflow is the right approach for digitally-born PDFs, and here's why.
Why aiparsedocument can get names/identifiers wrong
ai_parse_document uses an OCR + Vision Language Model pipeline under the hood — it's rendering pages as images and using AI to "read" them. This is powerful for scanned documents, complex tables, and figures, but it introduces non-determinism. The model is essentially interpreting the text visually rather than extracting the actual embedded text data. For names and identifiers — where a single character error matters — this visual interpretation can produce subtle hallucinations (e.g., swapping similar-looking characters, misspelling proper nouns).
The function is also explicitly non-deterministic per the docs — running it twice on the same PDF may yield slightly different results.
Why PyMuPDF + ai_query works better here
For digitally-born PDFs (not scanned), the text is embedded as actual character data in the PDF. PyMuPDF extracts this data directly — no AI interpretation needed — so it's deterministic and lossless. Then passing that clean text to ai_query for structured field extraction gives the LLM accurate input to work with.
Your workflow is actually the recommended two-stage pattern, just with a more reliable first stage for your document type:
PDF → PyMuPDF (deterministic text) → ai_query (structured extraction) → JSON
When to use which
|
Scenario |
Best approach |
|
**Digital/native PDFs** (text-selectable) |
PyMuPDF + ai_query — cheaper, faster, deterministic |
|
Scanned documents / images |
ai_parse_document + ai_query — OCR is necessary |
|
Complex tables / layouts |
ai_parse_document — extracts tables as HTML with structure |
|
Figures / charts needing descriptions |
ai_parse_document with `descriptionElementTypes` = `'*'` |
Tips if you do need aiparsedocument
If your pipeline must handle both digital and scanned PDFs:
- Enable `imageOutputPath` to visually inspect what the model received — this helps diagnose whether the issue is input quality or model interpretation:
SELECT ai_parse_document(
content,
map(
'imageOutputPath', '/Volumes/catalog/schema/volume/debug_images/',
'descriptionElementTypes', '*'
)
) FROM ...
- Add a validation step — since there are no confidence scores, implement your own (regex for expected ID formats, cross-reference against known values).
- Consider a hybrid approach — try PyMuPDF first; if it returns empty/garbled text (indicating a scanned PDF), fall back to ai_parse_document.
Example: Hybrid pipeline in PySpark
import pymupdf
def extract_text(pdf_bytes):
"""Try PyMuPDF first (digital PDFs), flag for ai_parse_document if empty."""
doc = pymupdf.open(stream=pdf_bytes, filetype="pdf")
text = "\n".join(page.get_text() for page in doc)
return text.strip() if text.strip() else None
# In your pipeline:
# 1. Run PyMuPDF on all docs
# 2. For docs where extract_text returns None → use ai_parse_document
# 3. Pass all extracted text to ai_query for structured extraction
Your finding aligns with what others have observed — ai_parse_document is best suited for documents that require AI-based parsing (scans, complex layouts), while digitally-born PDFs are better served by direct text extraction libraries.
Solutions Engineer @ Databricks