Skip to content

file type for PDF Multimodal support #38

@EAG08

Description

@EAG08

This appears to be supported via the API, but not from the python SDK https://openrouter.ai/docs/guides/overview/multimodal/pdfs

import base64
from openrouter import OpenRouter


def encode_pdf_to_base64(pdf_path):
    """Read a PDF file and encode it to base64 data URL."""
    with open(pdf_path, 'rb') as pdf_file:
        pdf_data = pdf_file.read()
        base64_encoded = base64.b64encode(pdf_data).decode('utf-8')
        return f"data:application/pdf;base64,{base64_encoded}"


def process_pdf(pdf_path, prompt="What is in this PDF?", model="anthropic/claude-sonnet-4.5"):
    """
    Send a PDF to OpenRouter and get a response.

    Args:
        pdf_path: Path to the local PDF file
        prompt: Question or instruction about the PDF
        model: Model to use for processing

    Returns:
        Response content from the model
    """
    pdf_data = encode_pdf_to_base64(pdf_path)
    filename = os.path.basename(pdf_path)

    with OpenRouter(api_key=os.getenv("OPENROUTER_API_KEY")) as client:
        # Try using the REST API format directly
        response = client.chat.send(
            model=model,
            messages=[
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": prompt},
                        {
                            "type": "file",
                            "file": {
                                "filename": filename,
                                "file_data": pdf_data
                            }
                        }
                    ]
                }
            ]
        )

        return response.choices[0].message.content


if __name__ == "__main__":
    pdf_file = "example.pdf"
    result = process_pdf(pdf_file, "Summarize this document")
    print(result)

Input tag 'file' found using <lambda>() does not match any of the expected tags: 'text', 'image_url', 'input_audio', 'input_video', 'video_url' [type=union_tag_invalid, input_value={'type': 'file', 'file': ...lZgoyMjE2ODEKJSVFT0YK'}}, input_type=dict]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions