Integration guide
OpenAI Python SDK Integration Guide
Use the OpenAI Python SDK with ZHUAN: keep the familiar message structure, configure the ZHUAN base_url, and supply a ZHUAN API key and exact model ID.
Install and prepare
Top up, generate an effective key and save it in ZHUAN_API_KEY. Install the OpenAI Python SDK. Existing projects can keep their dependency version; test compatibility in development before upgrading.
python -m pip install openai
A minimal text request
Use https://key.zhuan.xyz/v1 as base_url; do not append /chat/completions again. This GPT example uses a complete catalog ID. When changing models, first check the current catalog and the API supported by the model.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["ZHUAN_API_KEY"],
base_url="https://key.zhuan.xyz/v1",
timeout=60.0,
max_retries=0,
)
response = client.chat.completions.create(
model="gpt-6.1-sol",
messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content)
Stream the response
Reuse the client above, set stream=True and read each delta. Some events contain no text, so check choices and content. Keep reading the stream; disconnecting does not guarantee that usage already incurred will be free.
with client.chat.completions.create(
model="gpt-6.1-sol",
messages=[{"role": "user", "content": "Hello"}],
stream=True,
) as stream:
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)
Migration and error handling
- Check the endpoint, key and model ID together. A manufacturer's account key does not replace a ZHUAN key.
- Chat Completions, Responses and model-specific parameters differ. Validate the exact API rather than assuming all models accept identical parameters.
- Automatic retries are disabled here to simplify initial debugging. Production code can use bounded retries and backoff; a timed-out request may already have incurred usage.