The Qwen models on Model Studio support OpenAI compatible interfaces. You can migrate your existing OpenAI code to Model Studio by changing only the API key, base URL, and model name.
Compatibility information
BASE_URL
The BASE_URL is the network endpoint for accessing the model service. When you use the OpenAI compatible interface with Model Studio, configure the BASE_URL as follows.
When you call via the OpenAI SDK or other OpenAI compatible SDKs, use the following BASE_URL:
Troubleshoot failed calls: If a call through the OpenAI compatible interface fails with a 404, 401, 403, or connection error, check the following configurations:
Cross-region calls
A Model Studio API key is bound to the region in which it was created. When you call the base URL of a region, you must use an API key that was created in that same region. An API key from another region is rejected with an authentication error.
This rule applies to every region that provides an endpoint, including China (Beijing), US (Virginia), Singapore, and Japan (Tokyo), as well as China (Hong Kong). Create the API key in the console of the region whose endpoint you call.
For example, if you use an API key created in the China (Beijing) region to call the US (Virginia) endpoint, the request returns HTTP 401 with the error message Incorrect API key provided and the error code invalid_api_key. This error indicates that the API key and the endpoint belong to different regions, not that the API key is invalid or lacks permissions.
Supported models
Supported models: Qwen large language models (commercial and open-source editions), Qwen-VL, Qwen-Coder, Qwen-Omni, Qwen-Math, DeepSeek, Kimi, GLM, MiniMax.
Qwen-Audio does not support the OpenAI compatible protocol. Use the DashScope protocol instead.
Call via OpenAI SDK
Prerequisites
- Python is installed on your machine.
- The latest version of the OpenAI SDK is installed.
- You have activated Model Studio and obtained an API key. For instructions, see Get API key.
- (Recommended) Configure the API key as an environment variable to reduce the risk of key exposure. You can also configure it directly in code, but this increases the risk of exposure.
- Select the model you want to use from the supported models list.
Usage
The following examples show how to use the OpenAI SDK to access Qwen models on Model Studio.
Non-streaming example
Streaming example
Tool calling example
The following example demonstrates tool calling (function call) through the OpenAI compatible interface, using a weather query tool and a time query tool. The example code supports multi-turn tool calling.
Request parameters
The request parameters are aligned with the OpenAI interface. The following table describes the currently supported parameters:
Parameter | Type | Default | Description |
|---|---|---|---|
model | string | - | The model to use. For available models, see Supported models. |
messages | array | - | The conversation history between the user and the model. Each array element has the format |
top_p (optional) | float | - | The nucleus sampling probability threshold. For example, a value of 0.8 keeps only the smallest set of tokens whose cumulative probability is at least 0.8. Valid values: (0, 1.0). Higher values increase randomness; lower values increase determinism. |
temperature (optional) | float | - | Controls the randomness and diversity of model responses. Higher values flatten the probability distribution, selecting more low-probability tokens for more diverse output. Lower values sharpen the distribution, favoring high-probability tokens for more deterministic output. Valid values: [0, 2). A value of 0 is not recommended. |
presence_penalty (optional) | float | - | Controls repetition across the entire generated sequence. Higher values reduce repetition. Valid values: [-2.0, 2.0]. Supported only on Qwen commercial models and open-source models qwen1.5 and later. |
n (optional) | integer | 1 | The number of responses to generate. Valid values: |
max_tokens (optional) | integer | - | The maximum number of tokens the model can generate. For example, if the model supports up to 2k output tokens, you can set this to 1k to prevent overly long responses. Different models have different output limits. See the model list for details. |
seed (optional) | integer | - | The random seed for generation, used to control randomness of model output. Supports unsigned 64-bit integers. |
stream (optional) | boolean | False | Controls whether to use streaming output. When streaming is enabled, the interface returns a generator. Iterate over it to get results, where each output is the incremental sequence generated. |
stop (optional) | string or array | None | Controls precise stopping of content generation. Generation stops automatically when the model is about to produce the specified string or token_id. Can be a string or array type. When string type: generation stops when the model is about to produce the specified stop word. When array type: array elements can be token_ids, strings, or arrays of token_ids. Generation stops when the generated token or its token_id matches an element in stop. When stop is array type, you cannot mix token_ids and strings as elements. |
tools (optional) | array | None | The tool library available for the model to call. During a function call flow, the model selects one tool from this library. Each tool has the following structure: The tools parameter cannot be used with stream=True simultaneously. |
stream_options (optional) | object | None | Configures whether to display token usage in streaming output. Only takes effect when stream is True. To count tokens in streaming mode, set |
Response parameters
Parameter | Type | Description | Notes |
|---|---|---|---|
id | string | The system-generated ID for this request. | - |
model | string | The model name used for this request. | - |
system_fingerprint | string | The configuration version used by the model runtime. Currently not supported; returns an empty string. | - |
choices | array | The details of the model-generated content. | - |
choices[i].finish_reason | string | The reason generation stopped. Values: null (still generating), stop (stopped due to a stop condition), length (stopped due to exceeding max length). | - |
choices[i].message | object | The message output by the model. | - |
choices[i].message.role | string | The model role. Fixed value: assistant. | - |
choices[i].message.content | string | The text generated by the model. | - |
choices[i].index | integer | The sequence number of the generated result. Default: 0. | - |
created | integer | The timestamp (in seconds) of the generated result. | - |
usage | object | Metering information indicating the token consumption for this request. | - |
usage.prompt_tokens | integer | The token count of the user input text. | - |
usage.completion_tokens | integer | The token count of the model-generated response. | - |
usage.total_tokens | integer | The sum of usage.prompt_tokens and usage.completion_tokens. | - |
Call via langchain_openai SDK
Prerequisites
- Python is installed on your machine.
- The langchain_openai SDK is installed.
- You have activated Model Studio and obtained an API key. For instructions, see Get API key.
- (Recommended) Configure the API key as an environment variable to reduce the risk of key exposure. You can also configure it directly in code, but this increases the risk of exposure.
- Select the model you want to use from the supported models list.
Usage
The following examples show how to use the langchain_openai SDK to access Qwen models on Model Studio.
Non-streaming output
Non-streaming output uses the invoke method:
Streaming output
Streaming output uses the stream method. You do not need to configure a stream parameter separately.
Call via HTTP
You can call Model Studio through HTTP requests and receive responses in the same structure as OpenAI HTTP responses.
Prerequisites
- You have activated Model Studio and obtained an API key. For instructions, see Get API key.
- (Recommended) Configure the API key as an environment variable to reduce the risk of key exposure. You can also configure it directly in code, but this increases the risk of exposure.
Endpoint
Request examples
The following examples use cURL commands to call the API.
$DASHSCOPE_API_KEY with your actual API key.Non-streaming output
Streaming output
To use streaming output, set the stream parameter to true in the request body.
Error response
When a request fails, the response includes code and message fields indicating the cause:
Configure a third-party client
You can call Model Studio models from any third-party client that supports the OpenAI compatible protocol. The following steps use the Zhipu client as an example:
- In the provider settings of the client, select Custom provider.
-
Base URL: Enter the base URL that the OpenAI SDK uses for your region. For the base URL of each region, see BASE_URL. The base URL ends with
/compatible-mode/v1and does not include/chat/completions. Because base URLs differ by region, use the one for the region of your API key. For example, for the Singapore region, enterhttps://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1. Replace{WorkspaceId}with your workspace ID, which you can find on the workspace details page in the Model Studio console. The legacyhttps://dashscope.aliyuncs.comdomain remains available, but use the workspace-specific domain whenever possible. - API Key: Enter the Model Studio API key for the region that the base URL points to. You can create and obtain an API key on the API Key management page of the Model Studio console.
-
Model name: Enter the name of a large language model that supports the OpenAI compatible protocol. For the models that you can choose from, see Supported models. For example,
qwen3-vl-32b-thinking. This model name is an example only and does not indicate that the model provides a free quota. - Save the configuration and start a conversation to verify that the third-party client can call the model.
error.message set to current user api does not support http call and error.type set to invalid_request_error. This error means that the model you entered does not support HTTP calls through the OpenAI compatible interface. Replace it with a model from Supported models and try again. For example, qvq-max does not support this call method.
Error codes
Error code | Description |
|---|---|
400 - Invalid Request Error | The request is invalid. See the error message for details. |
401 - Incorrect API key provided | The API key is incorrect. |
429 - Rate limit reached for requests | QPS or QPM limit exceeded. |
429 - You exceeded your current quota, please check your plan and billing details | Quota exceeded or account in arrears. |
500 - The server had an error while processing your request | Server error. |
503 - The engine is currently overloaded, please try again later | Server overloaded. Retry later. |