Skip to main content
Anannas supports PDF file processing through the /chat/completions API.
PDFs can be sent as direct URLs or base64-encoded data URLs in the messages array, using the file content type. This works on all models, even if the model doesn’t natively support PDFs.
URLs are preferred for large files since they avoid payload bloat.Use base64 encoding when working with local or private files.

Using PDF URLs

For publicly accessible PDFs, send the file URL directly:
import requests

url = "https://api.anannas.ai/v1/chat/completions"
headers = {
    "Authorization": f"Bearer {API_KEY_REF}",
    "Content-Type": "application/json",
}

messages = [
    {
        "role": "user",
        "content": [
            { "type": "text", "text": "Summarize this PDF." },
            {
                "type": "file",
                "file": {
                    "filename": "sample.pdf",
                    "file_data": "https://example.com/sample.pdf",
                },
            },
        ],
    }
]

payload = { "model": "openai/gpt-4o-mini", "messages": messages }

response = requests.post(url, headers=headers, json=payload)
print(response.json())

Using Base64-Encoded PDFs

For local or private PDFs, encode to base64 and send as a data URL:
import base64
import requests

def encode_file_to_base64(file_path):
    with open(file_path, "rb") as f:
        return base64.b64encode(f.read()).decode("utf-8")

pdf_path = "document.pdf"
base64_pdf = encode_file_to_base64(pdf_path)
data_url = f"data:application/pdf;base64,{base64_pdf}"

url = "https://api.anannas.ai/v1/chat/completions"
headers = {
    "Authorization": f"Bearer {API_KEY_REF}",
    "Content-Type": "application/json",
}

messages = [
    {
        "role": "user",
        "content": [
            { "type": "text", "text": "Extract key points from this PDF." },
            {
                "type": "file",
                "file": {
                    "filename": "document.pdf",
                    "file_data": data_url,
                },
            },
        ],
    }
]

payload = { "model": "anthropic/claude-3-5-sonnet-20241022", "messages": messages }

response = requests.post(url, headers=headers, json=payload)
print(response.json())
Was this page helpful?