Skip to content

Qwen-Coder Model Capabilities

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

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

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

Qwen-Coder is a language model designed for code-related tasks. Use the API to invoke the model for code generation and code completion. You can also interact with external systems through tool calling.

QuickStart

API usage prerequisites: Obtain an API key and configure your API key in environment variables (to be deprecated, merged into API key configuration). If you invoke the API through an SDK, install the OpenAI or DashScope SDK.

The following example shows how to invoke the qwen3-coder-next model to write a Python function that finds prime numbers.

OpenAI-compatible Chat Completions API

DashScope

Python

Node.js

curl

Request example

python
import os
from openai import OpenAI

client = OpenAI(
    # API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    # If you have not set an environment variable, replace the next line with your Model Studio API key: api_key="sk-xxx",
    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-coder-next",
    messages=[\
        {'role': 'system', 'content': 'You are a helpful assistant.'},\
        {'role': 'user', 'content': 'Write a Python function find_prime_numbers that takes an integer n as input and returns a list of all prime numbers less than n. Do not output any non-code content or Markdown code blocks.'}],
)
print(completion.choices[0].message.content)

Response

plaintext
def find_prime_numbers(n):
    if n <= 2:
        return []

    primes = []
    for num in range(2, n):
        is_prime = True
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(num)

    return primes

Request example

nodejs
import OpenAI from "openai";

const client = new OpenAI(
    {
        // API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
        // If you have not set an environment variable, replace the next line with your Model Studio API key: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        // To use models in the Beijing region, change baseURL to: https://dashscope.aliyuncs.com/compatible-mode/v1
        baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
    }
);

async function main() {
    const completion = await client.chat.completions.create({
        model: "qwen3-coder-next",
        messages: [\
            { role: "system", content: "You are a helpful assistant." },\
            { role: "user", content: "Write a Python function find_prime_numbers that takes an integer n as input and returns a list of all prime numbers less than n. Do not output any non-code content or Markdown code blocks." }\
        ],
    });
    console.log(completion.choices[0].message.content);
}

main();

Response

plaintext
def find_prime_numbers(n):
    if n <= 2:
        return []

    primes = []
    for num in range(2, n):
        is_prime = True
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(num)

    return primes

Request example

To use models in the Beijing region, change the URL to: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions

curl
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-coder-next",
    "messages": [\
        {\
            "role": "system",\
            "content": "You are a helpful assistant."\
        },\
        {\
            "role": "user",\
            "content": "Write a Python function find_prime_numbers that takes an integer n as input and returns a list of all prime numbers less than n. Do not output any non-code content or Markdown code blocks."\
        }\
    ]
}'

Response

json
{
    "model": "qwen3-coder-next",
    "id": "chatcmpl-3123d5cb-01b8-9a90-98cc-5bffbb369xxx",
    "choices": [\
        {\
            "message": {\
                "content": "def find_prime_numbers(n):\n    if n <= 2:\n        return []\n    \n    primes = []\n    for num in range(2, n):\n        is_prime = True\n        for i in range(2, int(num ** 0.5) + 1):\n            if num % i == 0:\n                is_prime = False\n                break\n        if is_prime:\n            primes.append(num)\n    \n    return primes",\
                "role": "assistant"\
            },\
            "index": 0,\
            "finish_reason": "stop"\
        }\
    ],
    "created": 1770108104,
    "object": "chat.completion",
    "usage": {
        "total_tokens": 155,
        "completion_tokens": 89,
        "prompt_tokens": 66
    }
}

Python

Java

curl

Request example

python
import dashscope
import os

dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'

messages = [\
    {\
        "role": "system",\
        "content": "You are a helpful assistant."\
    },\
    {\
        "role": "user",\
        "content": "Write a Python function find_prime_numbers that takes an integer n as a parameter and returns a list of all prime numbers less than n. Do not output non-code content or Markdown code blocks."\
    }\
]

response = dashscope.Generation.call(
    # API keys vary by region. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
    # If you have not configured the environment variable, replace the following line with your Model Studio API key: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model="qwen3-coder-next",
    messages=messages,
    result_format="message"
)

if response.status_code == 200:
    print(response.output.choices[0].message.content)
else:
    print(f"HTTP return code: {response.status_code}")
    print(f"Error code: {response.code}")
    print(f"Error message: {response.message}")

Response

python
def find_prime_numbers(n):
    if n <= 2:
        return []

    primes = []
    for num in range(2, n):
        is_prime = True
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(num)

    return primes

Request example

java
import java.util.Arrays;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.protocol.Protocol;

public class Main {
    public static GenerationResult callWithMessage()
            throws NoApiKeyException, ApiException, InputRequiredException {
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        Generation gen = new Generation(Protocol.HTTP.getValue(), "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1");
        Message sysMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant.").build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("Write a Python function find_prime_numbers that takes an integer n as a parameter and returns a list of all prime numbers less than n. Do not output non-code content or Markdown code blocks.").build();
        GenerationParam param = GenerationParam.builder()
                .apiKey(apiKey)
                .model("qwen3-coder-next")
                .messages(Arrays.asList(sysMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        return gen.call(param);
    }
    public static void main(String[] args){
        try {
            GenerationResult result = callWithMessage();
            System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.err.println("Request exception: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Response

python
def find_prime_numbers(n):
    if n <= 2:
        return []

    primes = []
    for num in range(2, n):
        is_prime = True
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(num)

    return primes

Request example

To use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation

curl
curl -X POST "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-coder-next",
    "input":{
        "messages":[\
            {\
                "role": "system",\
                "content": "You are a helpful assistant."\
            },\
            {\
                "role": "user",\
                "content": "Write a Python function find_prime_numbers that takes an integer n as a parameter and returns a list of all prime numbers less than n. Do not output non-code content or Markdown code blocks."\
            }\
        ]
    },
    "parameters": {
        "result_format": "message"
    }
}'

Response

json
{
    "output": {
        "choices": [\
            {\
                "message": {\
                    "content": "def find_prime_numbers(n):\n    if n <= 2:\n        return []\n    \n    primes = []\n    for num in range(2, n):\n        is_prime = True\n        for i in range(2, int(num ** 0.5) + 1):\n            if num % i == 0:\n                is_prime = False\n                break\n        if is_prime:\n            primes.append(num)\n    \n    return primes",\
                    "role": "assistant"\
                },\
                "finish_reason": "stop"\
            }\
        ]
    },
    "usage": {
        "total_tokens": 155,
        "input_tokens": 66,
        "output_tokens": 89
    },
    "request_id": "dd78b1cf-8029-46bb-9bea-b794ded7bxxx"
}

Model Selection

  • Recommended choice:qwen3-coder-next delivers high code quality, fast response times, and cost efficiency, making it ideal for most scenarios. It supports multi-turn tool calling, improves repository-level code understanding, and enhances stability and adaptability for Agentic coding tools.

  • Model selection reference: We recommend using the latest general-purpose models as alternatives to Qwen-Coder models. See Model List to choose the model that best fits your scenario.

For information about model names, context, prices, snapshot versions, and more, see Model List ( Commercial Edition | Open Source Edition). For information about concurrent rate limiting, see Rate Limiting ( Commercial Edition | Open Source Edition).

Core capabilities

Tool calling

You can provide the model with a set of tools to enable interaction with external environments—for example, reading or writing files, calling APIs, or managing databases. The model determines whether and how to call these tools based on your instructions. For more information, see Function Calling.

The complete tool calling flow includes the following steps:

  1. Define tools and send a request: Include a list of tools in your request and ask the model to complete a task that requires those tools.

  2. Execute the tools: Parse the tool_calls returned by the model and call the corresponding tool functions that you have implemented locally to perform the requested task.

  3. Return the execution results: Package the tool execution results in the required format and send them back to the model. The model then uses those results to complete the final task.

The following example shows how to guide the model to generate code and save it to a local file using the write_file tool.

OpenAI compatible-Chat Completions API

DashScope

Python

Node.js

curl

python
import os
import json
from openai import OpenAI

client = OpenAI(
    # API keys vary by region. To obtain an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
    # If you have not configured the environment variable, replace the following line with your Alibaba Cloud Model Studio API key: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

tools = [\
    {\
        "type": "function",\
        "function": {\
            "name": "write_file",\
            "description": "Writes content to a specified file. Creates the file if it does not exist.",\
            "parameters": {\
                "type": "object",\
                "properties": {\
                    "path": {\
                        "type": "string",\
                        "description": "The relative or absolute path of the object file"\
                    },\
                    "content": {\
                        "type": "string",\
                        "description": "The string content to write to the file"\
                    }\
                },\
                "required": ["path", "content"]\
            }\
        }\
    }\
]

# Tool function implementation
def write_file(path: str, content: str) -> str:
    """Writes content to a file."""
    try:
        # Ensure the directory exists
        os.makedirs(os.path.dirname(path),
                    exist_ok=True) if os.path.dirname(path) else None
        with open(path, 'w', encoding='utf-8') as f:
            f.write(content)
        return f"Success: File '{path}' has been written."
    except Exception as e:
        return f"Error: An exception occurred while writing the file - {str(e)}"

messages = [{"role": "user", "content": "Write a Python quick sort script and name it quick_sort.py"}]

completion = client.chat.completions.create(
    model="qwen3-coder-next",
    messages=messages,
    tools=tools
)

assistant_output = completion.choices[0].message
if assistant_output.content is None:
    assistant_output.content = ""
messages.append(assistant_output)

# If no tool call is needed, output the content directly
if assistant_output.tool_calls is None:
    print(f"No tool call needed. Direct response: {assistant_output.content}")
else:
    # Enter the tool calling loop
    while assistant_output.tool_calls is not None:
        for tool_call in assistant_output.tool_calls:
            tool_call_id = tool_call.id
            func_name = tool_call.function.name
            arguments = json.loads(tool_call.function.arguments)
            print(f"Calling tool [{func_name}], arguments: {arguments}")
            # Execute the tool
            tool_result = write_file(**arguments)
            # Construct the tool return message
            tool_message = {
                "role": "tool",
                "tool_call_id": tool_call_id,
                "content": tool_result,
            }
            print(f"Tool returns: {tool_message['content']}")
            messages.append(tool_message)
        # Call the model again to get a summarized natural language response
        response = client.chat.completions.create(
            model="qwen3-coder-next",
            messages=messages,
            tools=tools
        )
        assistant_output = response.choices[0].message
        if assistant_output.content is None:
            assistant_output.content = ""
        messages.append(assistant_output)
    print(f"Final model response: {assistant_output.content}")

Result

plaintext
Calling tool [write_file], arguments: {'content': 'def quick_sort(arr):\\n    if len(arr) <= 1:\\n        return arr\\n    pivot = arr[len(arr) // 2]\\n    left = [x for x in arr if x < pivot]\\n    middle = [x for x in arr if x == pivot]\\n    right = [x for x in arr if x > pivot]\\n    return quick_sort(left) + middle + quick_sort(right)\\n\\nif __name__ == \\"__main__\\":\\n    example_list = [3, 6, 8, 10, 1, 2, 1]\\n    print(\\"Original list:\\", example_list)\\n    sorted_list = quick_sort(example_list)\\n    print(\\"Sorted list:\\", sorted_list)', 'path': 'quick_sort.py'}
Tool returns: Success: File 'quick_sort.py' has been written.
Final model response: OK. I have created the file `quick_sort.py` for you, which contains the Python implementation of quick sort. You can run this file to see the sample output. Let me know if you need any further modifications or explanations!
nodejs
import OpenAI from "openai";
import fs from "fs/promises";
import path from "path";

const client = new OpenAI({
    // API keys vary by region. To obtain an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
    // If you have not configured the environment variable, replace the following line with your Model Studio API key: apiKey: "sk-xxx",
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});

const tools = [\
    {\
        "type": "function",\
        "function": {\
            "name": "write_file",\
            "description": "Writes content to a specified file. Creates the file if it does not exist.",\
            "parameters": {\
                "type": "object",\
                "properties": {\
                    "path": {\
                        "type": "string",\
                        "description": "The relative or absolute path of the object file"\
                    },\
                    "content": {\
                        "type": "string",\
                        "description": "The string content to write to the file"\
                    }\
                },\
                "required": ["path", "content"]\
            }\
        }\
    }\
];

// Tool function implementation
async function write_file(filePath, content) {
    try {
        // For security reasons, the file writing feature is disabled by default. To use it, uncomment the following lines and ensure the path is secure.
        // const dir = path.dirname(filePath);
        // if (dir) {
        //     await fs.mkdir(dir, { recursive: true });
        // }
        // await fs.writeFile(filePath, content, "utf-8");
        return `Success: File '${filePath}' has been written`;
    } catch (error) {
        return `Error: An exception occurred while writing the file - ${error.message}`;
    }
}

const messages = [{"role": "user", "content": "Write a Python quick sort script and name it quick_sort.py"}];

async function main() {
    const completion = await client.chat.completions.create({
        model: "qwen3-coder-next",
        messages: messages,
        tools: tools
    });

    let assistant_output = completion.choices[0].message;
    // Ensure content is not null
    if (!assistant_output.content) assistant_output.content = "";
    messages.push(assistant_output);

    // If no tool call is needed, output the content directly
    if (!assistant_output.tool_calls) {
        console.log(`No tool call needed. Direct response: ${assistant_output.content}`);
    } else {
        // Enter the tool calling loop
        while (assistant_output.tool_calls) {
            for (const tool_call of assistant_output.tool_calls) {
                const tool_call_id = tool_call.id;
                const func_name = tool_call.function.name;
                const args = JSON.parse(tool_call.function.arguments);
                console.log(`Calling tool [${func_name}], arguments: `, args);
                // Execute the tool
                const tool_result = await write_file(args.path, args.content);
                // Construct the tool return message
                const tool_message = {
                    "role": "tool",
                    "tool_call_id": tool_call_id,
                    "content": tool_result
                };
                console.log(`Tool returns: ${tool_message.content}`);
                messages.push(tool_message);
            }
            // Call the model again to get a summarized natural language response
            const response = await client.chat.completions.create({
                model: "qwen3-coder-next",
                messages: messages,
                tools: tools
            });
            assistant_output = response.choices[0].message;
            if (!assistant_output.content) assistant_output.content = "";
            messages.push(assistant_output);
        }
        console.log(`Final model response: ${assistant_output.content}`);
    }
}

main();

Result

plaintext
Calling tool [write_file], arguments:  {
  content: 'def quick_sort(arr):\\n    if len(arr) <= 1:\\n        return arr\\n    pivot = arr[len(arr) // 2]\\n    left = [x for x in arr if x < pivot]\\n    middle = [x for x in arr if x == pivot]\\n    right = [x for x in arr if x > pivot]\\n    return quick_sort(left) + middle + quick_sort(right)\\n\\nif __name__ == \\"__main__\\":\\n    example_list = [3, 6, 8, 10, 1, 2, 1]\\n    print(\\"Original list:\\", example_list)\\n    sorted_list = quick_sort(example_list)\\n    print(\\"Sorted list:\\", sorted_list)',
  path: 'quick_sort.py'
}
Tool returns: Success: File 'quick_sort.py' has been written.
Final model response: The `quick_sort.py` file has been successfully created. It contains the Python implementation of quick sort. You can run the file to view the sorting result of the sample list. Let me know if you need further modifications or explanations!

This example shows the first step of the tool calling flow: sending a request and receiving the model’s intent to call a tool.

To use models in the Beijing region, change the URL to: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions

curl
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-coder-next",
    "messages": [\
        {\
            "role": "user",\
            "content": "Write a Python quick sort script and name it quick_sort.py"\
        }\
    ],
    "tools": [\
        {\
            "type": "function",\
            "function": {\
                "name": "write_file",\
                "description": "Writes content to a specified file. Creates the file if it does not exist.",\
                "parameters": {\
                    "type": "object",\
                    "properties": {\
                        "path": {\
                            "type": "string",\
                            "description": "The relative or absolute path of the object file"\
                        },\
                        "content": {\
                            "type": "string",\
                            "description": "The string content to write to the file"\
                        }\
                    },\
                    "required": ["path", "content"]\
                }\
            }\
        }\
    ]
}'

Result

json
{
    "choices": [\
        {\
            "message": {\
                "content": "",\
                "role": "assistant",\
                "tool_calls": [\
                    {\
                        "index": 0,\
                        "id": "call_0ca7505bb6e44471a40511e5",\
                        "type": "function",\
                        "function": {\
                            "name": "write_file",\
                            "arguments": "{\"content\": \"def quick_sort(arr):\\\\n    if len(arr) <= 1:\\\\n        return arr\\\\n    pivot = arr[len(arr) // 2]\\\\n    left = [x for x in arr if x < pivot]\\\\n    middle = [x for x in arr if x == pivot]\\\\n    right = [x for x in arr if x > pivot]\\\\n    return quick_sort(left) + middle + quick_sort(right)\\\\n\\\\nif __name__ == \\\\\\\"__main__\\\\\\\":\\\\n    example_list = [3, 6, 8, 10, 1, 2, 1]\\\\n    print(\\\\\\\"Original list:\\\\\\\", example_list)\\\\n    sorted_list = quick_sort(example_list)\\\\n    print(\\\\\\\"Sorted list:\\\\\\\", sorted_list)\", \"path\": \"quick_sort.py\"}"\
                        }\
                    }\
                ]\
            },\
            "finish_reason": "tool_calls",\
            "index": 0,\
            "logprobs": null\
        }\
    ],
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 494,
        "completion_tokens": 193,
        "total_tokens": 687,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    },
    "created": 1761620025,
    "system_fingerprint": null,
    "model": "qwen3-coder-next",
    "id": "chatcmpl-20e96159-beea-451f-b3a4-d13b218112b5"
}

Python

Java

curl

python
import os
import json
import dashscope

dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'

tools = [\
    {\
        "type": "function",\
        "function": {\
            "name": "write_file",\
            "description": "Writes content to a specified file. Creates the file if it does not exist.",\
            "parameters": {\
                "type": "object",\
                "properties": {\
                    "path": {\
                        "type": "string",\
                        "description": "The relative or absolute path of the object file"\
                    },\
                    "content": {\
                        "type": "string",\
                        "description": "The string content to write to the file"\
                    }\
                },\
                "required": ["path", "content"]\
            }\
        }\
    }\
]

# Tool function implementation
def write_file(path: str, content: str) -> str:
    """Writes content to a file."""
    try:
        # For security reasons, the file writing feature is disabled by default. To use it, uncomment the following lines and ensure the path is secure.
        # os.makedirs(os.path.dirname(path),exist_ok=True) if os.path.dirname(path) else None
        # with open(path, 'w', encoding='utf-8') as f:
        #     f.write(content)
        return f"Success: File '{path}' has been written."
    except Exception as e:
        return f"Error: An exception occurred while writing the file - {str(e)}"

messages = [{"role": "user", "content": "Write a Python quick sort script and name it quick_sort.py"}]

response = dashscope.Generation.call(
    # If you have not configured the environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    model='qwen3-coder-next',
    messages=messages,
    tools=tools,
    result_format='message'
)

if response.status_code == 200:
    assistant_output = response.output.choices[0].message
    messages.append(assistant_output)

    # If no tool call is needed, output the content directly
    if "tool_calls" not in assistant_output or not assistant_output["tool_calls"]:
        print(f"No tool call needed. Direct response: {assistant_output['content']}")
    else:
        # Enter the tool calling loop
        while "tool_calls" in assistant_output and assistant_output["tool_calls"]:
            for tool_call in assistant_output["tool_calls"]:
                func_name = tool_call["function"]["name"]
                arguments = json.loads(tool_call["function"]["arguments"])
                tool_call_id = tool_call.get("id")
                print(f"Calling tool [{func_name}], arguments: {arguments}")
                # Execute the tool
                tool_result = write_file(**arguments)
                # Construct the tool return message
                tool_message = {
                    "role": "tool",
                    "content": tool_result,
                    "tool_call_id": tool_call_id
                }
                print(f"Tool returns: {tool_message['content']}")
                messages.append(tool_message)
            # Call the model again to get a summarized natural language response
            response = dashscope.Generation.call(
                api_key=os.getenv('DASHSCOPE_API_KEY'),
                model='qwen3-coder-next',
                messages=messages,
                tools=tools,
                result_format='message'
            )
            if response.status_code == 200:
                print(f"Final model response: {response.output.choices[0].message.content}")
                assistant_output = response.output.choices[0].message
                messages.append(assistant_output)
            else:
                print(f"Error during summary response execution: {response}")
                break
else:
    print(f"Execution error: {response}")

Result

plaintext
Calling tool [write_file], arguments: {'content': 'def quick_sort(arr):\\n    if len(arr) <= 1:\\n        return arr\\n    pivot = arr[len(arr) // 2]\\n    left = [x for x in arr if x < pivot]\\n    middle = [x for x in arr if x == pivot]\\n    right = [x for x in arr if x > pivot]\\n    return quick_sort(left) + middle + quick_sort(right)\\n\\nif __name__ == \\"__main__\\":\\n    example_list = [3, 6, 8, 10, 1, 2, 1]\\n    print(\\"Original list:\\", example_list)\\n    sorted_list = quick_sort(example_list)\\n    print(\\"Sorted list:\\", sorted_list)', 'path': 'quick_sort.py'}
Tool returns: Success: File 'quick_sort.py' has been written.
Final model response: The `quick_sort.py` file has been successfully created. It contains the Python implementation of quick sort. You can run the file to view the sorting result of the sample list. Let me know if you need further modifications or explanations!
java
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.protocol.Protocol;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.tools.FunctionDefinition;
import com.alibaba.dashscope.tools.ToolCallBase;
import com.alibaba.dashscope.tools.ToolCallFunction;
import com.alibaba.dashscope.tools.ToolFunction;
import com.alibaba.dashscope.utils.JsonUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    /**
     * Writes content to a file.
     * @param arguments A JSON string passed by the model, containing the parameters required by the tool.
     * @return A string representing the result after the tool is executed.
     */
    public static String writeFile(String arguments) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode argsNode = objectMapper.readTree(arguments);
            String path = argsNode.get("path").asText();
            String content = argsNode.get("content").asText();

            // For security reasons, the file writing feature is disabled by default. To use it, uncomment the following lines and ensure the path is secure.
            // File file = new File(path);
            // File parentDir = file.getParentFile();
            // if (parentDir != null && !parentDir.exists()) {
            //     parentDir.mkdirs();
            // }
            // Files.write(Paths.get(path), content.getBytes(StandardCharsets.UTF_8));
            return "Success: File '" + path + "' has been written";
        } catch (Exception e) {
            return "Error: An exception occurred while writing the file - " + e.getMessage();
        }
    }

    public static void main(String[] args) {
        try {
            // Define the tool parameter schema
            String writePropertyParams =
                    "{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"The relative or absolute path of the object file\"},\"content\":{\"type\":\"string\",\"description\":\"The string content to write to the file\"}},\"required\":[\"path\",\"content\"]}";

            FunctionDefinition writeFileFunction = FunctionDefinition.builder()
                    .name("write_file")
                    .description("Writes content to a specified file. Creates the file if it does not exist.")
                    .parameters(JsonUtils.parseString(writePropertyParams).getAsJsonObject())
                    .build();

            Generation gen = new Generation(Protocol.HTTP.getValue(), "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1");

            String userInput = "Write a Python quick sort script and name it quick_sort.py";
            List<Message> messages = new ArrayList<>();
            messages.add(Message.builder().role(Role.USER.getValue()).content(userInput).build());

            // First call to the model
            GenerationParam param = GenerationParam.builder()
                    .model("qwen3-coder-next")
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    .messages(messages)
                    .tools(Arrays.asList(ToolFunction.builder().function(writeFileFunction).build()))
                    .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                    .build();

            GenerationResult result = gen.call(param);
            Message assistantOutput = result.getOutput().getChoices().get(0).getMessage();
            messages.add(assistantOutput);

            // If no tool call is needed, output the content directly
            if (assistantOutput.getToolCalls() == null || assistantOutput.getToolCalls().isEmpty()) {
                System.out.println("No tool call needed. Direct response: " + assistantOutput.getContent());
            } else {
                // Enter the tool calling loop
                while (assistantOutput.getToolCalls() != null && !assistantOutput.getToolCalls().isEmpty()) {
                    for (ToolCallBase toolCall : assistantOutput.getToolCalls()) {
                        ToolCallFunction functionCall = (ToolCallFunction) toolCall;
                        String funcName = functionCall.getFunction().getName();
                        String arguments = functionCall.getFunction().getArguments();
                        System.out.println("Calling tool [" + funcName + "], arguments: " + arguments);

                        // Execute the tool
                        String toolResult = writeFile(arguments);

                        // Construct the tool return message
                        Message toolMessage = Message.builder()
                                .role("tool")
                                .toolCallId(toolCall.getId())
                                .content(toolResult)
                                .build();
                        System.out.println("Tool returns: " + toolMessage.getContent());
                        messages.add(toolMessage);
                    }

                    // Call the model again to get a summarized natural language response
                    param.setMessages(messages);
                    result = gen.call(param);
                    assistantOutput = result.getOutput().getChoices().get(0).getMessage();
                    messages.add(assistantOutput);
                }
                System.out.println("Final model response: " + assistantOutput.getContent());
            }

        } catch (NoApiKeyException | InputRequiredException e) {
            System.err.println("Error: " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Result

plaintext
Calling tool [write_file], arguments: {"content": "def quick_sort(arr):\\n    if len(arr) <= 1:\\n        return arr\\n    pivot = arr[len(arr) // 2]\\n    left = [x for x in arr if x < pivot]\\n    middle = [x for x in arr if x == pivot]\\n    right = [x for x in arr if x > pivot]\\n    return quick_sort(left) + middle + quick_sort(right)\\n\\nif __name__ == \\\"__main__\\\":\\n    example_array = [3, 6, 8, 10, 1, 2, 1]\\n    print(\\\"Original array:\\\", example_array)\\n    sorted_array = quick_sort(example_array)\\n    print(\\\"Sorted array:\\\", sorted_array)", "path": "quick_sort.py"}
Tool returns: Success: File 'quick_sort.py' has been written.
Final model response: The Python code file `quick_sort.py` for quick sort has been successfully created for you. It contains a `quick_sort` function and a usage example. You can run it in your terminal or editor to test the quick sort feature.

This example shows the first step of the tool calling flow: sending a request and receiving the model’s intent to call a tool.

To use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation

curl
curl --location "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
    "model": "qwen3-coder-next",
    "input": {
        "messages": [{\
            "role": "user",\
            "content": "Write a Python quick sort script and name it quick_sort.py"\
        }]
    },
    "parameters": {
        "result_format": "message",
        "tools": [\
        {\
            "type": "function",\
            "function": {\
                "name": "write_file",\
                "description": "Writes content to a specified file. Creates the file if it does not exist.",\
                "parameters": {\
                    "type": "object",\
                    "properties": {\
                        "path": {\
                            "type": "string",\
                            "description": "The relative or absolute path of the object file"\
                        },\
                        "content": {\
                            "type": "string",\
                            "description": "The string content to write to the file"\
                        }\
                    },\
                    "required": ["path", "content"]\
                }\
            }\
        }\
    ]
    }
}'

Response

json
{
    "output": {
        "choices": [\
            {\
                "finish_reason": "tool_calls",\
                "message": {\
                    "role": "assistant",\
                    "tool_calls": [\
                        {\
                            "function": {\
                                "name": "write_file",\
                                "arguments": "{\"content\": \"def quick_sort(arr):\\\\n    if len(arr) <= 1:\\\\n        return arr\\\\n    pivot = arr[len(arr) // 2]\\\\n    left = [x for x in arr if x < pivot]\\\\n    middle = [x for x in arr if x == pivot]\\\\n    right = [x for x in arr if x > pivot]\\\\n    return quick_sort(left) + middle + quick_sort(right)\\\\n\\\\nif __name__ == \\\\\\\"__main__\\\\\\\":\\\\n    example_list = [3, 6, 8, 10, 1, 2, 1]\\\\n    print(\\\\\\\"Original list:\\\\\\\", example_list)\\\\n    sorted_list = quick_sort(example_list)\\\\n    print(\\\\\\\"Sorted list:\\\\\\\", sorted_list), \"path\": \"quick_sort.py\"}"\
                            },\
                            "index": 0,\
                            "id": "call_645b149bbd274e8bb3789aae",\
                            "type": "function"\
                        }\
                    ],\
                    "content": ""\
                }\
            }\
        ]
    },
    "usage": {
        "total_tokens": 684,
        "output_tokens": 193,
        "input_tokens": 491,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    },
    "request_id": "d2386acd-fce3-9d0f-8015-c5f3a8bf9f5c"
}

Code completion

Qwen-Coder supports two code completion methods. Choose the one that best fits your needs:

  • Partial mode: This method works with all Qwen-Coder models and regions. It supports prefix-based completion, is simple to implement, and is recommended for most use cases.

  • Completions API: This method supports only the qwen2.5-coder series models in the Chinese mainland (Beijing) region. It supports both prefix-based and fill-in-the-middle completion.

Partial mode

This feature lets the model automatically complete the rest of your code based on the part you have already written—the prefix.

To use this feature, add a message with the role set to assistant to the messages list and set partial: true. The assistant message's content is the code prefix you provide. For more information, see Partial mode.

OpenAI compatible

DashScope

Python

Node.js

curl

Request example

python
import os
from openai import OpenAI

client = OpenAI(
    # If you have not configured the environment variable, replace the following line with your Alibaba Cloud Model Studio API key: api_key="sk-xxx",
    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-coder-next",
    messages=[{\
        "role": "user",\
        "content": "Please write a Python script to generate prime numbers up to 100. Do not output non-code content or Markdown code blocks."\
    },\
    {\
        "role": "assistant",\
        "content": "def generate_prime_number",\
        "partial": True\
    }]
    )
print(completion.choices[0].message.content)

Result

plaintext
(n):
    primes = []
    for i in range(2, n+1):
        is_prime = True
        for j in range(2, int(i**0.5)+1):
            if i % j == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(i)
    return primes

prime_numbers = generate_prime_number(100)
print(prime_numbers)

Request example

nodejs
import OpenAI from "openai";

const client = new OpenAI(
    {
        // If you have not configured the environment variable, replace the following line with your Model Studio API key: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
    }
);

async function main() {
    const completion = await client.chat.completions.create({
        model: "qwen3-coder-next",
        messages: [\
            { role: "user", content: "Please write a Python script to generate prime numbers up to 100. Do not output non-code content or Markdown code blocks." },\
            { role: "assistant", content: "def generate_prime_number", partial: true}\
        ],
    });
    console.log(completion.choices[0].message.content);
}

main();

Result

plaintext
(n):
    primes = []
    for i in range(2, n+1):
        is_prime = True
        for j in range(2, int(i**0.5)+1):
            if i % j == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(i)
    return primes

prime_numbers = generate_prime_number(100)
print(prime_numbers)

Request example

To use models in the Beijing region, change the URL to: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions

curl
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-coder-next",
    "messages": [{\
        "role": "user",\
        "content": "Please write a Python script to generate prime numbers up to 100. Do not output non-code content or Markdown code blocks."\
    },\
    {\
        "role": "assistant",\
        "content": "def generate_prime_number",\
        "partial": true\
    }]
}'

Result

json
{
    "choices": [\
        {\
            "message": {\
                "content": "(n):\n    primes = []\n    for num in range(2, n + 1):\n        is_prime = True\n        for i in range(2, int(num ** 0.5) + 1):\n            if num % i == 0:\n                is_prime = False\n                break\n        if is_prime:\n            primes.append(num)\n    return primes\n\nprime_numbers = generate_prime_number(100)\nprint(prime_numbers)",\
                "role": "assistant"\
            },\
            "finish_reason": "stop",\
            "index": 0,\
            "logprobs": null\
        }\
    ],
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 38,
        "completion_tokens": 93,
        "total_tokens": 131,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    },
    "created": 1761634556,
    "system_fingerprint": null,
    "model": "qwen3-coder-next",
    "id": "chatcmpl-c108050a-bb6d-4423-9d36-f64aa6a32976"
}

Python

curl

Request example

python
from http import HTTPStatus
import dashscope
import os

dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'

messages = [{\
    "role": "user",\
    "content": "Please write a Python script to generate prime numbers up to 100. Do not output non-code content or Markdown code blocks."\
},\
{\
    "role": "assistant",\
    "content": "def generate_prime_number",\
    "partial": True\
}]
response = dashscope.Generation.call(
    # If you have not configured the environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    model='qwen3-coder-next',
    messages=messages,
    result_format='message',
)
if response.status_code == HTTPStatus.OK:
    print(response.output.choices[0].message.content)
else:
    print(f"HTTP return code: {response.status_code}")
    print(f"Error code: {response.code}")
    print(f"Error message: {response.message}")

Result

plaintext
(n):
    primes = []
    for i in range(2, n+1):
        is_prime = True
        for j in range(2, int(i**0.5)+1):
            if i % j == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(i)
    return primes

prime_numbers = generate_prime_number(100)
print(prime_numbers)

Request example

To use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation

curl
curl -X POST "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-coder-next",
    "input":{
        "messages":[{\
            "role": "user",\
            "content": "Please write a Python script to generate prime numbers up to 100. Do not output non-code content or Markdown code blocks."\
        },\
        {\
            "role": "assistant",\
            "content": "def generate_prime_number",\
            "partial": true\
        }]
    },
    "parameters": {
        "result_format": "message"
    }
}'

Result

json
{
    "output": {
        "choices": [\
            {\
                "message": {\
                    "content": "(n):\n    prime_list = []\n    for i in range(2, n+1):\n        is_prime = True\n        for j in range(2, int(i**0.5)+1):\n            if i % j == 0:\n                is_prime = False\n                break\n        if is_prime:\n            prime_list.append(i)\n    return prime_list\n\nprime_numbers = generate_prime_number(100)\nprint(prime_numbers)",\
                    "role": "assistant"\
                },\
                "finish_reason": "stop"\
            }\
        ]
    },
    "usage": {
        "total_tokens": 131,
        "output_tokens": 92,
        "input_tokens": 39,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    },
    "request_id": "9917f629-e819-4519-af44-b0e677e94b2c"
}

Completions API

Important

The Completions API applies only to models in the Chinese mainland (Beijing) region. You must use an API key from the Chinese mainland (Beijing) region.

Supported models:

qwen-coder-turbo

The Completions API uses special fim (Fill-in-the-Middle) tokens in the prompt to guide the model to perform completion.

Prefix-based completion

Prefix- and suffix-based completion

Prompt template:

plaintext
<|fim_prefix|>{prefix_content}<|fim_suffix|>
  • <|fim_prefix|> and <|fim_suffix|> are special tokens that guide the model to complete the text. Do not modify them.

  • Replace {prefix_content} with the prefix information, such as the function name, input parameters, and usage instructions.

Python

Node.js

curl

Python

python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    api_key=os.getenv("DASHSCOPE_API_KEY")
)

completion = client.completions.create(
  model="qwen-coder-turbo",
  prompt="<|fim_prefix|>def quick_sort(arr):<|fim_suffix|>",
)

print(completion.choices[0].text)

Node.js

nodejs
import OpenAI from "openai";

const client = new OpenAI(
    {
        // If you have not configured the environment variable, replace the following line with your Alibaba Cloud Model Studio API key: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1"
    }
);

async function main() {
    const completion = await client.completions.create({
        model: "qwen-coder-turbo",
        prompt: "<|fim_prefix|>def quick_sort(arr):<|fim_suffix|>",
    });
    console.log(completion.choices[0].text)
}

main();

curl

curl
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-coder-turbo",
    "prompt": "<|fim_prefix|>def quick_sort(arr):<|fim_suffix|>"
}'

Prompt template:

plaintext
<|fim_prefix|>{prefix_content}<|fim_suffix|>{suffix_content}<|fim_middle|>
  • <|fim_prefix|>, <|fim_suffix|>, and <|fim_middle|> are special tokens that guide the model to complete the text. Do not modify them.

  • Replace {prefix_content} with the prefix information, such as the function name, input parameters, and usage instructions.

  • Replace {suffix_content} with the suffix information, such as the function’s return parameters.

Python

Node.js

curl

Python

python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    api_key=os.getenv("DASHSCOPE_API_KEY")
)

prefix_content = """def reverse_words_with_special_chars(s):
'''
Reverses each word in a string, preserving the position of non-alphabetic characters and word order.
    Example:
    reverse_words_with_special_chars("Hello, world!") -> "olleH, dlrow!"
    Parameters:
        s (str): The input string, which may contain punctuation.
    Returns:
        str: The processed string with words reversed but non-alphabetic characters in their original positions.
'''
"""

suffix_content = "return result"

completion = client.completions.create(
  model="qwen-coder-turbo",
  prompt=f"<|fim_prefix|>{prefix_content}<|fim_suffix|>{suffix_content}<|fim_middle|>",
)

print(completion.choices[0].text)

Node.js

nodejs
import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
  apiKey: process.env.DASHSCOPE_API_KEY
});

const prefixContent = `def reverse_words_with_special_chars(s):
'''
Reverses each word in a string, preserving the position of non-alphabetic characters and word order.
    Example:
    reverse_words_with_special_chars("Hello, world!") -> "olleH, dlrow!"
    Parameters:
        s (str): The input string, which may contain punctuation.
    Returns:
        str: The processed string with words reversed but non-alphabetic characters in their original positions.
'''
`;

const suffixContent = "return result";

async function main() {
  const completion = await client.completions.create({
    model: "qwen-coder-turbo",
    prompt: `<|fim_prefix|>${prefixContent}<|fim_suffix|>${suffixContent}<|fim_middle|>`
  });

  console.log(completion.choices[0].text);
}

main();

curl

curl
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-coder-turbo",
    "prompt": "<|fim_prefix|>def reverse_words_with_special_chars(s):\n\"\"\"\nReverses each word in a string, preserving the position of non-alphabetic characters and word order.\n    Example:\n    reverse_words_with_special_chars(\"Hello, world!\") -> \"olleH, dlrow!\"\n    Parameters:\n        s (str): The input string, which may contain punctuation.\n    Returns:\n        str: The processed string with words reversed but non-alphabetic characters in their original positions.\n\"\"\"\n<|fim_suffix|>return result<|fim_middle|>"
}'

Going live

To optimize Qwen-Coder usage efficiency and reduce costs, follow these recommendations:

  • Enable streaming output: Setting stream=True streams intermediate results, which reduces the risk of timeouts and improves the user experience.

  • Lower the temperature parameter: Code generation typically requires deterministic and accurate output. Reduce the temperature parameter to minimize randomness in generated code.

  • Use models that support context caching: In scenarios involving large amounts of repeated prefixes—such as code completion and code review—use models that support context caching. This significantly reduces overhead.

  • Limit the number of tools: To ensure efficient and cost-effective model calls, pass no more than 20 tools in a single request using the tools parameter. Including many tool descriptions consumes excessive input tokens, increasing costs, slowing response times, and reducing the model’s ability to select the correct tool. For more information, see Function Calling.

Billing and rate limiting

  • Basic billing: You are billed based on the number of input Tokens and output Tokens for each request. Unit prices vary by model. For detailed pricing, see the Model List.

  • Special billing items:

    • Tiered billing: The qwen3-coder series uses tiered billing. When the number of input tokens for a single request reaches a specific tier, all input and output tokens for that request are billed at the unit price for that tier.

    • Tool calling (Function Calling): When using tool calling, the tool descriptions defined in the tools parameter count toward the total Token count and incur charges.

  • Rate limiting: API calls are subject to dual limits: requests per minute (RPM) and tokens per minute (TPM). For more information, see or Rate limiting.

  • Free quota**(Singapore region only)**: The free quota is valid for 90 days, starting from the date Alibaba Cloud Model Studio is activated or your model request is approved. During this period, each Qwen-Coder model provides a free quota for new users of 1 million tokens.

API reference

For information about the input and output parameters of the Qwen code model, see Qwen.

FAQ

Why do developer tools such as Qwen Code and Claude Code use many tokens?

When an external developer tool calls the Qwen-Coder model to process issues, it may invoke the API multiple times, consuming many tokens. For guidance on monitoring and reducing token usage, see the Qwen Code and Claude Code documentation. Enable the Stop When Free Quota Is Exhausted feature to avoid unexpected charges after your free quota is used up.

You can also purchase an AI coding package, which offers a fixed monthly request quota for a flat fee and works with AI tools. For more information, see Coding Plan Overview.

How do I view model call volume?

One hour after you call a model, go to the Monitoring ( Singapore or Beijing) page. Set the query conditions, such as the time range and workspace. Then, in the Models area, find the target model and click Monitor in the Actions column to view the model's call statistics. For more information, see the Monitoring document.

Data is updated hourly. During peak periods, there may be an hour-level latency.

How can I make the model output only code without any explanatory text?

You can achieve this using the following methods:

  1. Prompt constraints: Provide clear instructions in the prompt. For example: “Return only code. Do not include any explanations, comments, or markdown tags.”

  2. Set astop sequence: Use phrases such as stop=["\n# Explanation:", "Description", "Explanation:", "Note:"] to stop the model before it starts generating explanatory text. For more information, see Qwen API Reference.

How do I use the 1,000 free calls per day for Qwen Code?

This quota is available exclusively for the Qwen Code tool and must be used through that tool. It is calculated separately from the free quota for new users you receive when you activate Alibaba Cloud Model Studio. These quotas do not conflict. For more information, see How do I use the 1,000 free calls per day?

Previous: Long context (Qwen-Long)Next: Machine translation (Qwen-MT)

Is this page helpful?

QuickStart

Model Selection

Core capabilities

Tool calling

Code completion

Going live

Billing and rate limiting

API reference

FAQ

Why do developer tools such as Qwen Code and Claude Code use many tokens?

How do I view model call volume?

How can I make the model output only code without any explanatory text?

How do I use the 1,000 free calls per day for Qwen Code?

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 >

Chat now with Alibaba Cloud Customer Service to assist you in finding the right products and services to meet your needs.

\ \ 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

© 2009-2026 Copyright by Alibaba Cloud All rights reserved

Careers About Us Privacy Policy Legal Integrity Compliance Reporting Channel Service Notices Links