Change the base URL in the OpenAI Python SDK

Set base_url on the OpenAI Python client to reach the gateway: the env alternative, the sync and async clients, and a way to confirm the request path.

Checked 2026-11-22

·All guides·Models

The OpenAI Python SDK takes a custom endpoint through a base_url setting on the client, so pointing it at UplinkSwitch is a matter of passing https://api.uplinkswitch.com/v1, a gateway key and an available catalog model, then confirming the client sends the chat request you intended. This page is the Python configuration reference: the explicit client, the environment alternative, the synchronous and asynchronous clients, and how precedence and process state decide which base actually takes effect. Pin the SDK version you build against; option behaviour is stated against a specific release, not in general. Everything below was checked on 2026-11-22 against openai 3.57.0, by sending each client to a local listener and reading the path it received.

The explicit client

Set base_url when you construct the client, and keep /v1 on the end. The OpenAI client appends the resource path to this base, so a base ending in /v1 produces /v1/chat/completions, with the version appearing once.

python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.uplinkswitch.com/v1",
    api_key="sk-uplink-live-...",
)

resp = client.chat.completions.create(
    model="gpt-6.1-sol",
    messages=[{"role": "user", "content": "One sentence on idempotency."}],
)
print(resp.choices[0].message.content)

On the 2026-11-23 catalog gpt-6.1-sol showed ok; swap in any chat id that reads ok there.

The environment alternative and precedence

The SDK also reads a base from the OPENAI_BASE_URL environment variable, so you can set it outside the code. When both are present, an explicit base_url passed to the constructor is the value that governs that client; in 3.57.0 a constructor value beat a conflicting OPENAI_BASE_URL every time. The practical rule: if a client is reaching the wrong host, check whether an environment value and a constructor value disagree, because the one you expect may not be the one winning.

The async client

The asynchronous client takes the same base_url, and it is a separate object from the synchronous one, so configure it in its own right rather than assuming a setting carries across.

python
import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI(base_url="https://api.uplinkswitch.com/v1", api_key="sk-uplink-live-...")

async def main():
    resp = await client.chat.completions.create(
        model="gpt-6.1-sol",
        messages=[{"role": "user", "content": "One sentence on retries."}],
    )
    print(resp.choices[0].message.content)

asyncio.run(main())

In a long-lived process or a notebook, remember that a client captures its configuration when it is created; changing an environment value after the client exists does not move an already-built client. Create the client after the environment is set.

Confirm the destination

Before you trust it, confirm the request path without logging the key. Switch on the SDK's request logging, or route one call to a small local receiver that prints what it got, and look for the POST /v1/chat/completions line; strip the key from any output first. How the path is composed is covered in the base-URL composition guide; a TLS trust-store failure, which looks like a connection problem but is not a base-URL issue, is diagnosed on the SSL certificate fix; and the generic multi-SDK setup is on the SDKs guide.

Questions and answers

Pass base_url="https://api.uplinkswitch.com/v1" when you construct OpenAI or AsyncOpenAI, keeping /v1 on the end. The client appends the resource path, so the request resolves to /v1/chat/completions.

An explicit base_url on the constructor governs that client, and OPENAI_BASE_URL covers clients you did not set explicitly. That order held in 3.57.0; when a client hits the wrong host, a disagreement between the two is the usual reason.

Build AsyncOpenAI with the same base_url and await the call. Confirm it by enabling request logging, which shows the host as well as the path, or by using a local echo endpoint with a placeholder key, and reading the captured POST /v1/chat/completions, with the authorization header redacted.
Join on Telegram
Help