Skip to main content

respan-gateway for Python

The respan-gateway LLM gateway as a Python package: use it in-process with the official OpenAI and Anthropic SDKs (no socket, no server), run it as a local server, or use the same command line as the Rust binary. The gateway is the Rust one, compiled into a native extension (abi3, Python 3.10+).

Install

pip install respan-gateway            # add `openai` and/or `anthropic` for gw.openai() / gw.anthropic()

Use the SDKs in-process

import respan_gateway as rg

with rg.Gateway() as gw:             # from OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, ...
    client = gw.openai()             # an openai.OpenAI that talks to gw directly

    # A Claude model through the OpenAI SDK is translated to Anthropic.
    reply = client.chat.completions.create(
        model="claude-haiku-5-5", messages=[{"role": "user", "content": "hi"}]
    )

gw.anthropic() is the same for anthropic.Anthropic; gw.openai_async() and gw.anthropic_async() give the async clients. Leaving the with (or async with) block stops the servers the gateway started and closes the clients it handed out. For any other httpx-based client, gw.transport() and gw.async_transport() are httpx transports: httpx.Client(transport=gw.transport()). Raw requests: gw.request("POST", "/v1/chat/completions", json=...) returns an httpx.Response; gw.stream(...) yields the body chunk by chunk as it arrives. arequest and astream are the asyncio versions.

Every kind

Each kind is an OpenAI endpoint the gateway serves when a provider speaks its format (openai-images, openai-transcriptions, openai-speech, openai-embeddings, openai-videos; see docs/configuration.md), and works through the same client:

client.images.generate(model="gpt-image-1", prompt="a red fox").data[0].b64_json

with open("talk.wav", "rb") as f:                      # a multipart upload
    client.audio.transcriptions.create(model="whisper-1", file=f).text

client.audio.speech.create(model="tts-1", voice="alloy", input="hi").read()   # audio bytes

client.embeddings.create(model="text-embedding-3-small", input="hi").data[0].embedding
# (the SDK asks for base64 and decodes it; the gateway passes it through)

job = client.videos.create(model="veo-3.0-generate-001", prompt="waves")  # Veo; then poll:
job = client.videos.retrieve(job.id)                             # queued -> completed
client.videos.download_content(job.id).read()                    # mp4 bytes

A model's kind is read from its name and the providers' formats: gw.models() lists the merged models of every provider as /v1/models does, each with its kind (gw.models(kind="image") filters).

Errors

HTTP error statuses are responses (and the SDKs raise their own exceptions for them), never exceptions of this package. A failure to carry out a request or start a server is a GatewayError. A timeout is httpx's httpx.ReadTimeout, which an SDK raises as its own timeout error. An invalid configuration is a ConfigError (a ValueError).

What the gateway knows

gw.models()                # [{"id": "gpt-image-1", "owned_by": "openai", "kind": "image"}, ...]
gw.providers()             # each provider with its formats and request counts
gw.requests(limit=20)      # the request log, newest first; filters such as client="ci" (a client key's name)

amodels(), aproviders() and arequests() are the asyncio versions. providers() and requests() read the web UI's admin API in-process, so ui = false turns them off (the log is in memory, always on with the UI). A gateway in your process prints nothing per request: log defaults to off here (the binary's is text). Set log = 'text' or log = 'json' in the config for stderr lines, serve_forever() included.

Serve

with gw.serve(port=0) as server:     # a real socket, in the background
    print(server.url)                # http://127.0.0.1:54321

async with gw.aserve() as server:    # the same for asyncio
    ...

gw.serve_forever(port=8080)          # blocks; Ctrl-C stops it (serve() picks a free port by default)

The server is the binary's (server.url + "/ui/" is the web UI; wheels from PyPI embed it, a build from the sdist or a plain checkout shows a placeholder page unless apps/ui is built first). Without client_keys in the config, anyone who reaches it spends your provider keys: keep it on a local address. Gateway.openai()'s api_key is one of those client keys when the config sets them; otherwise a placeholder, forwarded only to a provider with no key of its own.

Realtime

WebSocket sessions (OpenAI Realtime, Gemini Live, translated between each other) need a socket: use serve() and connect any WebSocket client, with http replaced by ws:

import websockets

with gw.serve() as server:
    async with websockets.connect(server.url.replace("http", "ws") + "/v1/realtime?model=gpt-realtime") as ws:
        ...

Command line

respan-gateway                                # also: init, check, proxy --anthropic|--openai, --help
python -m respan_gateway proxy --openai

Configuration

Gateway(config=...) takes a dict (typed as rg.GatewayConfig for editor completion) or a TOML string in the format of gateway.example.toml; Gateway.from_file(path) reads a file. With no config, Gateway() starts as the binary does with no arguments: from ./gateway.toml if it exists, else from the environment: every vendor whose key is set (OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, GROQ_API_KEY, ...) becomes a provider, models route by name (claude-* to Anthropic, gemini-* to Gemini, or explicitly groq/openai/gpt-oss-20b), and RESPAN_<VENDOR>_BASE_URL points one elsewhere.

Development

cd python
uv venv && uv pip install maturin pytest httpx openai anthropic mypy
.venv/bin/maturin develop        # builds the extension into .venv
.venv/bin/pytest                 # tests run against a fake provider, offline
.venv/bin/mypy --strict python/respan_gateway

The extension is the respan-gateway-python crate (src/), a workspace member that depends on the respan-gateway library in apps/gateway. The extension-module feature is enabled by maturin only, so cargo test and cargo clippy of the workspace link normally. One tokio runtime, started on first use, serves every gateway, server and the command line; every call that waits releases the GIL. Async requests use a native callback API (send_async, next_chunk_async) rather than executor threads: a waiting request holds no thread, so many open streams cannot starve anything, and cancelling the awaiting task drops the in-flight router future, which cancels the provider call. Sync calls block with the GIL released and wake every 100 ms to deliver Ctrl-C. httpx's read timeout bounds the wait for the response head and each body chunk (connect, write and pool timeouts have nothing to bound in-process). A server from serve() keeps running when its handle is dropped, until stop() or the process exits.

Metadata

Release files for respan-gateway 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for respan-gateway 0.2.0
File Size Uploaded Provenance
respan_gateway-0.2.0.tar.gz 803.4 kB With provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for respan-gateway 0.2.0
File
respan_gateway-0.2.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
respan_gateway-0.2.0-cp310-abi3-manylinux_2_39_x86_64.whl CPython 3.10 abi3 Linux glibc 2.39+ x86-64 Details
respan_gateway-0.2.0-cp310-abi3-manylinux_2_39_aarch64.whl CPython 3.10 abi3 Linux glibc 2.39+ ARM64 Details
respan_gateway-0.2.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
respan_gateway-0.2.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details

Total release size: 48.8 MB

Release files / respan_gateway-0.2.0.tar.gz

Download URL respan_gateway-0.2.0.tar.gz
Size 803.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9bccb9c7b704d524f9021d78be9be21bc3b2e425ecea288cbfd1c1f1e70bd0ce
BLAKE2b-256 checksum
How to use checksums
3ab89327e1211416ca84dc48042c000f2230e5155af32b86996522ebebbc6356
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Release files / respan_gateway-0.2.0-cp310-abi3-win_amd64.whl

Download URL respan_gateway-0.2.0-cp310-abi3-win_amd64.whl
Size 9.5 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
ee7c09b8beb8e5a88187c8946e55b22a20d98f8afe8b8013ee01fd7009bc2b73
BLAKE2b-256 checksum
How to use checksums
b49adb6c65ed02a78b24a7df927c11813f119bc8a6288b3f4cf9d863a126fb2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Release files / respan_gateway-0.2.0-cp310-abi3-manylinux_2_39_x86_64.whl

Download URL respan_gateway-0.2.0-cp310-abi3-manylinux_2_39_x86_64.whl
Size 10.0 MB
Tags CPython 3.10 Linux glibc 2.39+ x86-64 abi3
SHA-256 checksum
How to use checksums
3bca61795608e83ee726cb2bc64fbcbf1480d6b798a91bf504be2d7d6e5bc3cb
BLAKE2b-256 checksum
How to use checksums
4e4bf9995cb7d3691fe6bb665766c10c3859c8757c589d6a6bac4bb47955fc07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Release files / respan_gateway-0.2.0-cp310-abi3-manylinux_2_39_aarch64.whl

Download URL respan_gateway-0.2.0-cp310-abi3-manylinux_2_39_aarch64.whl
Size 9.7 MB
Tags CPython 3.10 Linux glibc 2.39+ ARM64 abi3
SHA-256 checksum
How to use checksums
474e7db1adb1189c79748d63de6ea477bb345fc9646279a7d19cf613b52a1175
BLAKE2b-256 checksum
How to use checksums
e38c09385a4c543b48f438e3cadad68e1fbb1172d153eec06c8e2e32dc9285b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Release files / respan_gateway-0.2.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL respan_gateway-0.2.0-cp310-abi3-macosx_11_0_arm64.whl
Size 9.2 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
eef7a244df1327b943bf858ed76b95da0358e9a8e15a0b9a12e6dec122c0dfe2
BLAKE2b-256 checksum
How to use checksums
e43ad49eb5727595e4fe3dcfa18f6ee052ce99df7a5a424bf81526a67a6a9190
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Release files / respan_gateway-0.2.0-cp310-abi3-macosx_10_12_x86_64.whl

Download URL respan_gateway-0.2.0-cp310-abi3-macosx_10_12_x86_64.whl
Size 9.6 MB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
3a11af250796cba82a5f8473fe0d52b12d09efc0c1a75a1390c57753e8b6ac73
BLAKE2b-256 checksum
How to use checksums
c2d53500ab4defaf39e4b93696ca7ecc1ca3f0813aba523a44d2307a625e07a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Security for respan-gateway 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for respan_gateway-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.2.0 This release

6 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page