Qwen-Coder is a language model designed for code-related tasks. You can use an API to call the model to generate code, complete code, and interact with external systems through function calling.
Getting started
Prerequisites
You have created an API key and exported it as an environment variable.
If you use an SDK to make calls, you must install the SDK. The DashScope Python SDK version must be 1.24.6 or later, and the DashScope Java SDK version must be 2.21.10 or later.
This topic provides an example of how to call the Qwen-Coder model to write a Python function that finds prime numbers.
OpenAI compatible
Python
Request example
import os
from openai import OpenAI
client = OpenAI(
# API keys for the Singapore and Beijing regions are different. 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 next line with your Model Studio API key: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/compatible-mode/v1
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
# This example uses qwen3-coder-plus. You can replace it with another model name as needed.
model="qwen3-coder-plus",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': 'Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks.'}],
)
print(completion.choices[0].message.content)
Response
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 primesNode.js
Request example
import OpenAI from "openai";
const client = new OpenAI(
{
// API keys for the Singapore and Beijing regions are different. 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 next line with your Model Studio API key: apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
// The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/compatible-mode/v1
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
}
);
async function main() {
const completion = await client.chat.completions.create({
model: "qwen3-coder-plus", //This example uses qwen3-coder-plus. You can replace it with another model name as needed.
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks." }
],
});
console.log(completion.choices[0].message.content);
}
main();Response
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 primescurl
Request example
The following URL is for the Singapore region. If you use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder-plus",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks."
}
]
}'Response
{
"choices": [
{
"message": {
"content": "def find_prime_numbers(n):\n if n <= 2:\n return []\n \n primes = []\n \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",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 96,
"completion_tokens": 90,
"total_tokens": 186,
"prompt_tokens_details": {
"cached_tokens": 0
}
},
"created": 1761615592,
"system_fingerprint": null,
"model": "qwen3-coder-plus",
"id": "chatcmpl-3de690bd-ae7f-461d-8eb6-d65b0577e803"
}DashScope
Python
Request example
import dashscope
import os
# The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages = [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks."
}
]
response = dashscope.Generation.call(
# API keys for the Singapore and Beijing regions are different. 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 next line with your Model Studio API key: api_key = "sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# This example uses qwen3-coder-plus. You can replace it with another model name as needed.
model="qwen3-coder-plus",
messages=messages,
result_format="message"
)
if response.status_code == 200:
print(response.output.choices[0].message.content)
else:
print(f"HTTP status code: {response.status_code}")
print(f"Error code: {response.code}")
print(f"Error message: {response.message}")Result
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 primesJava
Request example
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");
// The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/api/v1
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.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 named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks.").build();
// This example uses qwen3-coder-plus. You can replace it with another model name as needed.
GenerationParam param = GenerationParam.builder()
.apiKey(apiKey)
.model("qwen3-coder-plus")
.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();
}
}
}Result
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 primescurl
Request example
The following URL is for the Singapore region. If you use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
curl -X POST "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder-plus",
"input":{
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks."
}
]
},
"parameters": {
"result_format": "message"
}
}'Response
{
"output": {
"choices": [
{
"message": {
"content": "def find_prime_numbers(n):\n if n <= 2:\n return []\n \n primes = []\n \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": 186,
"output_tokens": 90,
"input_tokens": 96,
"prompt_tokens_details": {
"cached_tokens": 0
}
},
"request_id": "b1b8d1f8-0d26-4651-a466-66eefa0e7c51"
}Model selection
Qwen-Coder has been upgraded to the Qwen3 series. It supports a context window of up to 1 million tokens and offers multiple models to meet different requirements for performance, response speed, and cost in various scenarios.
Recommendations:
qwen3-coder-plus: This model offers the strongest coding capabilities and is suitable for high-quality tasks such as generating complex projects and performing in-depth code reviews.qwen3-coder-flash: This is a cost-effective model that balances performance and cost, offering faster speed at a lower price. It is suitable for scenarios that are sensitive to response speed.
For more information about model names, context windows, prices, and snapshot versions, see the Model list. For more information about concurrent request limits, see Rate limits.
Core capabilities
Streaming
To enhance the interactive experience and reduce the risk of timeouts for long-running requests, enable streaming output by setting the stream=True parameter. The model will then return the generated content in chunks as it is created, instead of waiting to return the entire response at once.
OpenAI compatible
Python
Request example
import os
from openai import OpenAI
client = OpenAI(
# API keys for the Singapore and Beijing regions are different. 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 next line with your Model Studio API key: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/compatible-mode/v1
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3-coder-plus",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks."}
],
stream=True,
# Get the token usage for this request from the last chunk
stream_options={"include_usage": True}
)
content_parts = []
print("="*20+"Response"+"="*20)
for chunk in completion:
# The last chunk does not contain choices, but it contains usage information.
if chunk.choices:
# delta.content might be None. Use 'or ""' to avoid errors during concatenation.
content = chunk.choices[0].delta.content or ""
print(content, end="", flush=True)
content_parts.append(content)
elif chunk.usage:
print("\n"+"="*20+"Token Usage"+"="*20)
print(f"Input Tokens: {chunk.usage.prompt_tokens}")
print(f"Output Tokens: {chunk.usage.completion_tokens}")
print(f"Total Tokens: {chunk.usage.total_tokens}")
full_response = "".join(content_parts)
# To get the full response string, uncomment the next line.
# print(f"\n--- Full Response ---\n{full_response}")Response
====================Response====================
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
====================Token Usage====================
Input Tokens: 66
Output Tokens: 89
Total Tokens: 155Node.js
Request example
import OpenAI from "openai";
const client = new OpenAI(
{
// API keys for the Singapore and Beijing regions are different. 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 next line with your Model Studio API key: apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
// The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/compatible-mode/v1
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
}
);
async function main() {
const stream = await client.chat.completions.create({
model: "qwen3-coder-plus",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks." },
],
stream: true,
// Get the token usage for this request from the last chunk
stream_options: { include_usage: true },
});
const contentParts = [];
console.log("=".repeat(20) + "Response" + "=".repeat(20));
for await (const chunk of stream) {
// The last chunk does not contain choices, but it contains usage information.
if (chunk.choices && chunk.choices.length > 0) {
const content = chunk.choices[0]?.delta?.content || "";
process.stdout.write(content);
contentParts.push(content);
} else if (chunk.usage) {
// The request is complete. Print the token usage.
console.log("\n"+"=".repeat(20) + "Token Usage" + "=".repeat(20));
console.log(`Input Tokens: ${chunk.usage.prompt_tokens}`);
console.log(`Output Tokens: ${chunk.usage.completion_tokens}`);
console.log(`Total Tokens: ${chunk.usage.total_tokens}`);
}
}
const fullResponse = contentParts.join("");
// To get the full response string, uncomment the next line.
// console.log(`\n--- Full Response ---\n${fullResponse}`);
}
main();Response
====================Response====================
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
====================Token Usage====================
Input Tokens: 66
Output Tokens: 89
Total Tokens: 155curl
Request example
The following URL is for the Singapore region. If you use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
--no-buffer \
-d '{
"model": "qwen3-coder-plus",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks."}
],
"stream": true,
"stream_options": {"include_usage": true}
}'Response
data: {"choices":[{"delta":{"content":"","role":"assistant"},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1763085409,"system_fingerprint":null,"model":"qwen3-coder-plus","id":"chatcmpl-61f94113-f29b-4f7d-9730-551749d40ef4"}
data: {"choices":[{"finish_reason":null,"logprobs":null,"delta":{"content":"def"},"index":0}],"object":"chat.completion.chunk","usage":null,"created":1763085409,"system_fingerprint":null,"model":"qwen3-coder-plus","id":"chatcmpl-61f94113-f29b-4f7d-9730-551749d40ef4"}
data: {"choices":[{"delta":{"content":" find_prime_numbers(n"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1763085409,"system_fingerprint":null,"model":"qwen3-coder-plus","id":"chatcmpl-61f94113-f29b-4f7d-9730-551749d40ef4"}
......
data: {"choices":[{"delta":{"content":" primes"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1763085409,"system_fingerprint":null,"model":"qwen3-coder-plus","id":"chatcmpl-61f94113-f29b-4f7d-9730-551749d40ef4"}
data: {"choices":[{"finish_reason":"stop","delta":{"content":""},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1763085409,"system_fingerprint":null,"model":"qwen3-coder-plus","id":"chatcmpl-61f94113-f29b-4f7d-9730-551749d40ef4"}
data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":66,"completion_tokens":89,"total_tokens":155,"prompt_tokens_details":{"cached_tokens":0}},"created":1763085409,"system_fingerprint":null,"model":"qwen3-coder-plus","id":"chatcmpl-61f94113-f29b-4f7d-9730-551749d40ef4"}
data: [DONE]DashScope
Python
Request example
import os
from http import HTTPStatus
import dashscope
from dashscope import Generation
# The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks."},
]
responses = Generation.call(
# API keys for the Singapore and Beijing regions are different. 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 next line with your Model Studio API key: api_key="sk-xxx",
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen3-coder-plus",
messages=messages,
result_format="message",
stream=True,
# Incremental output. Each data block contains only the newly generated content.
incremental_output=True,
)
content_parts = []
print("="*20+"Response"+"="*20+"\n", end="", flush=True)
for resp in responses:
if resp.status_code == HTTPStatus.OK:
content = resp.output.choices[0].message.content
print(content, end="", flush=True)
content_parts.append(content)
if resp.output.choices[0].finish_reason == "stop":
print("\n"+"=" * 20 + "Token Usage" + "=" * 20)
print(f"Input Tokens: {resp.usage.input_tokens}")
print(f"Output Tokens: {resp.usage.output_tokens}")
print(f"Total Tokens: {resp.usage.total_tokens}")
else:
print(f"HTTP status code: {resp.status_code}")
print(f"Error code: {resp.code}")
print(f"Error message: {resp.message}")
full_response = "".join(content_parts)
# To get the full response string, uncomment the next line.
# print(f"\n--- Full Response ---\n{full_response}")Response
====================Response====================
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
====================Token Usage====================
Input Tokens: 66
Output Tokens: 89
Total Tokens: 155Java
Request example
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 io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import com.alibaba.dashscope.protocol.Protocol;
public class Main {
public static void main(String[] args) {
String apiKey = System.getenv("DASHSCOPE_API_KEY");
// The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/api/v1
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
CountDownLatch latch = new CountDownLatch(1);
GenerationParam param = GenerationParam.builder()
.apiKey(apiKey)
.model("qwen3-coder-plus")
.messages(Arrays.asList(
Message.builder()
.role(Role.USER.getValue())
.content("Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks.")
.build()
))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.incrementalOutput(true) // Enable incremental output for streaming, where each data block contains only the newly generated content.
.build();
try {
Flowable<GenerationResult> result = gen.streamCall(param);
StringBuilder fullContent = new StringBuilder();
System.out.println("====================Response====================");
result
.subscribeOn(Schedulers.io()) // Execute the request on an I/O thread.
.observeOn(Schedulers.computation()) // Process the response on a computation thread.
.subscribe(
// onNext: Process each response chunk.
message -> {
String content = message.getOutput().getChoices().get(0).getMessage().getContent();
String finishReason = message.getOutput().getChoices().get(0).getFinishReason();
// Output the content.
System.out.print(content);
fullContent.append(content);
// When finishReason is not null, it indicates the last chunk. Output the usage information.
if (finishReason != null && !"null".equals(finishReason)) {
System.out.println("\n====================Token Usage====================");
System.out.println("Input Tokens: " + message.getUsage().getInputTokens());
System.out.println("Output Tokens: " + message.getUsage().getOutputTokens());
System.out.println("Total Tokens: " + message.getUsage().getTotalTokens());
}
System.out.flush(); // Flush the output immediately.
},
// onError: Handle errors.
error -> {
System.err.println("\nRequest failed: " + error.getMessage());
latch.countDown();
},
// onComplete: Completion callback.
() -> {
System.out.println(); // Add a new line.
// To get the full response string, uncomment the next line.
// System.out.println("Full response: " + fullContent.toString());
latch.countDown();
}
);
// Wait for the asynchronous task to complete in the main thread.
latch.await();
} catch (Exception e) {
System.err.println("Request exception: " + e.getMessage());
e.printStackTrace();
}
}
}Response
====================Response====================
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
====================Token Usage====================
Input Tokens: 66
Output Tokens: 89
Total Tokens: 155curl
Request example
The following URL is for the Singapore region. If you use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-SSE: enable" \
-d '{
"model": "qwen3-coder-plus",
"input":{
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Write a Python function named find_prime_numbers that accepts an integer n as a parameter and returns a list of all prime numbers less than n. Do not output any content other than the code and do not use Markdown code blocks."
}
]
},
"parameters": {
"result_format": "message",
"incremental_output":true
}
}'Return value
id:1
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"def","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":67,"output_tokens":1,"input_tokens":66,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"fadfc21b-4411-40d5-b143-8c3573284c42"}
id:2
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":" find_prime_numbers(n","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":71,"output_tokens":5,"input_tokens":66,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"fadfc21b-4411-40d5-b143-8c3573284c42"}
id:3
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"):\n if n","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":75,"output_tokens":9,"input_tokens":66,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"fadfc21b-4411-40d5-b143-8c3573284c42"}
...
id:26
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":" primes","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":155,"output_tokens":89,"input_tokens":66,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"fadfc21b-4411-40d5-b143-8c3573284c42"}
id:27
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"","role":"assistant"},"finish_reason":"stop"}]},"usage":{"total_tokens":155,"output_tokens":89,"input_tokens":66,"prompt_tokens_details":{"cached_tokens":0}},"request_id":"fadfc21b-4411-40d5-b143-8c3573284c42"}Function calling
To enable the model to interact with the external environment, such as reading and writing files, calling APIs, and interacting with databases, you can provide it with a set of tools. The model will decide whether and how to call these tools based on your instructions. For more information, see Function calling.
The complete tool calling process includes the following steps:
Define tools and make a request: Define a list of tools in your request and ask the model to perform a task that requires these tools.
Execute the tools: Parse the
tool_callsfrom the model's response and call the corresponding local tool functions to perform the task.Return the execution result: Package the tool's execution result into the required format and send it back to the model. This allows the model to complete the final task based on the result.
The following example shows how to guide the model to generate code and use the write_file tool to save it to a local file.
OpenAI compatible
Python
Request example
import os
import json
from openai import OpenAI
client = OpenAI(
# API keys for the Singapore and Beijing regions are different. 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 next line with your Model Studio API key: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/compatible-mode/v1
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
tools = [
{
"type": "function",
"function": {
"name": "write_file",
"description": "Writes content to the specified file. If the file does not exist, it is created.",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The relative or absolute path of the target 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:
"""Write file content"""
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: The 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 script for quick sort and name it quick_sort.py."}]
completion = client.chat.completions.create(
model="qwen3-coder-plus",
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, print the content directly.
if assistant_output.tool_calls is None:
print(f"No tool call needed, responding directly: {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}] with 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 returned: {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-plus",
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"Model's final response: {assistant_output.content}")
Response
Calling tool [write_file] with 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 returned: Success: The file 'quick_sort.py' has been written.
Model's final response: Okay, I have created the file named `quick_sort.py` for you, which contains the Python implementation of quick sort. You can run this file to see the example output. Let me know if you need any further modifications or explanations!Node.js
Request example
import OpenAI from "openai";
import fs from "fs/promises";
import path from "path";
const client = new OpenAI({
// API keys for the Singapore and Beijing regions are different. 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 next line with your Model Studio API key: apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
// The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/compatible-mode/v1
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
});
const tools = [
{
"type": "function",
"function": {
"name": "write_file",
"description": "Writes content to the specified file. If the file does not exist, it is created.",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The relative or absolute path of the target 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 code 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: The 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 script for quick sort and name it quick_sort.py."}];
async function main() {
const completion = await client.chat.completions.create({
model: "qwen3-coder-plus",
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, print the content directly.
if (!assistant_output.tool_calls) {
console.log(`No tool call needed, responding directly: ${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}] with 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 returned: ${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-plus",
messages: messages,
tools: tools
});
assistant_output = response.choices[0].message;
if (!assistant_output.content) assistant_output.content = "";
messages.push(assistant_output);
}
console.log(`Model's final response: ${assistant_output.content}`);
}
}
main();
Response
Calling tool [write_file] with 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 returned: Success: The file 'quick_sort.py' has been written.
Model's final response: The `quick_sort.py` file has been successfully created with the Python implementation of quick sort. You can run the file to see the sorting result for the example list. Let me know if you need any further modifications or explanations!curl
Request example
This example shows the first step of the tool calling process: making a request and obtaining the model's intent to call a tool.
The following URL is for the Singapore region. If you use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder-plus",
"messages": [
{
"role": "user",
"content": "Write a Python script for quick sort and name it quick_sort.py."
}
],
"tools": [
{
"type": "function",
"function": {
"name": "write_file",
"description": "Writes content to the specified file. If the file does not exist, it is created.",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The relative or absolute path of the target file."
},
"content": {
"type": "string",
"description": "The string content to write to the file."
}
},
"required": ["path", "content"]
}
}
}
]
}'Response
{
"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-plus",
"id": "chatcmpl-20e96159-beea-451f-b3a4-d13b218112b5"
}DashScope
Python
Request example
import os
import json
import dashscope
# The following base_url is for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
tools = [
{
"type": "function",
"function": {
"name": "write_file",
"description": "Writes content to the specified file. If the file does not exist, it is created.",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The relative or absolute path of the target 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:
"""Write file content"""
try:
# For security reasons, the file writing feature is disabled by default. To use it, uncomment the code 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: The 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 script for quick sort and name it quick_sort.py."}]
response = dashscope.Generation.call(
# If you have not configured an environment variable, replace the next line with your Model Studio API key: api_key="sk-xxx",
api_key=os.getenv('DASHSCOPE_API_KEY'),
model='qwen3-coder-plus',
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, print the content directly.
if "tool_calls" not in assistant_output or not assistant_output["tool_calls"]:
print(f"No tool call needed, responding directly: {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}] with 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 returned: {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-plus',
messages=messages,
tools=tools,
result_format='message'
)
if response.status_code == 200:
print(f"Model's final 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: {response}")
break
else:
print(f"Execution error: {response}")
Response
Calling tool [write_file] with 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 returned: Success: The file 'quick_sort.py' has been written.
Model's final response: The `quick_sort.py` file has been successfully created with the Python implementation of quick sort. You can run the file to see the sorting result for the example list. Let me know if you need any further modifications or explanations!Java
Request example
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 {
/**
* Write file content
* @param arguments A JSON string passed by the model, containing the parameters required by the tool.
* @return A string with the result of the tool execution.
*/
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 code 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: The 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 target 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 the specified file. If the file does not exist, it is created.")
.parameters(JsonUtils.parseString(writePropertyParams).getAsJsonObject())
.build();
// The following is the base_url for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/api/v1
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
String userInput = "Write a Python script for quick sort 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-plus")
.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, print the content directly.
if (assistantOutput.getToolCalls() == null || assistantOutput.getToolCalls().isEmpty()) {
System.out.println("No tool call needed, responding directly: " + 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 + "] with 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 returned: " + 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("Model's final response: " + assistantOutput.getContent());
}
} catch (NoApiKeyException | InputRequiredException e) {
System.err.println("Error: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Response
Calling tool [write_file] with 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 returned: Success: The file 'quick_sort.py' has been written.
Model's final response: I have successfully created the Python code file `quick_sort.py` for you. This file contains a `quick_sort` function and an example of its usage. You can run it in your terminal or editor to test the quick sort functionality.curl
Request example
This example shows the first step of the tool calling process: making a request and obtaining the model's intent to call a tool.
The following URL is for the Singapore region. If you use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
curl --location "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen3-coder-plus",
"input": {
"messages": [{
"role": "user",
"content": "Write a Python script for quick sort and name it quick_sort.py."
}]
},
"parameters": {
"result_format": "message",
"tools": [
{
"type": "function",
"function": {
"name": "write_file",
"description": "Writes content to the specified file. If the file does not exist, it is created.",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The relative or absolute path of the target file."
},
"content": {
"type": "string",
"description": "The string content to write to the file."
}
},
"required": ["path", "content"]
}
}
}
]
}
}'Response
{
"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 methods for code completion. You can choose one based on your needs:
Partial mode: This method is recommended. It is simple to implement, supports prefix completion, and is available for all Qwen-Coder models and regions.
Completions API: This API supports only the
qwen2.5-codermodel series in the China (Beijing) region. It supports prefix completion and fill-in-the-middle completion.
Partial mode
This feature allows the model to automatically complete the rest of your code based on a prefix you provide.
You can enable this feature by adding a message with the role set to assistant to the messages list and setting partial: true. The content of the assistant message is the code prefix that you provide. For more information, see Partial mode.
OpenAI compatible
Python
Request example
import os
from openai import OpenAI
client = OpenAI(
# If you have not configured an environment variable, replace the next 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. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/compatible-mode/v1
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3-coder-plus",
messages=[{
"role": "user",
"content": "Help me 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)
Response
(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)Node.js
Request example
import OpenAI from "openai";
const client = new OpenAI(
{
// If you have not configured an environment variable, replace the next line with your Model Studio API key: apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
// The following is the base_url for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/compatible-mode/v1
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
}
);
async function main() {
const completion = await client.chat.completions.create({
model: "qwen3-coder-plus",
messages: [
{ role: "user", content: "Help me 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
(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)curl
Request example
The following URL is for the Singapore region. If you use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder-plus",
"messages": [{
"role": "user",
"content": "Help me 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
{
"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-plus",
"id": "chatcmpl-c108050a-bb6d-4423-9d36-f64aa6a32976"
}DashScope
Python
Request example
from http import HTTPStatus
import dashscope
import os
# The following is the base_url for the Singapore region. If you use a model in the Beijing region, replace the base_url with: https://dashscope.aliyuncs.com/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages = [{
"role": "user",
"content": "Help me 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 an environment variable, replace the next line with your Model Studio API key: api_key="sk-xxx",
api_key=os.getenv('DASHSCOPE_API_KEY'),
model='qwen3-coder-plus',
messages=messages,
result_format='message',
)
if response.status_code == HTTPStatus.OK:
print(response.output.choices[0].message.content)
else:
print(f"HTTP status code: {response.status_code}")
print(f"Error code: {response.code}")
print(f"Error message: {response.message}")
Response
(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)curl
Request example
The following URL is for the Singapore region. If you use a model in the Beijing region, replace the URL with: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
curl -X POST "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder-plus",
"input":{
"messages":[{
"role": "user",
"content": "Help me 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"
}
}'Response
{
"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
The Completions API applies only to models in the China (Beijing) region and requires an API key for the China (Beijing) region.
Supported models:
qwen2.5-coder-0.5b-instruct, qwen2.5-coder-1.5b-instruct, qwen2.5-coder-3b-instruct, qwen2.5-coder-7b-instruct, qwen2.5-coder-14b-instruct, qwen2.5-coder-32b-instruct, qwen-coder-turbo-0919, qwen-coder-turbo-latest, and qwen-coder-turbo
The Completions API uses the special fim (Fill-in-the-Middle) tag in the prompt to instruct the model to perform completion.
Prefix completion
Prompt template:
<|fim_prefix|>{prefix_content}<|fim_suffix|><|fim_prefix|>and<|fim_suffix|>are special tokens that instruct 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.
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="qwen2.5-coder-32b-instruct",
prompt="<|fim_prefix|>def quick_sort(arr):<|fim_suffix|>",
)
print(completion.choices[0].text)import OpenAI from "openai";
const client = new OpenAI(
{
// If you have not configured an environment variable, replace the next line with your 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: "qwen2.5-coder-32b-instruct",
prompt: "<|fim_prefix|>def quick_sort(arr):<|fim_suffix|>",
});
console.log(completion.choices[0].text)
}
main();curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen2.5-coder-32b-instruct",
"prompt": "<|fim_prefix|>def quick_sort(arr):<|fim_suffix|>"
}'Prefix and suffix completion
Prompt template:
<|fim_prefix|>{prefix_content}<|fim_suffix|>{suffix_content}<|fim_middle|><|fim_prefix|>,<|fim_suffix|>, and<|fim_middle|>are special tokens that instruct 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.
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 while preserving the position of non-alphabetic characters and maintaining 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 reversed words, where the positions of non-alphabetic characters are unchanged.
'''
"""
suffix_content = "return result"
completion = client.completions.create(
model="qwen2.5-coder-32b-instruct",
prompt=f"<|fim_prefix|>{prefix_content}<|fim_suffix|>{suffix_content}<|fim_middle|>",
)
print(completion.choices[0].text)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 while preserving the position of non-alphabetic characters and maintaining 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 reversed words, where the positions of non-alphabetic characters are unchanged.
'''
`;
const suffixContent = "return result";
async function main() {
const completion = await client.completions.create({
model: "qwen2.5-coder-32b-instruct",
prompt: `<|fim_prefix|>${prefixContent}<|fim_suffix|>${suffixContent}<|fim_middle|>`
});
console.log(completion.choices[0].text);
}
main();curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen2.5-coder-32b-instruct",
"prompt": "<|fim_prefix|>def reverse_words_with_special_chars(s):\n\"\"\"\nReverses each word in a string while preserving the position of non-alphabetic characters and maintaining 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 reversed words, where the positions of non-alphabetic characters are unchanged.\n\"\"\"\n<|fim_suffix|>return result<|fim_middle|>"
}'Going live
To optimize the efficiency and reduce the cost of Qwen-Coder, consider the following:
Enable streaming output: Set
stream=Trueto return intermediate results in real time. This reduces the risk of timeouts and improves the user experience.Lower the temperature parameter: Code generation tasks usually require deterministic and accurate results. Set the
temperatureparameter to a value between0.1and0.3to reduce the randomness of the generated results.Use models that support context cache: For scenarios with many repeated prefixes, such as code completion and code review, use models that support context cache, such as qwen3-coder-plus and qwen3-coder-flash, to reduce overhead.
Control the number of tools: To ensure efficient and cost-effective model calls, do not pass more than 20
toolsat a time. Passing many tool descriptions consumes too many input tokens. This increases costs, reduces response speed, and makes it harder for the model to select the correct tool. For more information, see Function calling.
Billing and rate limits
Basic billing: You are charged for each request based on the number of input Tokens and output Tokens. The unit price varies depending on the model. For specific prices, see the Model list.
Special billing items:
Tiered pricing: The
qwen3-coderseries models use a tiered pricing structure. If the number of input tokens in a single request reaches a specific tier, all input and output tokens for that request are billed at the price of that tier.Context cache: For models that support context cache, such as
qwen3-coder-plusandqwen3-coder-flash, the caching mechanism can significantly reduce costs when multiple requests contain a large amount of repeated input, such as during code reviews. Input text that hits the implicit cache is billed at 20% of the standard price, and input text that hits the explicit cache is billed at 10% of the standard price. For more information, see Context cache.Function calling: When you use the tool calling feature, the tool descriptions that you define in the
toolsparameter are counted as input content and are included in the totalTokencount for billing.
Rate limits: API calls are subject to dual limits for requests per minute (RPM) and tokens per minute (TPM). For more information, see Rate limits.
Free quota (Singapore region only): The validity period begins when you activate Alibaba Cloud Model Studio or when your model request is approved. Within the 90-day validity period, each Qwen-Coder model provides 1 million tokens of free quota for new users.
API reference
For the input and output parameters of Qwen-Coder, see Qwen.
FAQ
Why are many tokens consumed when I use development tools such as Qwen Code and Claude Code?
When you use an external development tool to call the Qwen-Coder model, the tool might make multiple API calls, which can result in high token consumption. Start the tool in a specific project directory because having too many files in the startup directory, such as the root directory, increases token consumption. Enable the Free quota only feature to avoid incurring additional charges after your free quota is exhausted.
How can I view my model usage?
One hour after you call a model, go to the Model Observation (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 Model Observation 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 use the following methods:
Prompt constraint: Provide clear instructions in the prompt. For example: "Return only code. Do not include any explanations, comments, or Markdown tags."
Set a
stopsequence: Use phrases such asstop=["\n# Explanation:", "Description", "Explanation:", "Note:"]to stop the model early when it starts to generate explanatory text. For more information, see the Qwen API reference.