Careeroza
PDF Input
Send the PDF Directly
Current Claude models read PDFs natively, including tables, charts and scanned pages: send a document content block with the file as base64 (or upload it once with the Files API and refer to its file_id). Text and images on each page are both used.
import base64, anthropic
client = anthropic.Anthropic()
with open("shopease_seller_agreement.pdf", "rb") as f:
pdf_b64 = base64.standard_b64encode(f.read()).decode("utf-8")
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1500,
messages=[{
"role": "user",
"content": [
{"type": "document",
"source": {"type": "base64", "media_type": "application/pdf", "data": pdf_b64}},
{"type": "text", "text": "List the commission rate, payment cycle and notice period for termination."},
],
}],
)
print(next(b.text for b in response.content if b.type == "text"))Run it
python read_pdf.pyExample output
- Commission: 12% of the selling price for fashion, 8% for electronics (Section 4.1) - Payment cycle: payouts every 7 days after delivery, to the registered bank account (Section 5.2) - Termination: either party with 30 days' written notice (Section 11.3)
Base64 and Size
Example
Base64 turns bytes into text so they fit in JSON, at the cost of about 33% more size. Check file sizes before sending (requests have size limits; large PDFs are better uploaded via the Files API):
import base64, os, tempfile
path = os.path.join(tempfile.gettempdir(), "genai_medium_demo.pdf")
with open(path, "wb") as f:
f.write(b"%PDF-1.4\n" + b"x" * 3000 + b"\n%%EOF") # a fake 'PDF' just for sizes
with open(path, "rb") as f:
raw = f.read()
os.remove(path)
b64 = base64.standard_b64encode(raw).decode("utf-8")
print("starts like a PDF:", raw[:5] == b"%PDF-")
print("raw bytes:", len(raw), "| base64 chars:", len(b64), f"| growth: {len(b64) / len(raw):.2f}x")
print("first chars:", b64[:16])Output
starts like a PDF: True raw bytes: 3015 | base64 chars: 4020 | growth: 1.33x first chars: JVBERi0xLjQKeHh4
Tip: Each PDF page costs input tokens for its text and for a page image, so a 100-page PDF is expensive. If you only need the text, extract it first (e.g. with pypdf) and send text; send the PDF when layout, tables or images matter.