Files
openrouter-python-sdk-retry…/USAGE.md
T
Matt Apperson 5fc522c72f Initial commit: OpenRouter Python SDK
Auto-generated Python SDK for OpenRouter API, providing comprehensive client library with:
- Chat completions and streaming support
- Embeddings API
- Model and provider information
- API key management
- Analytics and usage tracking
- OAuth authentication flows
- Full type hints and error handling
2025-11-13 10:56:25 -05:00

4.0 KiB

# Synchronous Example
from openrouter import OpenRouter
import os


with OpenRouter(
    api_key=os.getenv("OPENROUTER_API_KEY", ""),
) as open_router:

    res = open_router.beta.responses.send(input=[
        {
            "type": "message",
            "role": "user",
            "content": "Hello, how are you?",
        },
    ], metadata={
        "user_id": "123",
        "session_id": "abc-def-ghi",
    }, tools=[
        {
            "type": "function",
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                    },
                },
            },
        },
    ], model="anthropic/claude-4.5-sonnet-20250929", text={
        "format_": {
            "type": "text",
        },
        "verbosity": "medium",
    }, reasoning={
        "summary": "auto",
        "enabled": True,
    }, temperature=0.7, top_p=0.9, prompt={
        "id": "<id>",
        "variables": {
            "key": {
                "type": "input_text",
                "text": "Hello, how can I help you?",
            },
        },
    }, service_tier="auto", truncation="auto", stream=False, provider={
        "data_collection": "deny",
        "zdr": True,
        "enforce_distillable_text": True,
        "order": [
            "OpenAI",
        ],
        "only": [
            "OpenAI",
        ],
        "ignore": [
            "OpenAI",
        ],
        "quantizations": None,
        "sort": "price",
    })

    with res as event_stream:
        for event in event_stream:
            # handle event
            print(event, flush=True)

The same SDK client can also be used to make asynchronous requests by importing asyncio.

# Asynchronous Example
import asyncio
from openrouter import OpenRouter
import os

async def main():

    async with OpenRouter(
        api_key=os.getenv("OPENROUTER_API_KEY", ""),
    ) as open_router:

        res = await open_router.beta.responses.send_async(input=[
            {
                "type": "message",
                "role": "user",
                "content": "Hello, how are you?",
            },
        ], metadata={
            "user_id": "123",
            "session_id": "abc-def-ghi",
        }, tools=[
            {
                "type": "function",
                "name": "get_current_weather",
                "description": "Get the current weather in a given location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                        },
                    },
                },
            },
        ], model="anthropic/claude-4.5-sonnet-20250929", text={
            "format_": {
                "type": "text",
            },
            "verbosity": "medium",
        }, reasoning={
            "summary": "auto",
            "enabled": True,
        }, temperature=0.7, top_p=0.9, prompt={
            "id": "<id>",
            "variables": {
                "key": {
                    "type": "input_text",
                    "text": "Hello, how can I help you?",
                },
            },
        }, service_tier="auto", truncation="auto", stream=False, provider={
            "data_collection": "deny",
            "zdr": True,
            "enforce_distillable_text": True,
            "order": [
                "OpenAI",
            ],
            "only": [
                "OpenAI",
            ],
            "ignore": [
                "OpenAI",
            ],
            "quantizations": None,
            "sort": "price",
        })

        async with res as event_stream:
            async for event in event_stream:
                # handle event
                print(event, flush=True)

asyncio.run(main())