Appearance
Call Qwen models via OpenAI API
Reference, synced 2026-06-13.
flowchart TD n0["Models"] n1["Overview"] n2["Products"] n3["Solutions"] n4["Pricing"] n5["Resources"] n6["Partners"] n7["Support"] n8["Language"] n0 --> n1 n1 --> n2 n2 --> n3 n3 --> n4 n4 --> n5 n5 --> n6 n6 --> n7 n7 --> n8
Free access Accelerate Delivery with Fixed-Cost Agentic CodingWatch how it works
Models
Empowering AI innovation for both enterprises and developers with Alibaba Cloud’s best-in-class Qwen models, AI-native apps, and AI solutions.
Alibaba Cloud Model Studio \ Enterprise-grade large model service and application development platform.
Try Visual Model \ Supports image understanding, image generation, and video generation.
Models
HappyHorse-1.0-T2V \ Cinematic creative generation, ultimate dynamic details Qwen3-VL-Plus \ Native VL, spatial reasoning, 1M-context video analysis Wan2.7-VideoEdit \ Supports both localized and global editing with prompt
Qwen3.6-Plus \ Native multimodal, 1M context, agentic coding Wan2.7-Image-Pro \ Interactive editing, long-text rendering, precise prompt following Qwen-Plus \ Balanced intelligence, efficient inference, production-ready performance
Qwen-Image-2.0 \ Professional infographics, exquisite photorealism Z-Image-Turbo \ Ultra-fast image generation, high throughput, cost-optimized inference Qwen3-Coder-Next \ Multi-turn tool interactions, future-ready development support
Wan2.7-T2V \ High-fidelity T2V, 15s duration, advanced camera control Wan2.7-I2V \ Cinematic I2V with emotional depth and visceral impact Wan2.7-R2V \ Up to 5 mixed image/video inputs and audio timbre cloning
GenAI Application
Qoder \ Intelligent coding assistant, available for enterprise-dedicated deployment. Qoder CN \ AI-powered coding assistant that boosts developer productivity with intelligent code completion, AI chat, multi-file editing, and task automation.
AI Service
Model Experience \ Experience full-scale, multimodal model capabilities online. Platform for AI \ An AI-native algorithm engineering platform for end-to-end modeling, training, and inference service deployment. Fine-tune Video Generation Model \ Customize Wan’s text-to-video capabilities through model fine-tuning to meet your unique requirements.
AI Use Case
AI Savings Plan Hot \ Save up to 47% on AI costs. Limited-time offer tailored to your usage. AI Video Creation \ Elevate your professional video production with Wan 2.6.
AI Token Plan \ One plan. Multiple models. Big Savings with a Fixed Subscription. AI Image Creation \ All-in-one creative suite for copywriting, image generation, and poster design.
Overview
As a global full-stack AI leader, Alibaba Cloud aims to make computing accessible to everyone and help worldwide customers accelerate innovation.
Why Alibaba Cloud
About Alibaba Cloud \ AI Powered Cloud Technology Our Global Network \ Explore our global presence and deployment regions around the world Our Global Offices \ With offices in 4 continents, we're always close to where it matters.
Asia Accelerator \ Accelerate Success in Asia with Alibaba Cloud Go Global \ Benefits of our Global Alliance Trust Center \ Empowering enterprises with a secure, compliant, and globally trusted cloud infrastructure
Customers and Insights
Olympic Games \ Alibaba Cloud Powers Olympic Games with AI-powered cloud technology Case Studies \ Learn how customers are scaling their businesses on Alibaba Cloud Analyst Reports \ Learn what the top industry analyst firms are saying about Alibaba Cloud
What's New
Events and Webinars \ Quick access to upcoming and on-demand events Product Updates \ Stay informed of the latest innovations Press Room \ Latest news and media releases
Products
Featured ProductsAI & Machine Learning Computing Container Storage Networking & CDN Security Middleware Database Analytics ComputingMedia ServicesEnterprise Services & Cloud CommunicationDomain Names and WebsitesEnd User ComputingServerlessDeveloper ToolsMigration & O&M ManagementApsara Stack
Featured Products
Alibaba Cloud Model Studio \ Supercharge your AI journey effortlessly with industry-leading GenAI models ApsaraDB RDS \ Store and manage your business data, with automated monitoring and backups Certificate Management Service (Original SSL Certificate) \ Create a safe and secure connection between your website and users
Elastic Compute Service (ECS) \ Host websites anywhere and scale enterprise workloads Container Service for Kubernetes (ACK) \ Run and scale containerized applications on managed Kubernetes infrastructure Object Storage Service (OSS) \ Store large amounts of data in the cloud and access it anywhere, anytime
Simple Application Server (SAS) \ All-in-one services for fast deployment Elastic IP Address (EIP) \ Manage your public IPs independently to improve internet network quality Domain Names and Website \ Get the perfect domain name to suit your every need
Related Programs
Solutions
Solutions by Industry Technical Solutions AI WebsitesNetworking Security and ComplianceData and AnalyticsEnterprise Service and ApplicationCloud MigrationCloud NativeHybrid CloudSMB solutions
Financial Services \ Innovate faster with Alibaba Cloud Games \ Grow your game rapidly with high global availability
New Retail \ Alibaba Cloud enables digital retail transformation to fuel growth and realize an omnichannel customer experience throughout the consumer journey. Media and Entertainment \ Ready your content for today's media market with a digitalized media journey
Supply Chain \ Power your supply chain with intelligent, efficient, and reliable solutions Sports \ Digitizing the sports industry with intelligent tech
Sustainability \ Achieve a sustainable future with low-carbon and energy-efficient technologies
Pricing
Flexible options like pay-as-you-go and clear billing rules to meet diverse business needs.
Overview & Tools
Pricing Calculator \ Get an instant pricing estimate based on your usage and needs Free Trial \ Try our 80+ cloud products for free.
Pricing Options \ Get the most out of Alibaba Cloud with flexible pricing
Optimize your cost
Migrate & Save \ Superior Performance At Lower Pricing. Save up to 50%. Promotion Center \ Unlock the latest Alibaba Cloud offers & promos
Resources
Official documentation, extensive tools, training resources, and a community to grow and innovate in the cloud.
Technical Resources
Documentation \ Product guides and FAQs Architecture Center \ Design reliable, secure, and efficient cloud architecture. Intelligent Solution Explorer \ Find the right solution for you, powered by AI
Blog \ Latest cloud insights and developer trends Whitepapers \ Research that explores the how and why behind our technology
Training&Certification
Alibaba Cloud Academy \ Build cloud skills and earn certifications with expert-led training.
Developer Hub
Alibaba Cloud Project Hub \ Explore real-world projects built by developers using our platform. Our Developer MVPs \ Celebrating the developers who lead, build, and inspire our community
Partners
Partner-first strategy offering collaborative product, sales, and service models, plus high-quality partner solutions that complement Alibaba Cloud’s capabilities.
Marketplace
AI Alliance for ISVs \ Partner with us to build and grow AI solutions together ISV Benefits \ Unlock resources, market access, and go-to-market support as an ISV partner
Alibaba Cloud Marketplace \ Explore ready-to-deploy solutions from our partners and ISVs
Find a Partner
Partner Hub \ Find your ideal partner in no time
Become a Partner
Partner Network \ A partner portal for Alibaba Cloud Channel, Technology, MSP partner and other partner programs
Support
Full-lifecycle support and expert services, from cloud advisory and migration to operations.
Support & Professional Services
Professional Services \ Expert-led services to design, migrate, and optimize your cloud journey Support Plans \ Flexible support for every stage — from startup to enterprise
Partner Support Program \ Priority technical support for partners, with dedicated managers and faster issue resolution
Contact us
Connect With Us \
Talk to a sales expert and get a custom quote for your business
Language
- English
- 简体中文
- 繁體中文
- 日本語
- Bahasa Indonesia
Locale
Visit aliyun.com
Documentation
Alibaba Cloud Model Studio
User Guide (Models) User Guide (Application) API Reference (Models) API Reference (Application)
Search for Help Content
Getting Started
The Beginner's Guide
Well-Architected Framework
AI & Machine Learning
Platform For AI
Alibaba Cloud Model Studio
DashVector
Artificial Intelligence Recommendation
OpenSearch
Image Search
Machine Translation
Intelligent Speech Interaction
Optimization Solver
Intelligent Computing LINGJUN
Computing
Elastic Compute Service
Elastic GPU Service
Elastic Container Instance
Dedicated Host
Compute Nest
Simple Application Server
Cloud Box
Auto Scaling
Elastic High Performance Computing
Batch Compute (Deprecated)
Function Compute
Serverless App Engine
ENS
Elastic Desktop Service
App Streaming
WUYING Terminal
Cloud Phone
Edge Network Acceleration
Alibaba Cloud Linux
AgentBay
Container
Container Service for Kubernetes
Container Compute Service
Container Registry
Storage
Object Storage Service
Cloud Parallel File Storage
File Storage NAS
Tablestore
Storage Capacity Unit
Simple Log Service
Cloud Backup
Intelligent Media Management
Drive and Photo Service
Data Transport
Cloud Storage Gateway
Data Online Migration
Hybrid Cloud Storage Array
Storage Services Overview
Backup and Disaster Recovery Center
Networking and CDN
Server Load Balancer
Elastic IP Address
Internet Shared Bandwidth
Data Transfer Plan
Virtual Private Cloud
NAT Gateway
PrivateLink
Alibaba Cloud DNS PrivateZone
Network Intelligence Service
Cloud Data Transfer
IPv6 Gateway
Anycast Elastic IP Address
Cloud Enterprise Network
Global Accelerator
VPN Gateway
Smart Access Gateway
Express Connect
CDN
Edge Security Acceleration
Cloud Network Well-architected Design Guidelines
Security
Anti-DDoS
Web Application Firewall
Cloud Firewall
Security Center
Bastionhost
Secure Access Service Edge
Certificate Management Service
Key Management Service
Data Security Center
Identity as a Service
Fraud Detection
AI Guardrails
Captcha
Blockchain as a Service
ID Verification
Managed Security Service
Middleware
Enterprise Distributed Application Service
Microservices Engine
Alibaba Cloud Service Mesh
SchedulerX
ApsaraMQ for RocketMQ
ApsaraMQ for Kafka
ApsaraMQ for RabbitMQ
ApsaraMQ for MQTT
Simple Message Queue (formerly MNS)
CloudFlow
EventBridge
Application Real-Time Monitoring Service
Managed Service for Prometheus
Managed Service for Grafana
Managed Service for OpenTelemetry
Performance Testing
STAROps
Databases
ApsaraDB Console
PolarDB
ApsaraDB RDS
ApsaraDB for OceanBase (Deprecated)
Tair (Redis® OSS-Compatible)
Lindorm
Time Series Database
ApsaraDB for MongoDB
ApsaraDB for HBase
ApsaraDB for Memcache
ApsaraDB for MyBase
AnalyticDB
ApsaraDB for ClickHouse
ApsaraDB for SelectDB
Data Transmission Service
Database Autonomy Service
Data Management
Database Gateway - Deprecated
ApsaraDB for Cassandra - Deprecated
Analytics Computing
MaxCompute
Hologres
Realtime Compute for Apache Flink
Elasticsearch
Vector Retrieval Service for Milvus
E-MapReduce
Data Lake Formation
DataV
Quick BI
Quick Audience
Quick Tracking
DataWorks
DataHub
Dataphin
Media Services
ApsaraVideo VOD
ApsaraVideo Live
Intelligent Media Services
ApsaraVideo Media Processing
Apsara Video SDK
Enterprise Services & Cloud Communication
Energy Expert
CloudQuotation
Salesforce on Alibaba Cloud
GoChina ICP Filing Assistant
Marketplace
Alibaba Mail
Direct Mail
Short Message Service
Voice Service
Phone Number Verification Service
Cell Phone Number Service
Chat App Message Service
Financial Intelligence Engine
Domain Names and Websites
Domain Names
ICP Filing
Alibaba Cloud DNS
End User Computing
Elastic Desktop Service
App Streaming
WUYING Terminal
Cloud Phone
AgentBay
Internet of Things
IoT Platform
Serverless
Serverless App Engine
CloudFlow
EventBridge
Simple Message Queue (formerly MNS)
Function Compute
Developer Tools
OpenAPI Explorer
Alibaba Cloud SDK
Cloud Shell
Resource Orchestration Service
Alibaba Cloud CLI
BSS OpenAPI
Terraform
Pulumi
Ticket System API
Mobile Platform as a Service
Alibaba Cloud DevOps
API Gateway
Cloud Control API
AI Coding Assistant Lingma
Cloud Skills Portal
Migration & O&M Management
CloudOps Orchestration Service
Cloud Monitor
Intelligent Advisor
Cloud Governance Center
ActionTrail
Cloud Config
Resource Access Management
Resource Management
Cloud Architect Design Tools
Migration Hub
Server Migration Center
Service Catalog
Logic Composer
Quota Center
CloudSSO
HTTPDNS
Solutions
SAP
SuperApp
OpenLake
Membership Service
Expenses and Costs
Account Center
More
Support
Legal
Tech Share Terms and Conditions
After Sales Support
China Gateway Program
Service Level Objectives
Management Console
Security Control
Model Studio provides an OpenAI-compatible interface for Qwen models. To migrate from OpenAI, update your API key, BASE_URL, and model name.
OpenAI compatibility
BASE_URL
Configure the BASE_URL to connect to Model Studio through the OpenAI-compatible interface. The BASE_URL is the network endpoint for the model service.
- When using the OpenAI SDK or other OpenAI-compatible SDKs, configure the
BASE_URLas follows:
http
Singapore: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1
US (Virginia): https://dashscope-us.aliyuncs.com/compatible-mode/v1
China (Beijing): https://dashscope.aliyuncs.com/compatible-mode/v1
China (Hong Kong): https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1- When making HTTP requests, configure the full endpoint as follows:
http
Singapore: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions
US (Virginia): POST https://dashscope-us.aliyuncs.com/compatible-mode/v1/chat/completions
China (Beijing): POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
China (Hong Kong): POST https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1/chat/completionsImportant
The legacy Singapore domain https://dashscope-intl.aliyuncs.comand China (Hong Kong) domain https://cn-hongkong.dashscope.aliyuncs.comwill be discontinued. Migrate to https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com (Singapore) and https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com (China (Hong Kong)) as soon as possible.
Supported models
Supported models include Qwen large language models (commercial and open source), Qwen-VL, Qwen-Coder, Qwen-Omni, and Qwen-Math, DeepSeek, Kimi, GLM, MiniMax.
Call Qwen models via the OpenAI SDK
Prerequisites
Install Python.
Install the latest OpenAI SDK.
shell
# If the following command fails, replace pip with pip3.
pip install -U openaiActivate Model Studio and get an API key. See Obtain an API key.
Configure the API key as an environment variable to reduce exposure risk. Configure the API key as an environment variable. You can also set it directly in code, but this increases exposure risk.
Select a model from the List of supported models.
Usage
Non-streaming call example
python
from openai import OpenAI
import os
def get_response():
client = OpenAI(
# API keys differ by region. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"), # If you have not configured an environment variable, replace this line with your Model Studio API key: api_key="sk-xxx"
# The following is the base_url for the Singapore region.
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-plus", # This example uses qwen-plus. You can change the model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models
messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},\
{'role': 'user', 'content': 'Who are you?'}]
)
print(completion.model_dump_json())
if __name__ == '__main__':
get_response()Output:
json
{
"id": "chatcmpl-xxx",
"choices": [\
{\
"finish_reason": "stop",\
"index": 0,\
"logprobs": null,\
"message": {\
"content": "I am a large-scale pre-trained model from Alibaba Cloud. My name is Qwen.",\
"role": "assistant",\
"function_call": null,\
"tool_calls": null\
}\
}\
],
"created": 1716430652,
"model": "qwen-plus",
"object": "chat.completion",
"system_fingerprint": null,
"usage": {
"completion_tokens": 18,
"prompt_tokens": 22,
"total_tokens": 40
}
}Streaming call example
python
from openai import OpenAI
import os
def get_response():
client = OpenAI(
# API keys differ by region. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
# If you have not configured an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx"
api_key=os.getenv("DASHSCOPE_API_KEY"),
# The following is the base_url for the Singapore region.
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-plus", # This example uses qwen-plus. You can change the model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models
messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},\
{'role': 'user', 'content': 'Who are you?'}],
stream=True,
# The following setting displays token usage information in the last line of the streaming output.
stream_options={"include_usage": True}
)
for chunk in completion:
print(chunk.model_dump_json())
if __name__ == '__main__':
get_response()Output:
json
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":"","function_call":null,"role":"assistant","tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":"I am","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":" a large","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":" language model","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":" from Alibaba","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":" Cloud, and my","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":" name is Qwen.","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":"","function_call":null,"role":null,"tool_calls":null},"finish_reason":"stop","index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":{"completion_tokens":16,"prompt_tokens":22,"total_tokens":38}}Function call example
The following code demonstrates multi-turn function calling with weather and time query tools.
python
from openai import OpenAI
from datetime import datetime
import json
import os
client = OpenAI(
# API keys differ by region. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
# If you have not configured an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# The following is the base_url for the Singapore region.
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# Define the list of tools. The model refers to the name and description of the tools when selecting which one to use.
tools = [\
# Tool 1: Get the current time.\
{\
"type": "function",\
"function": {\
"name": "get_current_time",\
"description": "Useful when you want to know the current time.",\
# Since no input parameters are needed to get the current time, the 'parameters' object is empty.\
"parameters": {}\
}\
},\
# Tool 2: Get the weather for a specified city.\
{\
"type": "function",\
"function": {\
"name": "get_current_weather",\
"description": "Useful when you want to query the weather for a specified city.",\
"parameters": {\
"type": "object",\
"properties": {\
# A location is required to query the weather, so a 'location' parameter is defined.\
"location": {\
"type": "string",\
"description": "A city or district, such as Beijing, Hangzhou, or Yuhang."\
}\
},\
"required": [\
"location"\
]\
}\
}\
}\
]
# Simulate a weather query tool. Example result: "It is rainy in Beijing today."
def get_current_weather(location):
return f"It is rainy in {location} today. "
# A tool to query the current time. Example result: "Current time: 2024-04-15 17:15:18."
def get_current_time():
# Get the current date and time.
current_datetime = datetime.now()
# Format the current date and time.
formatted_time = current_datetime.strftime('%Y-%m-%d %H:%M:%S')
# Return the formatted current time.
return f"Current time: {formatted_time}."
# Define the model response function.
def get_response(messages):
completion = client.chat.completions.create(
model="qwen-plus", # This example uses qwen-plus. You can change the model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models
messages=messages,
tools=tools
)
return completion.model_dump()
def call_with_messages():
print('\n')
messages = [\
{\
"content": input('Please enter: '), # Example prompts: "What time is it now?" "What time will it be in an hour?" "What is the weather like in Beijing?"\
"role": "user"\
}\
]
print("-"*60)
# First turn of the model call.
i = 1
first_response = get_response(messages)
assistant_output = first_response['choices'][0]['message']
print(f"\nLLM output in turn {i}: {first_response}\n")
if assistant_output['content'] is None:
assistant_output['content'] = ""
messages.append(assistant_output)
# If the model determines that a tool call is not needed, it prints the assistant's reply directly.
if assistant_output['tool_calls'] == None:
print(f"No tool call is needed. I can reply directly: {assistant_output['content']}")
return
# If a tool call is needed, the code continues to make model calls until a final answer is generated.
while assistant_output['tool_calls'] != None:
# If the model determines that the weather query tool needs to be called, run the weather query tool.
if assistant_output['tool_calls'][0]['function']['name'] == 'get_current_weather':
tool_info = {"name": "get_current_weather", "role":"tool"}
# Extract the location parameter.
location = json.loads(assistant_output['tool_calls'][0]['function']['arguments'])['location']
tool_info['content'] = get_current_weather(location)
# If the model determines that the time query tool needs to be called, run the time query tool.
elif assistant_output['tool_calls'][0]['function']['name'] == 'get_current_time':
tool_info = {"name": "get_current_time", "role":"tool"}
tool_info['content'] = get_current_time()
print(f"Tool output: {tool_info['content']}\n")
print("-"*60)
messages.append(tool_info)
assistant_output = get_response(messages)['choices'][0]['message']
if assistant_output['content'] is None:
assistant_output['content'] = ""
messages.append(assistant_output)
i += 1
print(f"LLM output in turn {i}: {assistant_output}\n")
print(f"Final answer: {assistant_output['content']}")
if __name__ == '__main__':
call_with_messages()If you enter What's the weather like in Hangzhou and Beijing? What time is it now?, the program returns the following output:
Parameters
Input parameters compatible with the OpenAI API:
| Parameter | Type | Default | Description |
|---|
| Parameter | Type | Default | Description |
| model | string | - | The model to use. See List of supported models. |
| messages | array | - | The conversation history between the user and the model. Each element in the array has the format {"role": , "content": }. The available roles are system, user, and assistant. The system role is supported only in messages[0]. In general, the user and assistant roles must alternate, and the role of the last element in messages must be user. |
| top_p (optional) | float | - | Nucleus sampling threshold. A value of 0.8 retains the smallest set of tokens whose cumulative probability exceeds 0.8. Range: (0, 1.0). Higher values increase randomness; lower values increase determinism. |
| temperature (optional) | float | - | Controls output randomness. A higher value produces more diverse output; a lower value produces more deterministic output. Range: [0, 2). Do not set to 0. |
| presence_penalty (optional) | float | - | Controls the repetition of tokens in the generated sequence. A higher presence_penalty value reduces token repetition. The value must be in the range of [-2.0, 2.0]. Note This parameter is supported only by commercial Qwen models and open source models from qwen1.5 and later. |
| n (optional) | integer | 1 | The number of responses to generate. The value must be in the range of 1-4. For scenarios that require multiple responses, such as creative writing or ad copy, you can set a larger value for n. > Setting a larger n value does not increase input token usage but increases output token usage. > This parameter is currently supported only for the qwen-plus model, and its value is fixed to 1 when the tools parameter is passed. |
| max_tokens (optional) | integer | - | Maximum number of tokens the model can generate. Output limits vary by model. Check the supported models list above. |
| seed (optional) | integer | - | Random number seed for generation. seed supports unsigned 64-bit integers. |
| stream (optional) | boolean | False | Enables streaming output. When enabled, the API returns a generator that you iterate through. Each chunk is an incremental part of the response. |
| stop (optional) | string or array | None | The stop parameter provides precise control over the generation process by stopping generation when the model is about to output a specified string or token ID. stop can be a string or an array. - string type Stops generation when the model is about to generate the specified stop word. For example, if stop is set to "hello", the model will stop when it is about to generate "hello". - array type Elements can be token IDs, strings, or arrays of token IDs. Generation stops when the next token matches a stop list entry. Examples use the qwen-turbo tokenizer: 1. Elements are token IDs: The token IDs 108386 and 104307 correspond to the tokens "hello" and "weather" respectively. If stop is set to [108386,104307], the model will stop when it is about to generate "hello" or "weather". 2. Elements are strings: If stop is set to ["hello","weather"], the model will stop when it is about to generate "hello" or "weather". 3. Elements are arrays of token IDs: The token IDs 108386 and 103924 correspond to the tokens "hello" and "ah", and the token IDs 35946 and 101243 correspond to "I" and "am fine". If stop is set to [[108386, 103924],[35946, 101243]], the model stops when it is about to generate "hello ah" or "I am fine". Note When stop is an array, its elements must be of the same type. You cannot mix token IDs and strings, for example, ["hello", 104307]. |
| tools (optional) | array | None | Tools the model can call. In a function call flow, the model selects one tool from this library. Each tool in the tools array has the following structure: - type: Tool type. Currently only function is supported. - function: An object with the following keys: name, description, and parameters: - name: Function name. Only letters, numbers, underscores, and hyphens; max 64 characters. - description: Function description. The model uses this to decide when and how to call the function. - parameters: An object describing the tool's parameters as valid JSON Schema. If the parameters object is empty, the function takes no input parameters. In a function call flow, set the tools parameter both when initiating the call and when submitting tool results. Supported models: qwen-turbo, qwen-plus, and qwen-max. Note The tools parameter cannot be used with stream=True. |
| stream_options (optional) | object | None | Displays token usage during streaming. Effective only when stream is True. Set stream_options={"include_usage":True} to include token counts. |
| \ |
Response parameters\
| Parameter | Type | Description | Remarks |
| --- | --- | --- | --- |
| | | | |
| --- | --- | --- | --- |
| Parameter | Type | Description | Remarks |
| id | string | A unique, system-generated ID for the request. | - |
| model | string | The model used for the request. | - |
| system_fingerprint | string | Currently unused. Returns an empty string. | - |
| choices | array | A list of generated chat completions. | - |
| choices[i].finish_reason | string | The reason the model stopped generating tokens. Possible values are: - null: The generation is still in progress. - stop: The model generated a stop sequence specified in the request. - length: The maximum number of tokens specified in the request was reached. |
| choices[i].message | object | A message object generated by the model. |
| choices[i].message.role | string | The role of the message author. This value is always assistant. |
| choices[i].message.content | string | The model-generated message content. |
| choices[i].index | integer | The index of the choice in the choices list. The default value is 0. |
| created | integer | The creation time of the chat completion, as a Unix timestamp in seconds. | - |
| usage | object | Token usage statistics for the request. | - |
| usage.prompt_tokens | integer | The number of tokens in the input prompt. | - |
| usage.completion_tokens | integer | The number of tokens in the generated completion. | - |
| usage.total_tokens | integer | The total number of tokens used in the request (prompt_tokens + completion_tokens). | - |
\
Call with the langchain_openai SDK\
\
Prerequisites\
\
- Install Python.
\ - Install the langchain_openai SDK.
\
shell\
# If the following command fails, replace pip with pip3.\
pip install -U langchain_openai\
```\
\
\
- Activate Model Studio and get an API key. See Obtain an API key.\
\
- Configure the API key as an environment variable to reduce exposure risk. Configure the API key as an environment variable. You can also set it in code, **but this increases exposure risk**.\
\
- Select a model from the Supported model list.\
\
\
### **Usage**\
\
#### **Non-streaming output**\
\
Use the `invoke` method for non-streaming output:\
\
```python\
from langchain_openai import ChatOpenAI\
import os\
\
def get_response():\
llm = ChatOpenAI(\
# API keys differ by region. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key\
api_key=os.getenv("DASHSCOPE_API_KEY"), # If you have not configured an environment variable, replace this line with your Model Studio API key: api_key="sk-xxx"\
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1", # This is the base_url for the Singapore region.\
model="qwen-plus" # This example uses qwen-plus. You can change the model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models\
)\
messages = [\
{"role":"system","content":"You are a helpful assistant."},\
{"role":"user","content":"Who are you?"}\
]\
response = llm.invoke(messages)\
print(response.json())\
\
if __name__ == "__main__":\
get_response()\
```\
\
Output:\
\
```json\
{\
"content": "I am a large language model from Alibaba Cloud. My name is Tongyi Qwen.",\
"additional_kwargs": {},\
"response_metadata": {\
"token_usage": {\
"completion_tokens": 16,\
"prompt_tokens": 22,\
"total_tokens": 38\
},\
"model_name": "qwen-plus",\
"system_fingerprint": "",\
"finish_reason": "stop",\
"logprobs": null\
},\
"type": "ai",\
"name": null,\
"id": "run-xxx",\
"example": false,\
"tool_calls": [],\
"invalid_tool_calls": []\
}\
```\
\
#### **Streaming output**\
\
Use the `stream` method for streaming output. No additional `stream` parameter is required.\
\
```python\
from langchain_openai import ChatOpenAI\
import os\
\
def get_response():\
llm = ChatOpenAI(\
# API keys differ by region. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key\
api_key=os.getenv("DASHSCOPE_API_KEY"), # If you have not configured an environment variable, replace this line with your Model Studio API key: api_key="sk-xxx"\
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1", # This is the base_url for the Singapore region.\
model="qwen-plus", # This example uses qwen-plus. You can change the model name as needed. For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models\
stream_usage=True\
)\
messages = [\
{"role":"system","content":"You are a helpful assistant."},\
{"role":"user","content":"Who are you?"},\
]\
response = llm.stream(messages)\
for chunk in response:\
print(chunk.model_dump_json())\
\
if __name__ == "__main__":\
get_response()\
```\
\
Output:\
\
```json\
{"content": "", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}\
{"content": "I", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}\
{"content": " am", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}\
{"content": " a", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}\
{"content": " large", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}\
{"content": " language model", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}\
{"content": " from", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}\
{"content": " Alibaba Cloud. My name is Tongyi", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}\
{"content": " Qwen.", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}\
{"content": "", "additional_kwargs": {}, "response_metadata": {"finish_reason": "stop"}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}\
{"content": "", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": {"input_tokens": 22, "output_tokens": 16, "total_tokens": 38}, "tool_call_chunks": []}\
```\
\
For input parameters, see Input parameters.\
\
## **HTTP API calls**\
\
Call Model Studio via HTTP. Responses follow the same structure as the OpenAI API.\
\
### **Prerequisites**\
\
- Activate Model Studio and get an API key. See Obtain an API key.\
\
- Configure the API key as an environment variable to reduce exposure risk. Configure an API key as an environment variable. You can also set it in code, **but this increases exposure risk**.\
\
\
### **API request**\
\
```http\
Singapore: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions\
US (Virginia): POST https://dashscope-us.aliyuncs.com/compatible-mode/v1/chat/completions\
China (Beijing): POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions\
China (Hong Kong): POST https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1/chat/completions\
```\
\
### **Request example**\
\
Call the API with `cURL`.\
\
**Note**\
\
If you have not configured the API key as an environment variable, replace $DASHSCOPE\_API\_KEY with your API key.\
\
#### **Non-streaming output**\
\
curl\
\
curl\
\
```curl\
# This is the base_url for the Singapore region.\
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \\
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \\
--header 'Content-Type: application/json' \\
--data '{\
"model": "qwen-plus",\
"messages": [\
{\
"role": "system",\
"content": "You are a helpful assistant."\
},\
{\
"role": "user",\
"content": "Who are you?"\
}\
]\
}'\
```\
\
Output:\
\
```json\
{\
"choices": [\
{\
"message": {\
"role": "assistant",\
"content": "I am a large language model from Alibaba Cloud. My name is Qwen."\
},\
"finish_reason": "stop",\
"index": 0,\
"logprobs": null\
}\
],\
"object": "chat.completion",\
"usage": {\
"prompt_tokens": 11,\
"completion_tokens": 16,\
"total_tokens": 27\
},\
"created": 1715252778,\
"system_fingerprint": "",\
"model": "qwen-plus",\
"id": "chatcmpl-xxx"\
}\
```\
\
#### **Streaming output**\
\
To enable streaming output, set the `stream` parameter to `true` in the request body.\
\
```curl\
# This is the base_url for the Singapore region.\
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \\
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \\
--header 'Content-Type: application/json' \\
--data '{\
"model": "qwen-plus",\
"messages": [\
{\
"role": "system",\
"content": "You are a helpful assistant."\
},\
{\
"role": "user",\
"content": "Who are you?"\
}\
],\
"stream":true\
}'\
```\
\
Output:\
\
```json\
data: {"choices":[{"delta":{"content":"","role":"assistant"},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}\
\
data: {"choices":[{"finish_reason":null,"delta":{"content":"I am "},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}\
\
data: {"choices":[{"delta":{"content":"a large "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}\
\
data: {"choices":[{"delta":{"content":"language "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}\
\
data: {"choices":[{"delta":{"content":"model from "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}\
\
data: {"choices":[{"delta":{"content":"Alibaba Cloud. "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}\
\
data: {"choices":[{"delta":{"content":"My name is Qwen."},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}\
\
data: {"choices":[{"delta":{"content":""},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}\
\
data: [DONE]\
```\
\
See Input parameter configuration.\
\
### **Error response example**\
\
If a request fails, the response includes an error code and message explaining the failure.\
\
```json\
{\
"error": {\
"message": "Incorrect API key provided. ",\
"type": "invalid_request_error",\
"param": null,\
"code": "invalid_api_key"\
}\
}\
```\
\
## **Status codes**\
\
| **Error code** | **Description** |\
| --- | --- |\
\
| | |\
| --- | --- |\
| **Error code** | **Description** |\
| 400 - Invalid request error | Invalid request. See the error message for details. |\
| 401 - Incorrect API key provided | The provided API key is invalid. |\
| 429 - Rate limit reached for requests | The request rate has exceeded the limit, such as for queries per second (QPS) or queries per minute (QPM). |\
| 429 - You exceeded your current quota, please check your plan and billing details | Quota exceeded or your account has an overdue payment. |\
| 500 - The server had an error while processing your request | The server encountered an internal error. |\
| 503 - The engine is currently overloaded, please try again later | The service is temporarily overloaded. Please try again later. |\
\
Previous: Qwen-OCRNext: OpenAI-compatible - Responses\
\
Is this page helpful?\
\
\
\
OpenAI compatibility\
\
BASE\_URL\
\
Supported models\
\
Call Qwen models via the OpenAI SDK\
\
Prerequisites\
\
Usage\
\
Parameters\
\
Response parameters\
\
Call with the langchain\_openai SDK\
\
Prerequisites\
\
Usage\
\
HTTP API calls\
\
Prerequisites\
\
API request\
\
Request example\
\
Error response example\
\
Status codes\
\
\
\
[](https://www.alibabacloud.com/help/en/model-studio/compatibility-of-openai-with-dashscope#top "Back to Top")\
\
Contact Us\
\
\
\
## Sales Support\
\
Live-chat with our sales team or get in touch with a business development professional in your region.\
\
Contact Sales\
\
## Technical Support\
\
Open a ticket and get quick help from our technical team.\
\
Open a Ticket >\
\
## Connect & Report Abuse\
\
We look forward to your suggestion.\
\
Post a Suggestion > Report Abuse >\
\
\\
\\
Hi, I'm Alibaba Cloud AI Assistant!\\
\\
I can help with questions and solutions.\
\
Why Alibaba Cloud\
\
About Alibaba Cloud\
\
Asia Accelerator\
\
Our Global Network\
\
Global Offices\
\
Trust Center\
\
Case Studies\
\
Analyst Reports\
\
Products & Pricings\
\
Pricing Calculator\
\
ECS\
\
SAS\
\
Model Studio\
\
Database\
\
Security\
\
SMS\
\
Solutions\
\
Financial Services\
\
Retail Services\
\
Media Services\
\
Gaming Services\
\
ISV Solutions\
\
Engage\
\
Developer Community\
\
Partner Network\
\
Startups\
\
Marketplace\
\
Join Alibaba Cloud\
\
Resources & Support\
\
Developer Learning Hub\
\
Documentation Center\
\
Training & Certification\
\
Service Notices\
\
Submit a Ticket\
\
Security Report\
\
Qwen Cloud\
\
Careers About Us Privacy Policy Legal Integrity Compliance Reporting Channel Service Notices Links\
\
© 2009-2026 Copyright by Alibaba Cloud All rights reserved\
\
- Facebook\
- Linkedin\
- Twitter\
- YouTube\
- TikTok\
- contact.us@alibabacloud.com\
- Call Us Now\
- Discord\
\
[](https://privacy.truste.com/privacy-seal/validation?rid=e83a96cf-eaa2-4c82-8d85-883196fcb42c)\
\
[](https://privacy.truste.com/privacy-seal/validation?rid=51be162a-c152-402b-84d2-d0a724de679a)\
\
© 2009-2026 Copyright by Alibaba Cloud All rights reserved\
\
Careers About Us Privacy Policy Legal Integrity Compliance Reporting Channel Service Notices Links\
\
```\
\