---
title: "OpenAI Python SDK with a custom base_url (BroadNet) | BroadRouter"
description: "Point the official OpenAI Python SDK at BroadNet Inference with one base_url change. Install, set your key, make a first request and stream the answer."
url: https://broadrouter.com/guides/openai-python-sdk/
lang: en
updated: 2026-11-16
publisher: BroadRouter (a BroadNet site); API by BroadNet Inference, https://inference.broadnet.ai
---

# Use the OpenAI Python SDK with BroadNet Inference

Updated 2026-11-16

Install the openai package, then create the client with base\_url set to https://inference.broadnet.ai/v1 and your BroadNet key as api\_key. Every chat.completions call works unchanged; only the model ID changes, for example broadnet/deepseek-v4.1-flash. BroadNet serves chat completions and transcriptions, not the Responses API.

You need a BroadNet Inference account and an inference key. New accounts start with $5 of credit and no card; [create one at inference.broadnet.ai](https://inference.broadnet.ai/?utm_source=broadrouter&utm_medium=referral&utm_campaign=guides-openai-python-sdk&utm_content=body). The console shows a key once, and only its hash is stored, so copy it straight into your environment.

## 1\. Install and set the key

The current `openai` package (3.x) needs Python 3.10 or newer.

```bash
python -m pip install openai
export BROADNET_API_KEY='paste-the-key-once'
```

## 2\. Make a first request

Save this as `first_request.py`:

```python
from openai import OpenAI
import os

client = OpenAI(
    base_url="https://inference.broadnet.ai/v1",
    api_key=os.environ["BROADNET_API_KEY"],
)

response = client.chat.completions.create(
    model="broadnet/gemma-4-26b-a4b",
    messages=[{"role": "user", "content": "Reply with one short sentence."}],
)
print(response.choices[0].message.content)
```

Run it with `python first_request.py`. This example uses `broadnet/gemma-4-26b-a4b` because it works on the free credit. The other chat models are available after a top-up.

## 3\. Switch to the model you want

Change only `model`:

| You want | Model ID |
| --- | --- |
| Unlimited on a flat plan, 1M context | `broadnet/deepseek-v4.1-flash` |
| Tools and structured output, 1M context | `broadnet/glm-5.3` |
| Arabic text and images | `broadnet/qwen3.8-27b` |
| Low price, works on free credit | `broadnet/gemma-4-26b-a4b` |

DeepSeek V4.1 Flash, GLM 5.3 and Qwen3.8 27B reason before they answer, and the reasoning counts toward `max_tokens`. Give them 1,000 tokens or more. [Here is why a small limit returns an empty answer](https://broadrouter.com/guides/reasoning-tokens-empty-response/).

## 4\. Stream the answer

```python
stream = client.chat.completions.create(
    model="broadnet/deepseek-v4.1-flash",
    stream=True,
    stream_options={"include_usage": True},
    messages=[{"role": "user", "content": "Count from one to three."}],
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
    if chunk.usage:
        print("\n", chunk.usage)
```

With `include_usage`, the token counts arrive in the last chunk, which has no `choices`. [The streaming guide](https://broadrouter.com/guides/streaming-chat-completions/) covers dropped connections.

## 5\. Keep the request ID

Every response carries an `X-Request-ID` header. Log it: BroadNet support asks for it. Parsed responses expose it as `response._request_id`. To read it alongside other headers:

```python
raw = client.chat.completions.with_raw_response.create(
    model="broadnet/gemma-4-26b-a4b",
    messages=[{"role": "user", "content": "Hi"}],
)
print(raw.headers.get("x-request-id"))
response = raw.parse()
```

Pay-as-you-go keys allow 30 requests a minute and 4 in flight. [What each error code means and when to retry](https://broadrouter.com/guides/errors-and-retries/).

## Troubleshooting

-   **`AuthenticationError` (401).** The key is wrong, expired, or out of pay-as-you-go allowance. Check that `BROADNET_API_KEY` is set in the process that runs your code.
-   **`NotFoundError` (404).** Check that `base_url` ends in `/v1` and that you call `chat.completions`, not `responses`. Then check the model ID, with the `broadnet/` prefix.
-   **Paid model fails on a new account.** The $5 starting credit covers BroadNet Gemma 4 VL 26B (`broadnet/gemma-4-26b-a4b`) and the two BroadNet Transcribe models. Top up to use the other chat models: DeepSeek V4.1 Flash, GLM 5.3 and Qwen3.8 27B.
-   **Empty `content`.** Reasoning counts toward `max_tokens`. Raise it to 1,000 or more. [Why answers come back empty](https://broadrouter.com/guides/reasoning-tokens-empty-response/).
-   **`RateLimitError` (429).** You went over the per-minute or concurrent limit. The SDK retries twice by default; set `max_retries` on the client to change that.
-   **Custom HTTP client errors after upgrading.** The 3.x SDK uses HTTPX2. If you pass your own `http_client`, follow the SDK’s HTTPX2 migration guide.

## Terms in this guide

[Pay-as-you-go](https://broadrouter.com/glossary/pay-as-you-go/)

Pay-as-you-go is a pricing model in which you pay only for what you use, metered per unit such as tokens, requests or audio minutes, with no subscription or minimum commitment. Most LLM APIs bill this way, from a prepaid balance or a periodic invoice.

[Token](https://broadrouter.com/glossary/token/)

A token is the unit of text a language model reads and writes: a word, part of a word, a punctuation mark or a byte sequence produced by the model's tokenizer. LLM APIs count, limit and usually bill usage in tokens.

[Prepaid credit](https://broadrouter.com/glossary/prepaid-credit/)

Prepaid credit is money added to an API account before use. Each request deducts its cost from the balance, and when the balance runs out, metered requests stop until you top up. It caps spend at what you have paid in.

[API key](https://broadrouter.com/glossary/api-key/)

An API key is a secret string that identifies and authorizes a client calling an API. LLM APIs usually expect it in an Authorization: Bearer header, and anyone who holds the key can make requests that are billed to the account that owns it.

[Concurrency](https://broadrouter.com/glossary/concurrency/)

Concurrency is the number of requests a client has in flight at the same time. LLM APIs often cap it per key or plan, separately from requests per minute, because each open request holds GPU capacity until its response has finished.

[Rate limit](https://broadrouter.com/glossary/rate-limit/)

A rate limit is a cap an API places on how much a client can use it in a time window, such as requests per minute or tokens per minute. Requests over the limit are refused, usually with HTTP 429 Too Many Requests, until usage falls back under it.

[Streaming](https://broadrouter.com/glossary/streaming/)

Streaming is a way of returning a language model response in small chunks while it is still being generated, instead of as one reply at the end. OpenAI-compatible APIs stream over server-sent events when the request sets stream to true.

[Structured output](https://broadrouter.com/glossary/structured-output/)

Structured output is an LLM API feature that constrains a model's reply to a format the developer specifies, usually a JSON Schema, so code can parse it reliably. It is stricter than JSON mode, which guarantees valid JSON but not a particular shape.

## Questions

### Do I need a different SDK?

No. BroadNet Inference speaks the OpenAI chat-completions and audio-transcriptions shapes, so the official openai package works with a different base\_url.

### Can I keep using OPENAI\_API\_KEY?

You can, but a separate BROADNET\_API\_KEY variable avoids sending one provider's key to another by mistake.

### Does the SDK read OPENAI\_BASE\_URL?

Yes. If you don't pass base\_url, the client uses OPENAI\_BASE\_URL. Set it to https://inference.broadnet.ai/v1 and set OPENAI\_API\_KEY to your BroadNet key, and OpenAI() needs no arguments.

### Can I use client.responses.create with BroadNet?

No. The SDK's README leads with the Responses API, but BroadNet's docs list only chat completions and audio transcriptions. Use client.chat.completions.create.

## Related guides

-   [Use the OpenAI Node.js SDK with BroadNet Inference](https://broadrouter.com/guides/openai-node-sdk/)
-   [Stream chat completions and handle dropped connections](https://broadrouter.com/guides/streaming-chat-completions/)
-   [Why a reasoning model returns an empty answer](https://broadrouter.com/guides/reasoning-tokens-empty-response/)
-   [BroadNet Inference error codes and when to retry](https://broadrouter.com/guides/errors-and-retries/)

Try it on your real workload. New accounts start with $5 of credit for Gemma and transcription. DeepSeek, GLM and Qwen3.8 need a top-up.

[Get started](https://inference.broadnet.ai/account/?utm_source=broadrouter&utm_medium=referral&utm_campaign=guides-openai-python-sdk&utm_content=page-cta) ·

**Sources**

-   [BroadNet Inference API reference, first request](https://inference.broadnet.ai/docs?utm_source=broadrouter&utm_medium=referral&utm_campaign=guides-openai-python-sdk&utm_content=sources#base-url)
-   [OpenAI Python library on PyPI (3.x, Python 3.10+, checked 2026-11-14)](https://pypi.org/project/openai/)
