Skip to content
mentoor

Topic 1 of 4 · 4 min read

Sending PDFs and documents to a model

Goal: Choose between inline, URL and uploaded-file inputs for a document and send it with the question placed after the content.

Send a document as a typed content block, pick the cheapest route that fits its size and reuse, and put your question after the document. A model cannot open a file path on your disk. It only sees what is in the request.

Check yourself · 1 of 3

A 30-page PDF will be questioned dozens of times in one session. Which input route avoids re-sending it each time?

Choose one answer

Why it matters

A wrong route costs real money and latency. A 20 MB contract sent as base64 on every turn of a chat is uploaded again each time. A PDF behind a login cannot be fetched by URL. And a question placed before 80 pages of text is often answered worse than one placed after them.

The three routes

  1. Inline base64. You read the file, encode it and put it in the request body. Works anywhere, needs no extra call, but the whole file travels with every request.
  2. URL. You give a public link and the provider fetches it. Smallest request, but the file must be reachable without credentials and may change between calls.
  3. Uploaded file id. You upload once to a files endpoint (Claude's Files API, OpenAI's files endpoint) and reference the returned id. Best for large files and for asking many questions about one document.

How a PDF is read

Claude's PDF support treats each page as both extracted text and an image of the page, so charts, scans and layout are visible to the model. Providers differ: OpenAI's guide notes that for non-PDF files it does not extract embedded images or charts. Check the guide for the file type you send, because a Word file and a PDF may be handled differently. Convert binary formats such as .xlsx or .docx to text or PDF first when the provider does not accept them.

Where the question goes

Put the document block first and your instruction after it. Anthropic's long-context advice is the same for text and images: documents before the query.

Before and after

# Weak: question first, file re-sent as base64 on every call
content = [
    {"type": "text", "text": "What is the notice period?"},
    {"type": "document", "source": {"type": "base64",
        "media_type": "application/pdf", "data": b64}},
]
# Better: upload once, reference the id, question last
file_id = client.files.upload(file=open("contract.pdf", "rb")).id
content = [
    {"type": "document", "source": {"type": "file", "file_id": file_id}},
    {"type": "text", "text": "Quote the clause that sets the notice period."},
]

The exact field names differ by provider and SDK version, so copy the shape from the current docs.

What goes wrong

  • Passwords. Encrypted or password-protected PDFs are rejected.
  • Stale URLs. A signed link that expires before the provider fetches it fails with an error that looks like a model problem.
  • Wrong media type. A PDF labelled image/png is refused or misread.

Examples

Choosing a route

A support bot answers questions about a 4 MB product manual. Many users ask about the same manual all day.

  • Base64 on every call re-uploads 4 MB each time.
  • A public URL works if the manual is public and stable.
  • An uploaded file id is best here: upload once at deploy time, store the id, reference it per question.

Fixing the order

content = [
    {"type": "document", "source": {"type": "file", "file_id": file_id}},
    {"type": "text", "text": "List every termination right and cite the page."},
]

The document comes first, the instruction last, and the answer must cite pages so a person can open the PDF and check.

Key points

  • A model sees only what is in the request, so a file must be sent as a typed content block, never as a local path.
  • Inline base64 is simplest, a URL gives the smallest request, and an uploaded file id suits large or repeatedly used documents.
  • Claude reads each PDF page as extracted text plus an image, so charts and scans are visible; check how each provider treats other file types.
  • Place the document before the question, and ask for a quote or page reference so the answer can be checked.
  • Encrypted PDFs, expired signed URLs and wrong media types fail at the request layer, not in the model.

Common mistakes

  • Re-sending the same large PDF as base64 on every turn instead of uploading it once and reusing the id.
  • Putting the question before a long document, then blaming the model for a vague answer.
  • Using a URL that needs a login or expires quickly, so the provider cannot fetch the file.
  • Assuming every provider reads every file type the same way, instead of checking its guide for charts and embedded images.

Check yourself · 2 of 3

Where should the question go relative to a long document in the content blocks?

Choose one answer

Check yourself · 3 of 3

A request with a signed PDF URL fails with a fetch error ten minutes after the link was created. The link expires after five. What is the best fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic