openrouter

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package openrouter adapts the OpenRouter HTTP protocol to canonical inference values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeEmbedding

func DecodeEmbedding(reader io.Reader) (inference.EmbeddingRequest, error)

DecodeEmbedding decodes one strict OpenRouter embeddings request.

func WriteError

func WriteError(w http.ResponseWriter, status int, message string, metadata map[string]any)

WriteError writes one OpenRouter error response.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, value any) error

WriteJSON writes one OpenRouter JSON value.

Types

type Architecture

type Architecture struct {
	InputModalities  []string `json:"input_modalities"`
	OutputModalities []string `json:"output_modalities"`
	Tokenizer        string   `json:"tokenizer"`
	InstructType     *string  `json:"instruct_type"`
}

Architecture describes one OpenRouter model architecture.

type CacheControl

type CacheControl struct {
	Type string `json:"type"`
}

CacheControl marks an ephemeral prompt-cache breakpoint.

type ChatRequest

type ChatRequest struct {
	Model               string               `json:"model,omitempty"`
	Models              []string             `json:"models,omitempty"`
	Messages            []Message            `json:"messages"`
	Temperature         *float32             `json:"temperature,omitempty"`
	TopP                *float32             `json:"top_p,omitempty"`
	TopK                *int                 `json:"top_k,omitempty"`
	N                   *int                 `json:"n,omitempty"`
	Stream              bool                 `json:"stream,omitempty"`
	StreamOptions       *StreamOptions       `json:"stream_options,omitempty"`
	Stop                json.RawMessage      `json:"stop,omitempty"`
	MaxTokens           *int                 `json:"max_tokens,omitempty"`
	MaxCompletionTokens *int                 `json:"max_completion_tokens,omitempty"`
	PresencePenalty     *float32             `json:"presence_penalty,omitempty"`
	FrequencyPenalty    *float32             `json:"frequency_penalty,omitempty"`
	RepetitionPenalty   *float32             `json:"repetition_penalty,omitempty"`
	MinP                *float32             `json:"min_p,omitempty"`
	TopA                *float32             `json:"top_a,omitempty"`
	LogitBias           map[string]int       `json:"logit_bias,omitempty"`
	LogProbs            *bool                `json:"logprobs,omitempty"`
	TopLogProbs         *int                 `json:"top_logprobs,omitempty"`
	User                string               `json:"user,omitempty"`
	Seed                *int                 `json:"seed,omitempty"`
	Tools               []Tool               `json:"tools,omitempty"`
	ToolChoice          json.RawMessage      `json:"tool_choice,omitempty"`
	ParallelToolCalls   *bool                `json:"parallel_tool_calls,omitempty"`
	ResponseFormat      *ResponseFormat      `json:"response_format,omitempty"`
	Reasoning           *Reasoning           `json:"reasoning,omitempty"`
	ReasoningEffort     string               `json:"reasoning_effort,omitempty"`
	Provider            *ProviderPreferences `json:"provider,omitempty"`
	Route               string               `json:"route,omitempty"`
	Transforms          []string             `json:"transforms,omitempty"`
	Plugins             []json.RawMessage    `json:"plugins,omitempty"`
	Usage               json.RawMessage      `json:"usage,omitempty"`
	Debug               json.RawMessage      `json:"debug,omitempty"`
}

ChatRequest is the OpenRouter chat-completions wire request.

type ChatResponse

type ChatResponse struct {
	ID                string   `json:"id"`
	Object            string   `json:"object"`
	Created           int64    `json:"created"`
	Model             string   `json:"model"`
	Provider          string   `json:"provider,omitempty"`
	Choices           []Choice `json:"choices"`
	Usage             Usage    `json:"usage"`
	SystemFingerprint *string  `json:"system_fingerprint"`
}

ChatResponse is the OpenRouter chat-completions wire response.

func EncodeChat

func EncodeChat(response inference.ChatResponse) ChatResponse

EncodeChat converts one canonical chat result to OpenRouter wire values.

type Choice

type Choice struct {
	Index              int             `json:"index"`
	Message            ResponseMessage `json:"message"`
	FinishReason       string          `json:"finish_reason"`
	NativeFinishReason string          `json:"native_finish_reason,omitempty"`
	LogProbs           *LogProbs       `json:"logprobs,omitempty"`
}

Choice is one OpenRouter chat-completions result.

type CompletionTokenDetails

type CompletionTokenDetails struct {
	ReasoningTokens int `json:"reasoning_tokens"`
}

CompletionTokenDetails contains reasoning-token accounting.

type ContentPart

type ContentPart struct {
	Type         string        `json:"type"`
	Text         string        `json:"text,omitempty"`
	ImageURL     *ImageURL     `json:"image_url,omitempty"`
	CacheControl *CacheControl `json:"cache_control,omitempty"`
}

ContentPart is one OpenRouter multipart message input.

type DecodedChat

type DecodedChat struct {
	Inference inference.ChatRequest
	Route     string
	Provider  *ProviderPreferences
}

DecodedChat contains canonical inference and OpenRouter routing policy.

func DecodeChat

func DecodeChat(reader io.Reader) (DecodedChat, error)

DecodeChat decodes one strict OpenRouter request into canonical inference.

type Embedding

type Embedding struct {
	Object    string    `json:"object"`
	Index     int       `json:"index"`
	Embedding []float32 `json:"embedding"`
}

Embedding is one OpenRouter embedding vector.

type EmbeddingRequest

type EmbeddingRequest struct {
	Model          string          `json:"model"`
	Input          json.RawMessage `json:"input"`
	EncodingFormat string          `json:"encoding_format,omitempty"`
	Dimensions     *int            `json:"dimensions,omitempty"`
	User           string          `json:"user,omitempty"`
}

EmbeddingRequest is the OpenRouter embeddings wire request.

type EmbeddingResponse

type EmbeddingResponse struct {
	Object   string      `json:"object"`
	Data     []Embedding `json:"data"`
	Model    string      `json:"model"`
	Provider string      `json:"provider,omitempty"`
	Usage    Usage       `json:"usage"`
}

EmbeddingResponse is the OpenRouter embeddings wire response.

func EncodeEmbedding

func EncodeEmbedding(response inference.EmbeddingResponse) EmbeddingResponse

EncodeEmbedding converts one canonical embedding result to OpenRouter wire values.

type ErrorDetail

type ErrorDetail struct {
	Code     int            `json:"code"`
	Message  string         `json:"message"`
	Metadata map[string]any `json:"metadata,omitempty"`
}

ErrorDetail is one OpenRouter API error.

type ErrorResponse

type ErrorResponse struct {
	Error ErrorDetail `json:"error"`
}

ErrorResponse is the OpenRouter error envelope.

type Function

type Function struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Parameters  json.RawMessage `json:"parameters"`
}

Function defines an OpenRouter function tool.

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

FunctionCall is one requested function invocation.

type ImageURL

type ImageURL struct {
	URL    string `json:"url"`
	Detail string `json:"detail,omitempty"`
}

ImageURL is one OpenRouter image input.

type JSONSchema

type JSONSchema struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Schema      json.RawMessage `json:"schema"`
	Strict      bool            `json:"strict,omitempty"`
}

JSONSchema describes one structured output.

type LogProb

type LogProb struct {
	Token       string       `json:"token"`
	LogProb     float64      `json:"logprob"`
	Bytes       []int        `json:"bytes,omitempty"`
	TopLogProbs []TopLogProb `json:"top_logprobs"`
}

LogProb is one output-token probability.

type LogProbs

type LogProbs struct {
	Content []LogProb `json:"content"`
}

LogProbs contains output-token log probabilities.

type Message

type Message struct {
	Role       string          `json:"role"`
	Content    json.RawMessage `json:"content,omitempty"`
	Reasoning  string          `json:"reasoning,omitempty"`
	Name       string          `json:"name,omitempty"`
	ToolCalls  []ToolCall      `json:"tool_calls,omitempty"`
	ToolCallID string          `json:"tool_call_id,omitempty"`
}

Message is one OpenRouter chat message.

type MessageDelta

type MessageDelta struct {
	Role      string     `json:"role,omitempty"`
	Content   string     `json:"content,omitempty"`
	Reasoning string     `json:"reasoning,omitempty"`
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}

MessageDelta is one streamed OpenRouter message update.

type Model

type Model struct {
	ID                  string        `json:"id"`
	CanonicalSlug       string        `json:"canonical_slug,omitempty"`
	Name                string        `json:"name"`
	Created             int64         `json:"created"`
	Description         string        `json:"description,omitempty"`
	ContextLength       int           `json:"context_length"`
	Architecture        *Architecture `json:"architecture,omitempty"`
	Pricing             *Pricing      `json:"pricing,omitempty"`
	TopProvider         *TopProvider  `json:"top_provider,omitempty"`
	SupportedParameters []string      `json:"supported_parameters"`
}

Model is one OpenRouter model resource.

type ModelLinks struct {
	Next *string `json:"next"`
}

ModelLinks contains OpenRouter model-list pagination links.

type ModelList

type ModelList struct {
	Data       []Model    `json:"data"`
	TotalCount int        `json:"total_count"`
	Links      ModelLinks `json:"links"`
}

ModelList is the OpenRouter model-list response.

type Pricing

type Pricing struct {
	Prompt     string `json:"prompt"`
	Completion string `json:"completion"`
}

Pricing contains OpenRouter per-token prices.

type ProviderPreferences

type ProviderPreferences struct {
	Order             []string        `json:"order,omitempty"`
	Only              []string        `json:"only,omitempty"`
	Ignore            []string        `json:"ignore,omitempty"`
	AllowFallbacks    *bool           `json:"allow_fallbacks,omitempty"`
	RequireParameters *bool           `json:"require_parameters,omitempty"`
	DataCollection    string          `json:"data_collection,omitempty"`
	ZDR               *bool           `json:"zdr,omitempty"`
	Sort              string          `json:"sort,omitempty"`
	MaxPrice          json.RawMessage `json:"max_price,omitempty"`
}

ProviderPreferences is the OpenRouter provider-routing policy.

type Reasoning

type Reasoning struct {
	Effort    string `json:"effort,omitempty"`
	MaxTokens *int   `json:"max_tokens,omitempty"`
	Exclude   bool   `json:"exclude,omitempty"`
}

Reasoning configures OpenRouter reasoning behavior.

type ResponseFormat

type ResponseFormat struct {
	Type       string      `json:"type"`
	JSONSchema *JSONSchema `json:"json_schema,omitempty"`
}

ResponseFormat is the OpenRouter response-format contract.

type ResponseMessage

type ResponseMessage struct {
	Role      string     `json:"role"`
	Content   string     `json:"content"`
	Reasoning string     `json:"reasoning,omitempty"`
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}

ResponseMessage is one OpenRouter assistant message.

type StreamChoice

type StreamChoice struct {
	Index        int          `json:"index"`
	Delta        MessageDelta `json:"delta"`
	FinishReason *string      `json:"finish_reason"`
	LogProbs     *LogProbs    `json:"logprobs,omitempty"`
}

StreamChoice is one streamed OpenRouter choice.

type StreamChunk

type StreamChunk struct {
	ID                string         `json:"id"`
	Object            string         `json:"object"`
	Created           int64          `json:"created"`
	Model             string         `json:"model"`
	Provider          string         `json:"provider,omitempty"`
	Choices           []StreamChoice `json:"choices"`
	Usage             *Usage         `json:"usage,omitempty"`
	SystemFingerprint *string        `json:"system_fingerprint"`
}

StreamChunk is one OpenRouter SSE data value.

func EncodeStream

func EncodeStream(event inference.StreamEvent) StreamChunk

EncodeStream converts one canonical stream event to an OpenRouter chunk.

type StreamErrorChunk

type StreamErrorChunk struct {
	ID      string         `json:"id,omitempty"`
	Object  string         `json:"object"`
	Created int64          `json:"created,omitempty"`
	Model   string         `json:"model,omitempty"`
	Error   ErrorDetail    `json:"error"`
	Choices []StreamChoice `json:"choices"`
}

StreamErrorChunk is the OpenRouter mid-stream error contract.

func EncodeStreamError

func EncodeStreamError(event inference.StreamEvent, code int, message string, metadata map[string]any) StreamErrorChunk

EncodeStreamError creates an OpenRouter mid-stream error chunk.

type StreamOptions

type StreamOptions struct {
	IncludeUsage bool `json:"include_usage,omitempty"`
}

StreamOptions controls OpenRouter stream usage events.

type Tool

type Tool struct {
	Type     string   `json:"type"`
	Function Function `json:"function"`
}

Tool is an OpenRouter function tool.

type ToolCall

type ToolCall struct {
	ID       string       `json:"id"`
	Type     string       `json:"type"`
	Function FunctionCall `json:"function"`
}

ToolCall is an OpenRouter function call.

type TopLogProb

type TopLogProb struct {
	Token   string  `json:"token"`
	LogProb float64 `json:"logprob"`
	Bytes   []int   `json:"bytes,omitempty"`
}

TopLogProb is one alternate output token.

type TopProvider

type TopProvider struct {
	ContextLength       int `json:"context_length"`
	MaxCompletionTokens int `json:"max_completion_tokens"`
}

TopProvider contains representative provider limits.

type Usage

type Usage struct {
	PromptTokens           int                     `json:"prompt_tokens"`
	CompletionTokens       int                     `json:"completion_tokens"`
	TotalTokens            int                     `json:"total_tokens"`
	CompletionTokenDetails *CompletionTokenDetails `json:"completion_tokens_details,omitempty"`
}

Usage is OpenRouter token accounting.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL