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.3.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.3.0
File Size Uploaded Provenance
respan_gateway-0.3.0.tar.gz 843.6 kB With provenance Details

Built distributions (wheels)

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

Total release size: 49.0 MB

Release files / respan_gateway-0.3.0.tar.gz

Download URL respan_gateway-0.3.0.tar.gz
Size 843.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6b28a9622ff23f56df43834e09ed5ea097a0fdd7186c3d68cdb3e57f5acc7f3b
BLAKE2b-256 checksum
How to use checksums
142c003431ba1aced5e2e768172fbdfd8d0fef8e87967b7b24f9d2b5e0277047
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 25, 2026.

Transparency log

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

Download URL respan_gateway-0.3.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
82706ef3ec4afe448773c8a16289101b3f70a60d8b1a99efca963306a5b34ded
BLAKE2b-256 checksum
How to use checksums
0f4883302f89a3d93d4eb3c4fb0427aa4e0073a6f2dd6c8efdc14d727a3e93e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 25, 2026.

Transparency log

Release files / respan_gateway-0.3.0-cp310-abi3-manylinux_2_28_x86_64.whl

Download URL respan_gateway-0.3.0-cp310-abi3-manylinux_2_28_x86_64.whl
Size 10.0 MB
Tags CPython 3.10 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
48552edd8fa3d58cd8808cc6b6faae3e25011002254d21cdb488b36f56dcb820
BLAKE2b-256 checksum
How to use checksums
0b99f9f4d36be6f0d4592c09e27b87d2a9c71842b4a064be4bc4356f132ab7e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 25, 2026.

Transparency log

Release files / respan_gateway-0.3.0-cp310-abi3-manylinux_2_28_aarch64.whl

Download URL respan_gateway-0.3.0-cp310-abi3-manylinux_2_28_aarch64.whl
Size 9.7 MB
Tags CPython 3.10 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
28e3bfa6f0a7eb86776e033d43b58cd6fe0d8ccd515be741bf29e9dd91f9cf49
BLAKE2b-256 checksum
How to use checksums
cb6a2734a9187777acf9f6c99de0415dcf37c9d01f7beb6e483d7afc0868c7e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 25, 2026.

Transparency log

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

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

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 25, 2026.

Transparency log

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

Download URL respan_gateway-0.3.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
6bd1d97058090698ab302993d99228652dd6d6c56a3bb449b664c139da8c95ec
BLAKE2b-256 checksum
How to use checksums
25bc16ac0fe32ef076657579bbc787f6222bc5427d484e9e8c94c6d22922c7d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 25, 2026.

Transparency log

Security for respan-gateway 0.3.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.3.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 25, 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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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