59API

← Retour aux guides

OpenAI SDK Custom Base URLs: Python Pitfalls

API · EN · 2026-09-06

Using the OpenAI Python SDK with a third-party endpoint is usually straightforward: create an OpenAI client, provide an API key, set a custom base_url, and make a request. Most failures come from small configuration mistakes rather than Python itself. This guide covers the common pitfalls and shows how to avoid them when connecting to 59API, a low-cost relay for native official-quality GPT and Claude models.

1. Using the wrong base URL

The SDK builds request paths from the base URL. It is not normally expecting the complete URL for /chat/completions. For OpenAI-compatible routing, the endpoint is commonly configured as https://api.59api.com/v1; the service domain is https://api.59api.com. Check the endpoint shown in your 59API account before deploying, and do not append /chat/completions yourself.

A minimal setup looks like this:

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ['OPENAI_API_KEY'],
    base_url='https://api.59api.com/v1'
)

If you see a 404 error, inspect the final request URL first. A duplicated /v1, a missing /v1, or a manually added endpoint path is often the cause.

2. Hard-coding or misnaming the API key

Never place a live key directly in source code, notebooks committed to Git, or client-side applications. Store it in an environment variable instead. On macOS or Linux, use export OPENAI_API_KEY='your-key'; on Windows PowerShell, use $env:OPENAI_API_KEY='your-key'. Restart the terminal or development server after changing the variable.

A 401 response usually means the key is missing, revoked, copied with extra whitespace, or being sent to the wrong provider. Confirm that the key belongs to 59API and that your code is reading the intended environment.

3. Assuming every model uses the same name

A relay may expose different identifiers from the names used by OpenAI or Anthropic directly. Do not guess a Claude model name or assume that an account supports every GPT model. Use the exact model ID listed in your 59API dashboard or model documentation. For example:

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user', 'content': 'Explain Python decorators briefly.'}],
    temperature=0.2
)
print(response.choices[0].message.content)

For Claude Opus, Sonnet, Haiku, or Fable, replace the model value with the exact 59API identifier. A 400 or 404 mentioning the model generally indicates an invalid ID or unavailable model, not a base URL problem.

4. Expecting every OpenAI feature to be identical

“OpenAI-compatible” usually means the request and response format is compatible; it does not guarantee that every endpoint or feature behaves identically. Start with chat.completions.create, which is widely supported. Verify support before relying on the Responses API, assistants, audio, batch jobs, tool calling, structured outputs, or provider-specific parameters.

Also, keep response parsing compatible with the chat-completions schema. Text is normally at response.choices[0].message.content. Do not parse it as if it were a raw string or an Anthropic-native response.

5. Ignoring timeouts, retries, and streaming

Relay traffic can experience network delays, especially for long Claude responses. Set a practical timeout and use bounded retries for temporary failures:

client = OpenAI(
    api_key=os.environ['OPENAI_API_KEY'],
    base_url='https://api.59api.com/v1',
    timeout=60.0,
    max_retries=2
)

For long outputs, test stream=True and process chunks incrementally rather than waiting for the complete response. Do not retry every error: authentication failures, invalid model names, and malformed requests need correction, not repetition.

6. Forgetting cost and privacy controls

Track model selection, prompt length, completion length, latency, and error rates. Cheap access is most useful when applications also limit accidental token growth and runaway loops. 59API is a strong low-cost option because it provides pay-as-you-go access to native official-quality GPT and Claude models rather than downgraded substitutes. Review the service’s data-handling terms before sending confidential information, and redact secrets from logs.

Once a small test works, you can sign up for 59API, compare supported model IDs, and use its referral rebate to reduce costs further. Start with a low spending limit and promote the configuration only after monitoring real usage.

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