Sub topic 9
Sending a PDF directly
This chapter assumes you can call an LLM chat API through an SDK, you know what a content block is, and you can load a file as base64 (the load_as_base64 helper from earlier chapters). It covers sending a PDF as a single input and what the provider does with it.
How native PDF input works
A native PDF request sends the whole file as one document content block. The provider then reads the file for you. It typically extracts the text of each page and also renders each page as an image. The model sees both, so it can use the text layer and also read charts, tables and scanned pages.
The request has the same shape as an image request. Only the block type and the media type change.
Building the request
The example below uses the Anthropic Messages API. It reuses load_as_base64 and sends a document block followed by a text block.
import anthropic
client = anthropic.Anthropic()
media_type, data = load_as_base64("report.pdf") # media_type is application/pdf
message = client.messages.create(
model="MODEL_NAME", # replace with a model that supports PDF input
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": data,
},
},
{"type": "text", "text": "Summarize the key findings in this report."},
],
}
],
)
print(message.content[0].text)Three details matter here:
- The block type is
document, notimage. An image block with a PDF inside is rejected. - The
media_typemust beapplication/pdf. Other providers use different field names, so check your provider's documentation for the exact schema. - The document block comes before the question. Putting the file first and the instruction after it is the usual placement.
Limits to check first
Providers cap the size of a PDF file and the number of pages per request. Many also limit the total request size, and base64 adds about one third to the size of the file. A 24 MB PDF becomes roughly 32 MB once encoded, which can exceed a request limit that the raw file would pass.
Check these before you send:
- File size, after base64 encoding.
- Page count.
- Whether the file is password protected. Encrypted PDFs are usually refused.
- Whether the model you chose supports PDF input at all.
Exact numbers change between providers and models. Read the current documentation instead of hard-coding values from a tutorial.
Cost and what the model reads
Each page costs tokens twice over. The extracted text counts as text tokens, and the page image counts as image tokens. A 40-page PDF can therefore use far more of the context window than 40 pages of plain text would. Estimate the cost on a sample file before you run a batch.
Native input works best when layout carries meaning, such as tables, charts, forms and scanned pages. For a plain text document, extracting the text yourself is often cheaper. The chapter on choosing between text extraction, page images and native PDF covers that decision.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Request rejected as too large | Base64 encoding pushed the file over the limit | Split the PDF or render fewer pages |
| Error about media type | Wrong block type or media type | Use a `document` block with `application/pdf` |
| Error about the file being unreadable | Encrypted or corrupted PDF | Remove the password or re-export the file |
| Model does not support the input | Model lacks PDF input | Choose a PDF-capable model or render pages as images |
Exercise
A 30 MB PDF is rejected by a provider whose request limit is 32 MB. Why can this happen, and what are two ways to get the request through?
Show answer
Base64 encoding increases the size by about one third, so 30 MB becomes about 40 MB, which exceeds the limit. You can split the PDF into smaller parts and send them separately, or render only the needed pages as downscaled images and send those instead. Uploading the file through a files API, where the provider offers one, is another option.