opendunes
Inputs · 3 min
Copy page ▾

PDFs in

Send a PDF with your question; get an answer about it.

  1. Pick a model that reads PDFs.
  2. Add the PDF as a part of your message.
  3. Choose how the PDF is read.
  4. Read the answer.

1. Pick a model that reads PDFs

Models that take files in lists the ones that read a PDF themselves; the sample below uses anthropic/claude-sonnet-4.5. A model that cannot is still served: the PDF is read first, by a parser, and the model gets its text (step 3).

2. Add the PDF as a part of your message

A message's content is a text, or a list of parts. To send a PDF, make it a list: one text part for your question, one file part for the PDF.

PartHolds
{"type": "text", "text": "..."}your question
{"type": "file", "file": {"filename": "...", "file_data": "..."}}the PDF: a public link, or the PDF itself as a data:application/pdf;base64,... URL
{"type": "file", "file": {"file_id": "..."}}a PDF you kept in Files: see Files and storage
import json
import os
import urllib.request

API_KEY = os.environ["OPENDUNES_API_KEY"]
ADDRESS = "https://opendunes.com/v1/chat/completions"
MODEL = "anthropic/claude-sonnet-4.5"
PROMPT = "Summarise this document in two sentences."
FILENAME = "dummy.pdf"
PDF = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"

body = {
    "model": MODEL,
    "messages": [
        {
            "role": "user",
            "content": [
                {"type": "text", "text": PROMPT},
                {
                    "type": "file",
                    "file": {"filename": FILENAME, "file_data": PDF},
                },
            ],
        }
    ],
}

request = urllib.request.Request(
    ADDRESS,
    data=json.dumps(body).encode(),
    headers={
        "Authorization": "Bearer " + API_KEY,
        "Content-Type": "application/json",
    },
)
with urllib.request.urlopen(request) as response:
    reply = json.load(response)

print(reply["choices"][0]["message"]["content"])
print(reply["usage"]["cost"])
Do

Send a link when the PDF is online, a file_id when you keep it in Files and use it often, and a data: URL when it is a file on your side. The whole request stays under 32 MB.

Don't

Don't send a file_id that is not yours, or whose file has expired. The request is refused with a 422.

3. Choose how the PDF is read

A PDF can be read by the model itself, or by a parser that turns it into text first. Name the parser in plugins when the default is not the one you want.

You sendThe PDF is read byCharged as
nothingthe model, if it reads PDFs; otherwise the page-billed parserthe model's input tokens, or the pages
"plugins": [{"id": "file-parser", "pdf": {"engine": "native"}}]the model itselfinput tokens, beside your words
"plugins": [{"id": "file-parser", "pdf": {"engine": "cloudflare-ai"}}]a text parserno charge for the parsing; the text it finds is input tokens
"plugins": [{"id": "file-parser", "pdf": {"engine": "mistral-ocr"}}]an OCR parser, for scansthe pages it reads
Good to know

A scanned PDF holds pictures of text, not text. Name mistral-ocr for it.

4. Read the answer

The answer is the text in choices[0].message.content, as on any text request. usage.cost is the whole charge, the parsing included.

To send the answer back as a stream, see Streaming. To hold the answer to a shape, see Structured outputs. Every field of the request: Chat.

Errors

The body says why. What to do on every status: Errors and retries.

StatusMeans
400the model takes no PDFs, and no parser was named for it
402the balanceBalanceWhat you can spend, in DA.More on Balance and charges → cannot cover the request
404no such model
413the request is larger than the door takes
422the file_id is not yours, or its file has expired