Skip to main content

OpenAI SDKs (Python & TypeScript)

The official openai SDKs for Python and TypeScript work with GoRoute AI Gateway by overriding base_url / baseURL to http://localhost:8080/v1 and passing a GoRoute Virtual Key (sk-gw-...) as api_key / apiKey.


1. Client Initialization​

from openai import OpenAI

client = OpenAI(
base_url="http://localhost:8080/v1",
api_key="sk-gw-your-virtual-key",
)

2. Chat Completions (POST /v1/chat/completions)​

response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain circuit breakers in one sentence."},
],
)
print(response.choices[0].message.content)

3. Streaming (stream: true)​

stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Count from 1 to 5."}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)

4. Embeddings (POST /v1/embeddings)​

emb = client.embeddings.create(
model="text-embedding-3-small",
input="GoRoute AI Gateway",
)
print(len(emb.data[0].embedding))

5. Responses API (POST /v1/responses & /openai/v1/responses)​

GoRoute implements the stateful/agentic Responses API at both /v1/responses and /openai/v1/responses:

resp = client.responses.create(
model="gpt-4.1-mini",
input="Summarize the benefits of micro-dollar accounting.",
)
print(resp.output_text)

6. Realtime WebSocket (GET /v1/realtime & /openai/v1/realtime)​

For bidirectional audio/text streaming over WebSockets, connect to ws://localhost:8080/v1/realtime?model=gpt-4o-realtime-preview (or wss://... in production) with Authorization: Bearer sk-gw-your-virtual-key:

import WebSocket from "ws";

const ws = new WebSocket(
"ws://localhost:8080/v1/realtime?model=gpt-4o-realtime-preview",
{
headers: {
Authorization: "Bearer sk-gw-your-virtual-key",
},
}
);

ws.on("open", () => {
ws.send(JSON.stringify({ type: "response.create" }));
});

See Realtime WebSocket API and Gateway API Reference for the full list of supported OpenAI-compatible endpoints.