Skip to navigation

PDF Inputs

Send a PDF as a file part in a chat message. Models that take PDFs read the document directly, text and pages alike, so you can ask questions about it, summarise it or pull out data.

The samples use the OpenAI SDK set up as in the Quickstart.

Flow AI sends the PDF to the model as is. It doesn’t parse or OCR documents itself, so only models that take PDF input can read them; other models refuse the request with a 400 before anything is held.

From a local file

Encode the file as base64 and send it as a data:application/pdf;base64, URL in file_data, with a filename.

import base64
with open("invoice.pdf", "rb") as f:
pdf = base64.b64encode(f.read()).decode()
response = client.chat.completions.create(
model="<model with PDF input>",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What are the main points in this document?"},
{
"type": "file",
"file": {
"filename": "invoice.pdf",
"file_data": f"data:application/pdf;base64,{pdf}",
},
},
],
}],
)
print(response.choices[0].message.content)

From a URL

Some models also take a public link in file_data:

{
"type": "file",
"file": { "filename": "bitcoin.pdf", "file_data": "https://bitcoin.org/bitcoin.pdf" }
}

If a model doesn’t accept links, download the file and send it as base64.

Several documents

Add one file part per PDF in the same content list, and refer to them by file name in your prompt.

Tips

  • Ask for structure. Combine a PDF with structured outputs to pull fields such as totals, dates and line items as JSON.
  • Send only the pages you need. Every page costs input tokens, so split long documents first.
  • Reuse the same document. On models with prompt caching, put the PDF before the question so follow-up questions read it from the cache.

Billing

The model turns each page into input tokens at its input price. A page with images or scans usually takes more tokens than a page of text. Check usage.prompt_tokens on a first request.