openai.OpenRouterClient

OpenRouter's chat-completions gateway.

Reference version

Signature

class openai.OpenRouterClient

OpenRouter's chat-completions gateway.

A plain bearer-auth chat-completions endpoint at https://openrouter.ai/api/v1, keyed by OPENROUTER_API_KEY. Model ids are namespaced by upstream provider ("openai/gpt-4o-mini", "anthropic/claude-3.5-sonnet", …).

App-attribution headers are not a special option: pass them through the ordinary headers field — headers = {"HTTP-Referer": "https://myapp.com", "X-Title": "My App"}. OpenRouter-specific body fields (provider, route, transforms) go through request_body.

Source:<builtin>/openai/openrouter.bamlbytes 5715100

Fields

model

string

api_key

ai.Credential | null

base_url

ai.Credential | null

request_body

baml.json.json | null

headers

map<string, string> | null

query_params

map<string, string> | null

temperature

float | null

max_tokens

int | null

top_p

float | null

stop

string[] | null

seed

int | null

capture_wire

bool

Static methods

function

new

(
model: string,
api_key: ai.Credential | null = …,
base_url: ai.Credential | null = …,
request_body: baml.json.json | null = …,
headers: map<string, string> | null = …,
query_params: map<string, string> | null = …,
temperature: float | null = …,
max_tokens: int | null = …,
top_p: float | null = …,
stop: string[] | null = …,
seed: int | null = …,
capture_wire: bool = …
) -> openai.OpenRouterClient

Creates a client. Construction reads no environment variable, so declaring one is always safe; credentials and endpoints resolve when a request is built.

Instance methods

function

compat

(self) -> openai.internal.ChatCompat throws baml.errors.Io | baml.errors.ParseError

The provider record: everything the shared chat core needs to know about this endpoint.

function

params

(
self,
preview: bool = …
) -> openai.internal.ChatParams throws baml.errors.Io | baml.errors.ParseError

The per-request parameters handed to the shared chat core.

Parameters

  • preview: omit the credential, for rendering a request without sending it.
function

resolved_api_key

(self) -> string | null throws baml.errors.Io | baml.errors.ParseError

The request-time credential: the explicit value, else OPENROUTER_API_KEY. Null is reported as a configuration error when the request is built.

function

resolved_base_url

(self) -> string throws baml.errors.Io | baml.errors.ParseError

The request-time base URL, or https://openrouter.ai/api/v1.

Implementations

ai.Client for openai.OpenRouterClient

Instance methods

function

id

(self) -> string
function

invoke

(
self,
input: ai.ModelTurnInput
) -> ai.ModelTurn throws baml.errors.Timeout | baml.errors.UnknownError | reflect.errors.CompilationError | ai.errors.Failure
function

render

(
self,
input: ai.ModelTurnInput
) -> baml.http.Request throws baml.errors.Timeout | baml.errors.UnknownError | reflect.errors.CompilationError | ai.errors.Failure

Source:<builtin>/openai/openrouter.bamlbytes 39274666

ai.stream.StreamingClient for openai.OpenRouterClient

Instance methods

function

invoke_stream

(
self,
input: ai.ModelTurnInput
) -> ai.stream.TurnStream throws baml.errors.Timeout | baml.errors.UnknownError | reflect.errors.CompilationError | ai.errors.Failure

Source:<builtin>/openai/openrouter.bamlbytes 46725098