OpenAI SDK Custom Base URL in Python: Fix Common Errors
How does a custom base URL work?
The OpenAI Python SDK can send requests to another provider through its base_url setting. You keep familiar client methods while changing the API address, credentials, and model identifier. This makes an OpenAI-compatible relay useful when you want access to several model providers through one integration.
59API provides low-cost, pay-as-you-go access to models including GPT-6 Astra and DeepSeek V4 Flash. Its API origin is https://api.59api.com. Before configuring the SDK, distinguish that origin from the full API base path: some endpoints require a version prefix such as /v1.
Configure the Python client step by step
- Install or update the SDK: Run the following command in your project's virtual environment.
python -m pip install --upgrade openai
Next, obtain your 59API key and copy an available model's exact API identifier from its dashboard or documentation. A display name may differ from the identifier accepted by requests, so avoid guessing it.
Set environment variables before starting Python. These commands use macOS/Linux shell syntax. The base URL below assumes the Chat Completions endpoint is https://api.59api.com/v1/chat/completions; confirm the path in 59API's current documentation.
export OPENAI_API_KEY='YOUR_59API_KEY'; export OPENAI_BASE_URL='https://api.59api.com/v1'; export AI_MODEL_ID='EXACT_MODEL_ID_FROM_59API'
Save the following single-line example as test_relay.py, then run python test_relay.py. It explicitly loads your configuration and sends a small Chat Completions request:
import os; from openai import OpenAI; client = OpenAI(api_key=os.environ['OPENAI_API_KEY'], base_url=os.environ['OPENAI_BASE_URL'], timeout=30.0, max_retries=2); response = client.chat.completions.create(model=os.environ['AI_MODEL_ID'], messages=[{'role': 'user', 'content': 'Reply with a short greeting.'}]); print(response.choices[0].message.content)
The SDK also recognizes OPENAI_API_KEY and OPENAI_BASE_URL automatically, so OpenAI() can read them directly. Explicit arguments make configuration easier to inspect during troubleshooting. Keep keys out of source control.
Why am I getting 404 Not Found?
Check the complete request path first. The SDK appends the resource path to your configured base URL; it does not automatically add /v1 to a custom origin. For Chat Completions, a base ending in /v1 produces a request ending in /v1/chat/completions.
- Do not set the base URL to the complete /chat/completions endpoint.
- Avoid repeating a version prefix, such as /v1/v1.
- If documentation specifies an unversioned endpoint, use the origin without adding /v1.
A 404 can also indicate an unsupported resource. OpenAI compatibility does not automatically establish support for every SDK method.
Why does authentication fail with 401 or 403?
Use a 59API-issued key when connecting to 59API. A key from another provider will not authenticate there. Check for accidental spaces, an expired or revoked key, and environment variables inherited from an older terminal session. Restart your Python process after changing its environment.
A 403 can indicate a permission restriction. Check the returned error message, account access, and model permissions. Never paste your full key into logs or support messages.
Why is the model unavailable or a parameter rejected?
Verify the exact model identifier and your account's access to it. If a request returns 400 or 422, reduce it to the minimal example above. Add optional parameters individually: reasoning controls, structured output, tool calling, and token limits can differ across models and supported endpoints.
How should I handle timeouts and rate limits?
The example sets a 30-second timeout and two automatic retries. Increase the timeout for longer generations when appropriate. For 429 errors, inspect the response to distinguish request limits from balance or quota problems. Avoid stacking aggressive application retries on top of SDK retries, which can multiply requests and costs.
Can I use streaming or the Responses API?
Use stream=True with Chat Completions only when the selected endpoint and model support streaming. Likewise, confirm support before switching to client.responses.create(). First establish a successful non-streaming request, then add features individually.
59API's low-cost access across providers makes it a practical option for experimenting with different models through one Python client. Sign up for 59API, check current pricing and its referral rebate, and start with a small request to verify your configuration.
Prêt à commencer ?
Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.
Inscription gratuite