Querying multimodal data
You can query images and PDFs in Quail alongside text columns. Pass PNG and JPEG
images stored in a BINARY column directly to AI.IF, AI.SCORE,
AI.CLASSIFY, or AI.MAP. AI.PDF_PAGES turns each page of a PDF into a PNG
image so a vision model, a language model able to read images, can answer
questions from page layouts and scans. AI.PARSE_DOCUMENT instead reads the
text stored in a PDF, without optical character recognition (OCR), which reads
text from pictures. Use AI.PDF_PAGES for most PDF queries, and use
AI.PARSE_DOCUMENT when a text-only model runs the query or when AI.EXTRACT
copies answers from stored text.
Choose a model
Configure the session with a vision model when your query reads images or
AI.PDF_PAGES output:
qwen3.5-4b-bf16orqwen3.5-4b-fp8qwen3.5-27b-fp8qwen3.6-35b-a3b-fp8qwen3.8-27b-fp8qwen3.8-27b-nvfp4(B200 only)
Models and GPUs lists the GPUs able to run each model.
Images
Store images in a BINARY column before querying them. You can check expenses
in CORD, a dataset of
1,000 receipt photos from Indonesian restaurants priced in rupiah.
Download CORD's Parquet files from Hugging Face and put each photo's bytes in a
BINARY column, with an id for each receipt:
import pyarrow as pa
import pyarrow.parquet as pq
from huggingface_hub import HfApi, hf_hub_download
repo = "naver-clova-ix/cord-v2"
names = [name for name in HfApi().list_repo_files(repo, repo_type="dataset")
if name.endswith(".parquet")]
photos = pa.concat_tables(
pq.read_table(hf_hub_download(repo, name, repo_type="dataset"), columns=["image"])
for name in sorted(names))
images = [image["bytes"] for image in photos.column("image").to_pylist()]
receipts = pa.table({
"id": [f"r{index}" for index in range(len(images))],
"image": pa.array(images, pa.binary()),
})Register the table in a session so SQL can read the rows:
import quail
config = quail.EngineConfig(model="qwen3.5-4b-bf16", device="h100-sxm")
session = quail.Session(config=config)
session.register("receipts", receipts, id_col="id")Pass the image column to an AI function. AI.IF finds each receipt over 100,000
rupiah, and AI.CLASSIFY labels what each receipt over the limit paid for:
SELECT r.id,
AI.CLASSIFY(r.image, ['meal', 'drinks only', 'bakery or snacks']) AS category
FROM receipts r
WHERE AI.IF(PROMPT('Is this receipt''s total over 100,000 rupiah? {0}', r.image));![]() | ![]() | ![]() |
|---|---|---|
174,600: returned, meal | 230,000: returned, bakery or snacks | 91,000: filtered out |
You get answers for all 1,000 receipts in 92 seconds on one H100 with
qwen3.5-4b-bf16, for $0.10. The filter matches 96.8% of the 972 receipts with
a labeled total. The query runs fast because AI.CLASSIFY reuses KV, the
model's saved state for each receipt, from AI.IF.
Shrink large images
Vision models read one token per 32 by 32 pixels, so large photos cost thousands
of tokens. Shrink each photo on the CPU with AI.IMAGE, where the second
argument sets the longest side in pixels:
SELECT r.id,
AI.CLASSIFY(AI.IMAGE(r.image, 1024),
['meal', 'drinks only', 'bakery or snacks']) AS category
FROM receipts r
WHERE AI.IF(PROMPT('Is this receipt''s total over 100,000 rupiah? {0}',
AI.IMAGE(r.image, 1024)));AI.IMAGE(r.image, 1024) runs 4 times faster on CORD's photos and lowers
accuracy by 0.1 percentage points.
PDFs
Store PDFs in a BINARY column too. Load legal contracts, such as leases and
service agreements, from a folder named contracts:
from pathlib import Path
paths = sorted(Path("contracts").glob("*.pdf"))
files = pa.table({
"path": [path.name for path in paths],
"pdf": pa.array([path.read_bytes() for path in paths], pa.binary()),
})
session.register("files", files, id_col="path")Ask about each page
Turn each PDF into one row per page when asking about individual pages.
AI.PDF_PAGES returns a list of structs, each holding page (the page number
from 1) and image (a PNG image). UNNEST turns the list into rows so AI.IF
can find each contract page with a liability cap, a clause limiting how much one
party can owe the other:
SELECT p.path, p.u['page'] AS page
FROM (SELECT f.path, UNNEST(AI.PDF_PAGES(f.pdf, 1024)) AS u FROM files f) p
WHERE AI.IF(PROMPT('Does this page cap either party''s liability? {0}',
p.u['image']));Ask about a whole PDF
Pass the list from AI.PDF_PAGES directly to an AI function to ask about a
whole PDF. The AI function reads the pages together as one document. Set the
first and last pages with the third and fourth arguments of AI.PDF_PAGES to
skip later pages on a long PDF:
SELECT f.path,
AI.CLASSIFY(AI.PDF_PAGES(f.pdf, 1024, 1, 3),
['lease', 'invoice', 'annual report', 'other']) AS kind
FROM files f;Choose a page size
Set the second argument of AI.PDF_PAGES, the longest side of each page image
in pixels, to trade small-print accuracy against token count. The default is
1,536 pixels, and a US Letter page at 1,024 pixels costs 800 tokens.
Parse to text
AI.PARSE_DOCUMENT reads the text stored in a PDF on the CPU with the Rust
crate pdf-extract and puts a form feed
('\f') between pages. Pass the parsed text to AI.EXTRACT, which copies
answers from text and rejects images:
SELECT f.path,
AI.EXTRACT(AI.PARSE_DOCUMENT(f.pdf),
{'effective_date': 'What is the effective date?'}) AS answers
FROM files f;AI.PARSE_DOCUMENT returns an empty string for a scanned page because a scanned
PDF holds pictures of pages rather than text, and returns NULL for a malformed
PDF.
AI functions describes every argument of
AI.PDF_PAGES, AI.PARSE_DOCUMENT, and AI.IMAGE. CORD (Park et al., 2019)
provides the receipts under
CC BY 4.0.


