Skip to main content

Python SDK Reference

See the Python SDK Guide for usage examples and getting started.

Client

Initialize a client with configuration options:

Resources

All resources are available on both Client (sync) and AsyncClient (async).

Chat Completions

client.chat.completions.create(...)

Creates a chat completion. Supports single-model and multi-model routing.

Parameters

gateway.metadata fields

Only the four fields above are accepted. Use custom_fields for arbitrary key-value pairs.

Response (non-streaming)

Response (streaming)

Returns a Stream that yields ChatCompletionChunk objects.

Responses

client.responses.create(...)

Creates a response using the OpenAI Response API format. Supports single-model and multi-model routing.

Parameters

Response (non-streaming)

Response (streaming)

Returns a ResponseStream that yields Response API events.
routing_metadata on completed_response is available for both streaming and non-streaming responses. For streaming, it’s populated after iteration completes.

Models

Query the model catalog:

Identity

Get current API key identity:

Error Classes

All errors extend ArbytraAPIError. Dispatch is driven by the type field of the canonical error envelope (see Errors for the full envelope and retry policy).

ArbytraAPIError Fields

Unknown error responses fall through to the base ArbytraAPIError class. Always keep a catch-all for forward compatibility.

Response Headers

Available on ChatCompletion.response_headers and Stream.response_headers:

ArbytraAsyncOpenAI (experimental)

ArbytraAsyncOpenAI is an AsyncOpenAI subclass that captures routing metadata from every successful response. Use it with frameworks that accept an external AsyncOpenAI instance. This is a Tier 4 experimental integration. For most use cases, use the native SDK or AsyncOpenAI(base_url=...) directly. Install with the optional openai-compat extra:

Constructor

All named parameters are keyword-only.

last_routing_metadata property

Returns RoutingMetadata | None. Populated after a successful response completes. Returns None before any request, after a request errors, or when the response carried no routing_metadata field. Concurrency caveat: the property uses last-write-wins semantics on a shared client. For per-request capture across concurrent callers, use the on_response callback. Streaming caveat: metadata is extracted during SDK byte iteration, not on stream creation. A streaming caller that does not iterate every chunk may read None.

on_response callback

Signature: Callable[[RoutingMetadata], Any].
  • Sync only. Passing an async callable raises TypeError at construction.
  • Fires once per successful response with a populated routing_metadata. Does not fire on error status, absent routing_metadata, or malformed routing_metadata.
  • Use this callback for per-request capture in concurrent scenarios where the shared last_routing_metadata property is race-prone.
Import RoutingMetadata for type annotations from arbytra.route_types:
RoutingMetadata is not exported at top-level arbytra. The import from arbytra import RoutingMetadata raises ImportError.

Error behavior

ArbytraAsyncOpenAI raises dual-inheritance errors for HTTP failures (4xx, 5xx). Each error is catchable as both an Arbytra error and an OpenAI error:
The same error is also catchable as openai.RateLimitError. No map_openai_error() wrapping is needed. Network-layer exceptions (openai.APITimeoutError, openai.APIConnectionError) propagate unchanged.
Mid-stream SSE errors (raised after the HTTP 200 during stream=True) remain unmapped openai.APIError. The bridge covers HTTP-level status errors only.
For map_openai_error() usage with plain AsyncOpenAI, see Error mapping.

Types

Client & Stream

Chat Response Types

Response Types

Common Types

Routing Types

Extensions

Model Catalog Types

Error Classes

Utilities

parse_routing_metadata(response) extracts RoutingMetadata from an OpenAI SDK response. Returns None if absent or unparseable. Returns None on Arbytra SDK responses — use response.routing_metadata directly instead. parse_routing_metadata is Python-only. TypeScript SDK responses include routing_metadata as a typed property. map_error_from_code(code, message, *, param=None, doc_url=None, provider=None, suggestion=None, response_headers=None) constructs a typed ArbytraAPIError subclass from an error code string (e.g., "rate_limit_error"RateLimitError).