Skip to main content

This is an unofficial wrapper of OpenRouter.

Project description

OpenRouter Provider

An unofficial Python wrapper for the OpenRouter API that provides a simple, intuitive interface for interacting with multiple LLM models. OpenRouter Provider supports chat conversations, image processing, tool integration, streaming responses, and structured output generation.

Features

  • Conversation Memory: Automatic chat history management with easy memory control
  • Image Processing: Built-in image resizing and base64 encoding for multimodal interactions
  • Tool Integration: Decorator-based function calling with automatic tool execution
  • Streaming Support: Real-time response streaming for both sync and async operations
  • Structured Output: JSON schema-based response formatting using Pydantic models
  • Async Support: Full async/await support for non-blocking operations
  • Provider Configuration: You can select your favorite provoder easily

Quick start

Installation

pip install openrouter-provider

Set API key

Please get your API key from OpenRouter and set it as environment valuable.

OPENROUTER_API_KEY="your-api-key-here"

Basic Usage

from openrouter import *
from dotenv import load_dotenv

load_dotenv()

client = OpenRouterClient(system_prompt="You are a friendly AI assistant.")
model = LLMModel(name="openai/gpt-5")
query = Message(text="Hello, how are you?")

reply = client.invoke(model=model, query=query)
print(reply.text)

Timeout and retries

from openrouter import *

client = OpenRouterClient(
    system_prompt="You are a friendly AI assistant.",
    timeout=30,
    max_retries=0,
)

reply = client.invoke(
    model=LLMModel(name="openai/gpt-5"),
    query=Message(text="Hello"),
    timeout=10,
    max_retries=2,
)
print(reply.text)

OpenAI-Compatible Endpoints

from openrouter import *

client = OpenRouterClient(
    system_prompt="You are a helpful assistant.",
    base_url="http://localhost:11434/v1",
)

model = LLMModel(name="llama3.1")
query = Message(text="Hello, how are you?")

reply = client.invoke(model=model, query=query)
print(reply.text)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openrouter_provider-1.0.14.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openrouter_provider-1.0.14-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file openrouter_provider-1.0.14.tar.gz.

File metadata

  • Download URL: openrouter_provider-1.0.14.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for openrouter_provider-1.0.14.tar.gz
Algorithm Hash digest
SHA256 59bd3fcf62990af050c756d9bf3ded37ad65fe4983de9d014914e07afe3e83b8
MD5 49dffbafeecfba569fb47cd67eb778b0
BLAKE2b-256 536bdbd1e98d48397735da7d5ccdb780b2085c2de5034df8a6d93fe063bf7007

See more details on using hashes here.

File details

Details for the file openrouter_provider-1.0.14-py3-none-any.whl.

File metadata

File hashes

Hashes for openrouter_provider-1.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 82cbb327be2441de46cfa6bbe79ecd0f205b8ccf0e4c4bb9ce6f76b28b05860d
MD5 337cb27d504bd14fed38d90960840af9
BLAKE2b-256 462941e29e6473d11100807ffab52b2487362b3b31c7e713521155aad8d7dafa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page