Call Qwen on Alibaba Cloud Model Studio in OpenAI-compatible mode
Use the OpenAI Python SDK with a Model Studio workspace base URL and DASHSCOPE_API_KEY to call qwen3.8-max, including streaming with usage, following Alibaba Cloud's OpenAI-compatible Chat documentation.
Verification
- Test environment
- Unknown
- Tested on
- Unknown
- Last updated
- 2026-11-11
Related models: Qwen3.8-Max
Qwen models on Alibaba Cloud Model Studio accept OpenAI-compatible requests: according to Alibaba Cloud, you migrate existing OpenAI code by changing only the API key, the base URL and the model name. This guide follows the official OpenAI compatible - Chat page. The code is taken from that page with its inline comments removed; we have not run it ourselves.
1. Prerequisites
- Python and the latest OpenAI SDK:
pip install -U openai - Model Studio activated and an API key created. Alibaba Cloud recommends putting the key in the
DASHSCOPE_API_KEYenvironment variable instead of in code.
2. Pick the right base URL
Model Studio now uses workspace-specific domains. Replace {WorkspaceId} with your own workspace ID. For the OpenAI SDK, the documented base URLs include:
- Singapore:
https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1 - Virginia:
https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/compatible-mode/v1 - Hong Kong (China):
https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1 - Japan (Tokyo):
https://{WorkspaceId}.ap-northeast-1.maas.aliyuncs.com/compatible-mode/v1
The old dashscope.aliyuncs.com (Beijing) and dashscope-intl.aliyuncs.com (Singapore) domains should be migrated to the workspace domains; for Beijing the page names https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com. Check the page's BASE_URL table for your region before you deploy.
API keys are bound to a region. A key created in one region gets HTTP 401 invalid_api_key ("Incorrect API key provided") when used against another region's endpoint, even though the key itself is valid.
3. Non-streaming request
from openai import OpenAI
import os
def get_response():
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3.8-max",
messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': '你是谁?'}]
)
print(completion.model_dump_json())
if __name__ == '__main__':
get_response()
The response is a standard chat.completion object with choices and usage (prompt_tokens, completion_tokens, total_tokens).
4. Streaming request
Add stream=True. With stream_options={"include_usage": True} the last chunk carries token usage and an empty choices list:
completion = client.chat.completions.create(
model="qwen3.8-max",
messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': '你是谁?'}],
stream=True,
stream_options={"include_usage": True}
)
for chunk in completion:
print(chunk.model_dump_json())
5. Notes
- The same compatible endpoint also serves Qwen-VL, Qwen-Coder, Qwen-Omni, Qwen-Math and some third-party models (DeepSeek, Kimi, GLM, MiniMax). Qwen-Audio does not support the OpenAI protocol.
- Tool calling uses the usual OpenAI
toolslist; the docs include a full multi-turn example. - If you get 404, 401, 403 or connection errors, check the region of your key and base URL first.
See Qwen3.8-Max for its specs and the Pricing page to compare costs.
Sources
- OpenAI compatible - Chat (Alibaba Cloud Model Studio docs)www
.alibabacloud .com (opens in a new tab) · checked 2026-11-11