How to Call Models via an OpenAI-Compatible API
2026-11-11
TLDR
To call a model through an OpenAI-compatible API, configure the provider's API key, base URL, and exact model ID, then send a chat-completions request. This guide walks through Python and cURL examples, streaming, and error diagnosis, with separate endpoint guidance for image, video, and speech generation.
To call models through an OpenAI-compatible API, give the OpenAI client your provider's API key and base URL, then send a chat-completions request with the exact model ID. You can also send the same request directly with cURL: Bearer authentication, a JSON message list, and the model identifier are the essentials.
Start with one short text request. Once it returns an answer, add streaming or application-specific options. That sequence makes configuration errors easier to isolate than starting with tools, images, and a long conversation together.
1. Get the three connection settings right
An OpenAI-compatible API exposes familiar request and response structures through a provider's own service. Using the OpenAI SDK does not require sending your request to OpenAI; the configured base URL determines its destination. With AIAdapter, you can reuse that client and account key across supported chat models, keeping the connection configuration separate from the model choice.
For AIAdapter, the API documentation specifies:
| Setting | What to use |
|---|---|
| API key | Your AIAdapter account key |
| Base URL | https://api.aiadapter.com/v1 |
| Model ID | The full marketplace identifier, including its vendor prefix |
The SDK appends /chat/completions to the base URL. Keep /v1, and do not put the full endpoint into base_url.
Select a chat model from the model marketplace. The examples below use the listed Qwen3.5-Plus model, identified as aliyun-bailian/qwen3.5-plus. Copy the identifier exactly rather than entering a family name such as Qwen.
Create your key in Console → API Keys. Store it server-side in an environment variable or secrets manager, not in frontend JavaScript. An AIAdapter key authenticates with AIAdapter; an OpenAI account key is not a substitute.
2. Prepare Python and your environment
Use Python 3.10 or higher for the current OpenAI Python SDK. Install it with the interpreter that will run your script:
python -m pip install --upgrade openaiSet these environment variables in your terminal, replacing the key placeholder with your account key.
macOS, Linux, or Bash:
export AIADAPTER_API_KEY="your-aiadapter-api-key"
export AIADAPTER_MODEL_ID="aliyun-bailian/qwen3.5-plus"Windows PowerShell:
$env:AIADAPTER_API_KEY = "your-aiadapter-api-key"
$env:AIADAPTER_MODEL_ID = "aliyun-bailian/qwen3.5-plus"These settings apply to that terminal session. Launch Python from the same terminal so it receives them. In a deployed application, configure the variables through your hosting environment's secret settings. Avoid entering keys on shared machines where shell history may expose them.
3. Send your first chat request
Save this example as call_model.py:
import os
from openai import OpenAI
with OpenAI(
api_key=os.environ["AIADAPTER_API_KEY"],
base_url="https://api.aiadapter.com/v1",
timeout=60.0,
max_retries=0,
) as client:
completion = client.chat.completions.create(
model=os.environ["AIADAPTER_MODEL_ID"],
messages=[
{"role": "user", "content": "Explain an API in two sentences."}
],
)
print(completion.choices[0].message.content)Run it with python call_model.py.
The SDK builds the authorization header and serializes the request. messages supplies the conversation; model chooses the model. A normal text completion places the answer in choices[0].message.content, which the script prints.
This first request omits optional sampling, reasoning, and output-limit fields. Add those according to the selected model's documentation after the basic call works. Some models use max_tokens, while others use max_completion_tokens or different controls.
max_retries=0 disables SDK retries while you diagnose the first call. timeout=60.0 sets a 60-second timeout for network operations; it is not a total execution deadline. For production, choose timeouts and a bounded retry policy that fit your application.
4. Make the equivalent request with cURL
Use this Bash command to test the HTTP request without the Python SDK:
curl https://api.aiadapter.com/v1/chat/completions \
-H "Authorization: Bearer $AIADAPTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "aliyun-bailian/qwen3.5-plus",
"messages": [
{"role": "user", "content": "Explain an API in two sentences."}
]
}'Here you supply the full endpoint because cURL does not append SDK routes. The -d option sends a POST request. Read the returned JSON, including the error message if the request fails.
If cURL works but Python fails, compare the environment variables, installed SDK, and actual client configuration. If both fail, investigate the key, endpoint, model ID, and account balance before changing your prompt.
5. Stream the answer as it arrives
For a chat model that supports streaming, set stream=True and read text from each chunk's delta.content. Save this as a separate script using the same environment variables:
import os
from openai import OpenAI
with OpenAI(
api_key=os.environ["AIADAPTER_API_KEY"],
base_url="https://api.aiadapter.com/v1",
timeout=60.0,
max_retries=0,
) as client:
with client.chat.completions.create(
model=os.environ["AIADAPTER_MODEL_ID"],
messages=[{"role": "user", "content": "Explain an API in two sentences."}],
stream=True,
) as stream:
for chunk in stream:
if chunk.choices:
text = chunk.choices[0].delta.content
if text:
print(text, end="", flush=True)
print()AIAdapter documents Server-Sent Events for streamed chat responses. The checks skip chunks without a text delta, including control or usage-only chunks. Streaming delivers output incrementally; it does not change the model's input requirements.
6. Diagnose errors before adding features
Use the HTTP status and returned error message together:
| Symptom | First action |
|---|---|
401 authentication error | Check that the key belongs to this provider and reaches the process or Bearer header. |
404 or model-not-found error | Check the /v1 path, endpoint, and exact vendor-prefixed model ID. |
400 or 422 request error | Reduce the request to model and messages, then verify each optional field. |
429 rate-limit error | Lower concurrency and retry with bounded backoff. |
| Insufficient balance or quota | Check the console and resolve the billing or quota issue before retrying. |
| Connection failure or timeout | Check DNS, network access, and client timeouts; inspect server-side processing before resubmitting. |
In Python, catch openai.APIStatusError to inspect status_code and response. Catch openai.APIConnectionError for connection failures. An error delivered inside a stream can instead raise openai.APIError, so handle failures during iteration as well as when creating the request. Keep credentials and sensitive prompts out of diagnostic logs.
Frequently asked questions
Does OpenAI-compatible mean every OpenAI endpoint works?
No. Compatibility applies to the endpoints a provider implements. This tutorial uses Chat Completions because AIAdapter documents that route. An SDK may also expose Responses, file uploads, assistants, or other resources, but their presence in the library does not establish provider support. Match your SDK method to the provider's documented operation. For advanced chat features, check the chosen model's accepted tool, reasoning, and structured-output fields before adding them to a working request.
Can I change models without changing this Python client?
For another model using the same chat-completions endpoint and authentication, you can keep the client and change the exact model ID. The script reads that ID from AIADAPTER_MODEL_ID, so you can select a different chat model through configuration. Then check its supported parameters, context limits, and response handling. Start with the same short message to verify the new selection before restoring your application's tools, formatting constraints, or longer conversation history.
Can this chat example generate images or videos?
Use the operation for the output you need. AIAdapter documents /v1/images/generations for image generation, /v1/video/generations for video jobs, and /v1/audio/speech for speech synthesis. Video generation requires submission followed by polling /v1/video/generations/{id}. A vision-language model that understands an image is different from an image-generation model. Keep the relevant model's request fields and output handling with each operation rather than inserting a video ID into the chat example.
How do retries affect a failed or timed-out call?
The OpenAI Python SDK retries certain connection and HTTP errors twice by default. This guide disables that behavior for the initial test so failures are easier to trace. A timeout does not establish that the server performed no work, so repeated generation requests can create duplicate work. Use a bounded retry policy, inspect available request or job status, and account for additional attempts. If a stream breaks after delivering text, handle the partial output explicitly.
How do I check the cost of my first request?
Review the selected model's current rate and billing unit in the model pricing directory and on its detail page before calling it. For token-billed chat, input and output usage both matter; optional fields and longer responses can change consumption. Inspect returned usage when available and compare it with the console's billing record. Image, video, and speech operations can use different billing units, so do not estimate their cost from a chat token rate.
Begin with one short request, confirm the returned answer, then build on that working configuration. If you need a key, create an account and set up your first server-side integration.
FAQ
How to Call Models via an OpenAI-Compatible API
To call a model through an OpenAI-compatible API, configure the provider's API key, base URL, and exact model ID, then send a chat-completions request. This guide walks through Python and cURL examples, streaming, and error diagnosis, with separate endpoint guidance for image, video, and speech generation.