Overview

Updated at:

A text generation model generates text from natural language prompts for applications such as chatbots, content creation, document summarization, and code generation.

Input can range from a single keyword to complex, multi-step prompts with context. Common use cases include:

  • Content creation: Generate news articles, product descriptions, and short-form video scripts.
  • Customer service: Build 24/7 automated chatbots to answer frequently asked questions.
  • Text translation: Translate text between multiple languages.
  • Summarization: Summarize long articles, reports, and emails.
  • Legal document drafting: Draft contract templates and legal opinions.

For more examples, see Text Generation Examples.

Key concepts

The input to a text generation model is a prompt, which consists of one or more message objects each containing a role and content:

  • System message: Sets the model's persona, behavior guidelines, or task-specific instructions. Defaults to "You are a helpful assistant."
  • User message: The user's question, instruction, or input to the model.
  • Assistant message: The model's response. In a multi-turn conversation, pass historical assistant messages to maintain context.

To call the model, construct an array of these message objects named messages. A typical request consists of a system message that defines behavior guidelines and a user message with the user's input.

The system message is optional but recommended. Defining the model's role and behavioral constraints produces more consistent and predictable output.

[
    {"role": "system", "content": "You are a helpful assistant who provides precise, efficient, and insightful responses, ready to assist users with various tasks and questions."},
    {"role": "user", "content": "Who are you?"}
]

The response contains the model's reply in an assistant message.

{
    "role": "assistant",
    "content": "Hello! I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you with tasks like answering questions, creating text, logical reasoning, and coding. I understand and generate multiple languages, and can handle multi-turn conversations and complex instructions. If there is anything you need help with, just let me know!"
}

Quick start

Prerequisites: Get an API key and Configure API key as an environment variable. If using an SDK, also install the OpenAI or DashScope SDK. The {WorkspaceId} in the example base URLs is your workspace ID. For how to obtain it, see Regions and access domains.

OpenAI-compatible Chat Completions API

Python

import os
from openai import OpenAI

try:
    client = OpenAI(
        # API keys vary by region. To obtain an API key, see https://help.aliyun.com/en/model-studio/get-api-key
        # If you haven't set 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"),
        # The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
        base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
    )

    completion = client.chat.completions.create(
        model="qwen3.8-max",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Who are you?"},
        ],
    )
    print(completion.choices[0].message.content)
    # To view the full response, uncomment the following line.
    # print(completion.model_dump_json())
except Exception as e:
    print(f"Error message: {e}")
    print("For more information, see the documentation: https://help.aliyun.com/en/model-studio/developer-reference/error-code")

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

Java

// We recommend using OpenAI Java SDK v3.5.0 or later.
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

public class Main {
    public static void main(String[] args) {
        try {
            OpenAIClient client = OpenAIOkHttpClient.builder()
                    // API keys vary by region. To obtain an API key, see https://help.aliyun.com/en/model-studio/get-api-key
                    // If you haven't set the environment variable, replace the following line with your Alibaba Cloud Model Studio API key: .apiKey("sk-xxx")
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    // The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
                    .baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
                    .build();

            // Create ChatCompletion parameters.
            ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                    .model("qwen3.8-max")
                    .addSystemMessage("You are a helpful assistant.")
                    .addUserMessage("Who are you?")
                    .build();

            // Send the request and receive the response.
            ChatCompletion chatCompletion = client.chat().completions().create(params);
            String content = chatCompletion.choices().get(0).message().content().orElse("No valid content returned");
            System.out.println(content);
            // To view the full response, uncomment the following line.
            // System.out.println(chatCompletion);

        } catch (Exception e) {
            System.err.println("Error message: " + e.getMessage());
            System.out.println("For more information, see the documentation: https://help.aliyun.com/en/model-studio/developer-reference/error-code");
        }
    }
}

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

Node.js

// This code requires Node.js v18+ and must be run in an ES Module environment.
import OpenAI from "openai";

const openai = new OpenAI(
    {
        // API keys vary by region. To obtain an API key, see https://help.aliyun.com/en/model-studio/get-api-key
        // If you haven't set the environment variable, replace the following line with your Alibaba Cloud Model Studio API key: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        // The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
        baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
    }
);
const completion = await openai.chat.completions.create({
    model: "qwen3.8-max",
    messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "Who are you?" }
    ],
});
console.log(completion.choices[0].message.content);
// To view the full response, uncomment the following line.
// console.log(JSON.stringify(completion, null, 4));

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

Go

// We recommend using OpenAI Go SDK v2.4.0 or later.
package main

import (
	"context"
	// To view the full response, uncomment the import below and the related code at the end.
	// "encoding/json"
	"fmt"
	"os"

	"github.com/openai/openai-go/v2"
	"github.com/openai/openai-go/v2/option"
)

func main() {
	// API keys vary by region. To obtain an API key, see https://help.aliyun.com/en/model-studio/get-api-key
	// If you haven't set the environment variable, replace the following line with your Alibaba Cloud Model Studio API key: apiKey := "sk-xxx"
	apiKey := os.Getenv("DASHSCOPE_API_KEY")
	client := openai.NewClient(
		option.WithAPIKey(apiKey),
		// The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
		option.WithBaseURL("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"),
	)
	chatCompletion, err := client.Chat.Completions.New(
		context.TODO(), openai.ChatCompletionNewParams{
			Messages: []openai.ChatCompletionMessageParamUnion{
				openai.SystemMessage("You are a helpful assistant."),
				openai.UserMessage("Who are you?"),
			},
			Model: "qwen3.8-max",
		},
	)

	if err != nil {
		fmt.Fprintf(os.Stderr, "Request failed: %v\n", err)
		// For more information, see the documentation: https://help.aliyun.com/en/model-studio/developer-reference/error-code
		os.Exit(1)
	}

	if len(chatCompletion.Choices) > 0 {
		fmt.Println(chatCompletion.Choices[0].Message.Content)
	}
	// To view the full response, uncomment the following lines.
	// jsonData, _ := json.MarshalIndent(chatCompletion, "", "  ")
	// fmt.Println(string(jsonData))

}

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

C# (HTTP)

using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;

class Program
{
    private static readonly HttpClient httpClient = new HttpClient();

    static async Task Main(string[] args)
    {
        // API keys vary by region. To obtain an API key, see https://help.aliyun.com/en/model-studio/get-api-key
        // If you haven't set the environment variable, replace the following line with your Alibaba Cloud Model Studio API key: string? apiKey = "sk-xxx";
        string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
        // The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
        string url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions";
        string jsonContent = @"{
            ""model"": ""qwen3.8-max"",
            ""messages"": [
                {
                    ""role"": ""system"",
                    ""content"": ""You are a helpful assistant.""
                },
                {
                    ""role"": ""user"",
                    ""content"": ""Who are you?""
                }
            ]
        }";

        string result = await SendPostRequestAsync(url, jsonContent, apiKey);

        // To view the full response, uncomment the following line.
        // Console.WriteLine(result);

        // Parse the JSON to extract and print the content.
        using JsonDocument doc = JsonDocument.Parse(result);
        JsonElement root = doc.RootElement;

        if (root.TryGetProperty("choices", out JsonElement choices) &&
            choices.GetArrayLength() > 0)
        {
            JsonElement firstChoice = choices[0];
            if (firstChoice.TryGetProperty("message", out JsonElement message) &&
                message.TryGetProperty("content", out JsonElement content))
            {
                Console.WriteLine(content.GetString());
            }
        }
    }

    private static async Task<string> SendPostRequestAsync(string url, string jsonContent, string apiKey)
    {
        using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await httpClient.PostAsync(url, content);
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
            else
            {
                // For more information, see the documentation: https://help.aliyun.com/en/model-studio/developer-reference/error-code
                return $"Request failed: {response.StatusCode}";
            }
        }
    }
}

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

PHP (HTTP)

<?php
// Set the request URL.
// The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
$url = 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions';
// API keys vary by region. To obtain an API key, see https://help.aliyun.com/en/model-studio/get-api-key
// If you haven't set the environment variable, replace the following line with your Alibaba Cloud Model Studio API key: $apiKey = "sk-xxx";
$apiKey = getenv('DASHSCOPE_API_KEY');
// Set the request headers.
$headers = [
    'Authorization: Bearer '.$apiKey,
    'Content-Type: application/json'
];
// Set the request body.
$data = [
    "model" => "qwen3.8-max",
    "messages" => [
        [
            "role" => "system",
            "content" => "You are a helpful assistant."
        ],
        [
            "role" => "user",
            "content" => "Who are you?"
        ]
    ]
];
// Initialize a cURL session.
$ch = curl_init();
// Set cURL options.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute the cURL session.
$response = curl_exec($ch);
// Check for errors.
// For more information, see the documentation: https://help.aliyun.com/en/model-studio/developer-reference/error-code
if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
}
// Close the cURL resource.
curl_close($ch);
// Parse and output the response content.
$dataObject = json_decode($response);
$content = $dataObject->choices[0]->message->content;
echo $content;
// To view the full response, uncomment the following line.
//echo $response;
?>

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

curl

The 'base_url' and API key are region-specific. See OpenAI compatible - Chat for endpoint URLs and Obtain an API key to get your key.

# The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3.8-max",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "Who are you?"
        }
    ]
}'

Response

{
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!"
            },
            "finish_reason": "stop",
            "index": 0,
            "logprobs": null
        }
    ],
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 26,
        "completion_tokens": 66,
        "total_tokens": 92
    },
    "created": 1726127645,
    "system_fingerprint": null,
    "model": "qwen3.8-max",
    "id": "chatcmpl-81951b98-28b8-9659-ab07-xxxxxx"
}

OpenAI-Compatible Responses API

The Responses API succeeds the Chat Completions API. For usage instructions, code examples, and migration guides, see OpenAI-Compatible Responses.

Python

import os
from openai import OpenAI

try:
    client = OpenAI(
        # API Keys vary by region. Get your API Key at: https://help.aliyun.com/en/model-studio/get-api-key
        # If you do not set the environment variable, provide your API Key directly: api_key="sk-xxx",
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
        base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
    )

    response = client.responses.create(
        model="qwen3.8-max",
        input="Briefly introduce what you can do."
    )

    print(response)
except Exception as e:
    print(f"An error occurred: {e}")
    print("For details, see the error code documentation: https://help.aliyun.com/en/model-studio/developer-reference/error-code")

Response

Key response fields:

  • id: The response ID.

  • output: List containing reasoning and message objects.

    reasoning only appears when thinking is enabled (enabled by default for Qwen3.6 series).

  • usage: Token usage.

Sample message content. For the full response, see the curl section.

Hello! I am an AI assistant with a knowledge base current as of 2026. Here are the main things I can do for you:

1.  **Content Creation**: Draft articles, emails, reports, stories, poems, and more.
2.  **Code Assistance**: Write, debug, and explain code, with support for multiple mainstream programming languages.
3.  **Knowledge Q&A**: Answer questions in various fields such as science, history, culture, and technology.
4.  **Logical Analysis**: Assist with mathematical problem-solving, data analysis, solution planning, and decision support.
5.  **Language Translation**: Provide accurate translations between multiple languages.
6.  **Everyday Conversation**: Casual conversation, offer life advice, engage in role-playing, or provide tutoring.

Is there a specific task I can help you with? Just let me know!

Node.js

// Node.js v18+ is required. This code must be run in an ES Module environment.
import OpenAI from "openai";

const openai = new OpenAI({
    // API Keys vary by region. Get your API Key at: https://help.aliyun.com/en/model-studio/get-api-key
    // If you do not set the environment variable, provide your API Key directly: apiKey: "sk-xxx",
    apiKey: process.env.DASHSCOPE_API_KEY,
    // The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
    baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
});

async function main() {
    try {
        const response = await openai.responses.create({
            model: "qwen3.8-max",
            input: "Briefly introduce what you can do."
        });

        // Get the model response
        console.log(response);
    } catch (error) {
        console.error("An error occurred:", error);
    }
}

main();

Response

Key response fields:

  • id: The response ID.

  • output: List containing reasoning and message objects.

    reasoning only appears when thinking is enabled (enabled by default for Qwen3.6 series).

  • usage: Token usage.

Sample message content. For the full response, see the curl section.

Hello! I am an AI assistant with a knowledge base current as of 2026. Here are the main things I can do for you:

1.  **Content Creation**: Draft articles, emails, reports, stories, poems, and more.
2.  **Code Assistance**: Write, debug, and explain code, with support for multiple mainstream programming languages.
3.  **Knowledge Q&A**: Answer questions in various fields such as science, history, culture, and technology.
4.  **Logical Analysis**: Assist with mathematical problem-solving, data analysis, solution planning, and decision support.
5.  **Language Translation**: Provide accurate translations between multiple languages.
6.  **Everyday Conversation**: Casual conversation, offer life advice, engage in role-playing, or provide tutoring.

Is there a specific task I can help you with? Just let me know!

curl

# The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3.8-max",
    "input": "Briefly introduce what you can do."
}'

Response

{
    "created_at": 1772249644,
    "id": "56ec1cc7-f246-448e-bdf9-b13838bc6xxx",
    "model": "qwen3.8-max",
    "object": "response",
    "output": [
        {
            "id": "msg_082d9e97-2317-4a74-9c07-fc46e4845xxx",
            "summary": [
                {
                    "text": "Thinking Process:\n\n1.  **Analyze the Request:**\n    *   User asks: \"Briefly introduce what you can do?\"\n\n... (The full thinking process is omitted here for brevity.)",
                    "type": "summary_text"
                }
            ],
            "type": "reasoning"
        },
        {
            "content": [
                {
                    "annotations": [],
                    "text": "Hello! I am an AI assistant. Based on a knowledge base current as of 2026, I can help you with the following:\n\n1.  **Content Creation**: Write articles, emails, stories, poems, and various types of copy.\n2.  **Code Assistance**: Write, debug, and explain code, supporting multiple mainstream programming languages.\n3.  **Knowledge Q&A**: Answer various knowledge-based questions in fields like science, history, and culture.\n4.  **Text Processing**: Translate, summarize long articles, extract key information, and polish text.\n5.  **Logic and Analysis**: Assist in analyzing problems, providing suggestions, and performing logical reasoning.\n\nPlease note that I cannot perform physical actions, and without specific tool support, I cannot access real-time internet information. Is there a specific task I can assist you with?",
                    "type": "output_text"
                }
            ],
            "id": "msg_28643acb-1153-4d4f-84f8-19df29f11xxx",
            "role": "assistant",
            "status": "completed",
            "type": "message"
        }
    ],
    "parallel_tool_calls": false,
    "status": "completed",
    "tool_choice": "auto",
    "tools": [],
    "usage": {
        "input_tokens": 52,
        "input_tokens_details": {
            "cached_tokens": 0
        },
        "output_tokens": 1280,
        "output_tokens_details": {
            "reasoning_tokens": 1122
        },
        "total_tokens": 1332,
        "x_details": [
            {
                "input_tokens": 52,
                "output_tokens": 1280,
                "output_tokens_details": {
                    "reasoning_tokens": 1122
                },
                "total_tokens": 1332,
                "x_billing_type": "response_api"
            }
        ]
    }
}

DashScope

Importantqwen3.7-max, qwen3.7-max-2026-05-20, and qwen3.6-max-preview only support the text API. qwen3.8-max, qwen3.8-flash, and qwen3.7-max-2026-06-08 support the multimodal API. The Qwen3.6 and Qwen3.5 series require the multimodal DashScope API. Running the following examples with these models returns a url error. For the correct multimodal API call, see Image and Video Data Processing.

Python

import json
import os
from dashscope import Generation
import dashscope

# The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who are you?"},
]
response = Generation.call(
    # API keys vary by region. To get an API key, see https://help.aliyun.com/en/model-studio/get-api-key
    # If you have not set the environment variable, replace the following line with your Model Studio API key: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # qwen3.7-max, qwen3.7-max-2026-05-20, and qwen3.6-max-preview only support the text API. qwen3.8-max and qwen3.7-max-2026-06-08 support the multimodal API. Qwen3.6 and Qwen3.5 series require the multimodal API. Directly replacing the model will cause an error.
    model="qwen-plus",
    messages=messages,
    result_format="message",
)

if response.status_code == 200:
    print(response.output.choices[0].message.content)
    # To view the full response, uncomment the following line.
    # print(json.dumps(response, default=lambda o: o.__dict__, indent=4))
else:
    print(f"HTTP status code: {response.status_code}")
    print(f"Error code: {response.code}")
    print(f"Error message: {response.message}")
    print("For more information, see: https://help.aliyun.com/en/model-studio/developer-reference/error-code")

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

Java

import java.util.Arrays;
import java.lang.System;
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.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.protocol.Protocol;
import com.alibaba.dashscope.utils.JsonUtils;

public class Main {
    public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
        // The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
        Generation gen = new Generation(Protocol.HTTP.getValue(), "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1");
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant.")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("Who are you?")
                .build();
        GenerationParam param = GenerationParam.builder()
                // If you have not set the environment variable, replace the following line with your Model Studio API key: .apiKey("sk-xxx")
                // API keys vary by region. To get an API key, see https://help.aliyun.com/en/model-studio/get-api-key
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // qwen3.7-max, qwen3.7-max-2026-05-20, and qwen3.6-max-preview only support the text API. qwen3.8-max and qwen3.7-max-2026-06-08 support the multimodal API. Qwen3.6 and Qwen3.5 series require the multimodal API. Directly replacing the model will cause an error.
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, 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());
            // To view the full response, uncomment the following line.
            // System.out.println(JsonUtils.toJson(result));
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.err.println("Error message: "+e.getMessage());
            System.out.println("For more information, see: https://help.aliyun.com/en/model-studio/developer-reference/error-code");
        }
    }
}

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

Node.js (HTTP)

// Requires Node.js v18+
// API keys vary by region. To get an API key, see https://help.aliyun.com/en/model-studio/get-api-key
// If you have not set the environment variable, replace the following line with your Model Studio API key: const apiKey = "sk-xxx";
const apiKey = process.env.DASHSCOPE_API_KEY;

const data = {
    // qwen3.7-max, qwen3.7-max-2026-05-20, and qwen3.6-max-preview only support the text API. qwen3.8-max and qwen3.7-max-2026-06-08 support the multimodal API. Qwen3.6 and Qwen3.5 series require the multimodal API. Directly replacing the model will cause an error.
    model: "qwen-plus",
    input: {
        messages: [
            {
                role: "system",
                content: "You are a helpful assistant."
            },
            {
                role: "user",
                content: "Who are you?"
            }
        ]
    },
    parameters: {
        result_format: "message"
    }
};

async function callApi() {
    try {
        // The following URL is region-specific. Modify it for your region.
        const response = await fetch('https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation', {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${apiKey}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        });

        const result = await response.json();
        console.log(result.output.choices[0].message.content);
        // To view the full response, uncomment the following line.
        // console.log(JSON.stringify(result));
    } catch (error) {
        // For more information, see: https://help.aliyun.com/en/model-studio/developer-reference/error-code
        console.error('Request failed:', error.message);
    }
}

callApi();

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

Go (HTTP)

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
)

func main() {
	requestBody := map[string]interface{}{
		// qwen3.7-max, qwen3.7-max-2026-05-20, and qwen3.6-max-preview only support the text API. qwen3.8-max and qwen3.7-max-2026-06-08 support the multimodal API. Qwen3.6 and Qwen3.5 series require the multimodal API. Directly replacing the model will cause an error.
		"model": "qwen-plus",
		"input": map[string]interface{}{
			"messages": []map[string]string{
				{
					"role":    "system",
					"content": "You are a helpful assistant.",
				},
				{
					"role":    "user",
					"content": "Who are you?",
				},
			},
		},
		"parameters": map[string]string{
			"result_format": "message",
		},
	}

	// Serialize to JSON.
	jsonData, _ := json.Marshal(requestBody)

	// Create an HTTP client and request.
	client := &http.Client{}
	// The following URL is region-specific. Modify it for your region.
	req, _ := http.NewRequest("POST", "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation", bytes.NewBuffer(jsonData))

	// Set request headers.
	apiKey := os.Getenv("DASHSCOPE_API_KEY")
	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")

	// Send the request.
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	// Read the response body.
	bodyText, _ := io.ReadAll(resp.Body)

	// Parse the JSON and print the content.
	var result map[string]interface{}
	json.Unmarshal(bodyText, &result)
	content := result["output"].(map[string]interface{})["choices"].([]interface{})[0].(map[string]interface{})["message"].(map[string]interface{})["content"].(string)
	fmt.Println(content)

	// To view the full response, uncomment the following line.
	// fmt.Printf("%s\n", bodyText)
}

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

C# (HTTP)

using System.Net.Http.Headers;
using System.Text;

class Program
{
    private static readonly HttpClient httpClient = new HttpClient();

    static async Task Main(string[] args)
    {
        // API keys vary by region. To get an API key, see https://help.aliyun.com/en/model-studio/get-api-key
        // If you have not set the environment variable, replace the following line with your Model Studio API key: string? apiKey = "sk-xxx";
        string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
        // Set the request URL and content.
        // The following URL is region-specific. Modify it for your region.
        string url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
        // qwen3.7-max, qwen3.7-max-2026-05-20, and qwen3.6-max-preview only support the text API. qwen3.8-max and qwen3.7-max-2026-06-08 support the multimodal API. Qwen3.6 and Qwen3.5 series require the multimodal API. Directly replacing the model will cause an error.
        string jsonContent = @"{
            ""model"": ""qwen-plus"",
            ""input"": {
                ""messages"": [
                    {
                        ""role"": ""system"",
                        ""content"": ""You are a helpful assistant.""
                    },
                    {
                        ""role"": ""user"",
                        ""content"": ""Who are you?""
                    }
                ]
            },
            ""parameters"": {
                ""result_format"": ""message""
            }
        }";

        // Send the request and get the response.
        string result = await SendPostRequestAsync(url, jsonContent, apiKey);
        var jsonResult = System.Text.Json.JsonDocument.Parse(result);
        var content = jsonResult.RootElement.GetProperty("output").GetProperty("choices")[0].GetProperty("message").GetProperty("content").GetString();
        Console.WriteLine(content);
        // To view the full response, uncomment the following line.
        // Console.WriteLine(result);
    }

    private static async Task<string> SendPostRequestAsync(string url, string jsonContent, string? apiKey)
    {
        using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
        {
            // Set request headers.
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Send the request and get the response.
            HttpResponseMessage response = await httpClient.PostAsync(url, content);

            // Handle the response.
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
            else
            {
                return $"Request failed: {response.StatusCode}";
            }
        }
    }
}

Response

{
    "output": {
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!"
                }
            }
        ]
    },
    "usage": {
        "total_tokens": 92,
        "output_tokens": 66,
        "input_tokens": 26
    },
    "request_id": "09dceb20-ae2e-999b-85f9-xxxxxx"
}

PHP (HTTP)

<?php
// The following URL is region-specific. Modify it for your region.
$url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
// API keys vary by region. To get an API key, see https://help.aliyun.com/en/model-studio/get-api-key
// If you have not set the environment variable, replace the following line with your Model Studio API key: $apiKey = "sk-xxx";
$apiKey = getenv('DASHSCOPE_API_KEY');

$data = [
    // qwen3.7-max, qwen3.7-max-2026-05-20, and qwen3.6-max-preview only support the text API. qwen3.8-max and qwen3.7-max-2026-06-08 support the multimodal API. Qwen3.6 and Qwen3.5 series require the multimodal API. Directly replacing the model will cause an error.
    "model" => "qwen-plus",
    "input" => [
        "messages" => [
            [
                "role" => "system",
                "content" => "You are a helpful assistant."
            ],
            [
                "role" => "user",
                "content" => "Who are you?"
            ]
        ]
    ],
    "parameters" => [
        "result_format" => "message"
    ]
];

$jsonData = json_encode($data);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer $apiKey",
    "Content-Type: application/json"
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($httpCode == 200) {
    $jsonResult = json_decode($response, true);
    $content = $jsonResult['output']['choices'][0]['message']['content'];
    echo $content;
    // To view the full response, uncomment the following line.
    // echo "Model response: " . $response;
} else {
    echo "Request failed: " . $httpCode . " - " . $response;
}

curl_close($ch);
?>

Response

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!

curl

The base URL and API key vary by region. For details, see DashScope and Obtain an API key.

# The following URL is region-specific. Modify it for your region.
curl --location "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
    "model": "qwen-plus",
    "input":{
        "messages":[
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Who are you?"
            }
        ]
    },
    "parameters": {
        "result_format": "message"
    }
}'

Response

{
    "output": {
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, program, share opinions, play games, and more. If you have any questions or need help, feel free to ask!"
                }
            }
        ]
    },
    "usage": {
        "total_tokens": 92,
        "output_tokens": 66,
        "input_tokens": 26
    },
    "request_id": "09dceb20-ae2e-999b-85f9-xxxxxx"
}

dashscope CLI

Call via the dashscope command line.

export DASHSCOPE_API_KEY="your-api-key"
# Replace {WorkspaceId} with your Workspace ID, and cn-beijing with the corresponding region (Singapore: ap-southeast-1, US East: us-east-1)
export DASHSCOPE_HTTP_BASE_URL="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"
dashscope generation create -m qwen-plus -p "Hello"

ImportantThe SDK Expert interactive assistant can accomplish the same development and troubleshooting in natural language. See

DashScope SDK Expert.

For the complete region table, see Base URL overview.

Image and video data processing

Multimodal models process non-text data (images, videos) for tasks like visual question answering and event detection. They differ from text-only models in two ways:

  • User message construction: Multimodal user messages include text and non-text data such as images and audio.
  • DashScope SDK interfaces: Use the MultiModalConversation interface for the DashScope Python SDK, and the MultiModalConversation class for the DashScope Java SDK.

For limitations on image and video files, see Image and video understanding.

OpenAI compatible chat completions

Python

from openai import OpenAI
import os

client = OpenAI(
    # API keys vary by region. To get an API key: https://help.aliyun.com/en/model-studio/get-api-key
    # If the environment variable is not set, provide your Model Studio API key directly, for example: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The endpoint URL varies by region. Modify it for your region.
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
)
messages = [
    {
        "role": "user",
        "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251031/ownrof/f26d201b1e3f4e62ab4a1fc82dd5c9bb.png"
                    },
                },
            {"type": "text", "text": "What products are shown in the image?"},
        ],
    }
]
completion = client.chat.completions.create(
    model="qwen3.6-plus",
    messages=messages,
)
print(completion.choices[0].message.content)

Node.js

import OpenAI from "openai";

const openai = new OpenAI(
    {
        // API keys vary by region. To get an API key: https://help.aliyun.com/en/model-studio/get-api-key
        // If the environment variable is not set, provide your Model Studio API key directly, for example: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        // The endpoint URL varies by region. Modify it for your region.
        baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
    }
);

let messages = [
    {
        role: "user",
        content: [
            { type: "image_url", image_url: { "url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251031/ownrof/f26d201b1e3f4e62ab4a1fc82dd5c9bb.png" } },
            { type: "text", text: "What products are shown in the image?" },
        ]
    }]
async function main() {
    let response = await openai.chat.completions.create({
        model: "qwen3.6-plus",
        messages: messages
    });
    console.log(response.choices[0].message.content);
}

main()

curl

The 'base_url' and API key are region-specific. See OpenAI compatible - Chat for endpoint URLs and Obtain an API key to get your key.

# The endpoint URL varies by region. Modify it for your region.
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
  "model": "qwen3.6-plus",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "image_url",
          "image_url": {
            "url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251031/ownrof/f26d201b1e3f4e62ab4a1fc82dd5c9bb.png"
          }
        },
        {
          "type": "text",
          "text": "What products are shown in the image?"
        }
      ]
    }
  ]
}'

DashScope

Python

import os
import dashscope
from dashscope import MultiModalConversation

# This is the endpoint for the China (Beijing) region. Replace {WorkspaceId} with your actual workspace ID. Endpoints vary by region.
dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"

messages = [
    {
        "role": "user",
        "content": [
            {
                "image": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251031/ownrof/f26d201b1e3f4e62ab4a1fc82dd5c9bb.png"
            },
            {"text": "What products are shown in the image?"},
        ],
    }
]
response = MultiModalConversation.call(
    # API keys vary by region. To get an API key: https://help.aliyun.com/en/model-studio/get-api-key
    # If the environment variable is not set, provide your Model Studio API key directly, for example: api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    model='qwen3.6-plus',   # You can replace this with another multimodal model and modify the messages accordingly.
    messages=messages)
print(response.output.choices[0].message.content[0]['text'])

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    // This is the endpoint for the China (Beijing) region. Replace {WorkspaceId} with your actual workspace ID. Endpoints vary by region.
    static {Constants.baseHttpApiUrl="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";}

    private static final String modelName = "qwen3.6-plus";  // You can replace this with another multimodal model and modify the messages accordingly.

    public static void MultiRoundConversationCall() throws ApiException, NoApiKeyException, UploadFileException {
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
                .content(Arrays.asList(Collections.singletonMap("image", "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251031/ownrof/f26d201b1e3f4e62ab4a1fc82dd5c9bb.png"),
                        Collections.singletonMap("text", "What products are shown in the image?"))).build();
        List<MultiModalMessage> messages = new ArrayList<>();
        messages.add(userMessage);
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                // API keys vary by region. To get an API key: https://help.aliyun.com/en/model-studio/get-api-key
                // If the environment variable is not set, provide your Model Studio API key directly, for example: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .model(modelName)
                .messages(messages)
                .build();
        MultiModalConversationResult result = conv.call(param);
        System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text"));
    }

    public static void main(String[] args) {
        try {
            MultiRoundConversationCall();
        } catch (ApiException | NoApiKeyException | UploadFileException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}

curl

The base URL and API key vary by region. For details, see DashScope and Obtain an API key.

# The endpoint URL varies by region. Modify it for your region.
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
    "model": "qwen3.6-plus",
    "input":{
        "messages":[
            {
                "role": "user",
                "content": [
                    {"image": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251031/ownrof/f26d201b1e3f4e62ab4a1fc82dd5c9bb.png"},
                    {"text": "What products are shown in the image?"}
                ]
            }
        ]
    }
}'

Asynchronous calls

Asynchronous calls improve throughput for high-concurrency workloads.

OpenAI-compatible chat completions API

import os
import asyncio
from openai import AsyncOpenAI
import platform

# Create an asynchronous client instance.
client = AsyncOpenAI(
    # API keys vary by region. To get an API key, see https://help.aliyun.com/en/model-studio/get-api-key
    # If you have not set the environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # This endpoint is for the China (Hangzhou) region. Update it for your region.
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
)

# Define an asynchronous task.
async def task(question):
    print(f"Sending question: {question}")
    response = await client.chat.completions.create(
        messages=[
            {"role": "system", "content": "You are a helpful assistant." },
            {"role": "user", "content": question}
        ],
        model="qwen-plus",  # For a list of models, see https://help.aliyun.com/en/model-studio/getting-started/models
    )
    print(f"Model response: {response.choices[0].message.content}")

# Main asynchronous function.
async def main():
    questions = ["Who are you?", "What can you do?", "What's the weather like?"]
    tasks = [task(q) for q in questions]
    await asyncio.gather(*tasks)

if __name__ == '__main__':
    # Set the event loop policy.
    if platform.system() == 'Windows':
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    # Run the main coroutine.
    asyncio.run(main(), debug=False)
import com.openai.client.OpenAIClientAsync;
import com.openai.client.okhttp.OpenAIOkHttpClientAsync;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class Main {
    public static void main(String[] args) {
        // Create an OpenAI client to connect to the DashScope-compatible endpoint.
        OpenAIClientAsync client = OpenAIOkHttpClientAsync.builder()
                // API keys vary by region. To get an API key, see https://help.aliyun.com/en/model-studio/get-api-key
                // If you have not set the environment variable, replace the following line with your Model Studio API key: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // This endpoint is for the China (Hangzhou) region. Update it for your region.
                .baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
                .build();

        // Define a list of questions.
        List<String> questions = Arrays.asList("Who are you?", "What can you do?", "What's the weather like?");

        // Create a list of asynchronous tasks.
        CompletableFuture<?>[] futures = questions.stream()
                .map(question -> CompletableFuture.supplyAsync(() -> {
                    System.out.println("Sending question: " + question);
                    // Create ChatCompletion parameters.
                    ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                            .model("qwen-plus")  // Specify the model.
                            .addSystemMessage("You are a helpful assistant.")
                            .addUserMessage(question)
                            .build();

                    // Send an asynchronous request and handle the response.
                    return client.chat().completions().create(params)
                        .thenAccept(chatCompletion -> {
                            String content = chatCompletion.choices().get(0).message().content().orElse("No content in response");
                            System.out.println("Model response: " + content);
                        })
                        .exceptionally(e -> {
                            System.err.println("Error: " + e.getMessage());
                            System.out.println("See the documentation: https://help.aliyun.com/en/model-studio/developer-reference/error-code");
                            return null;
                        });
                }).thenCompose(future -> future))
                .toArray(CompletableFuture[]::new);

        // Wait for all asynchronous operations to complete.
        CompletableFuture.allOf(futures).join();
    }
}

DashScope

Asynchronous text generation with the DashScope SDK is only supported in Python.

# This requires DashScope Python SDK v1.19.0 or later.
import asyncio
import platform
from dashscope.aigc.generation import AioGeneration
import os
import dashscope

# This is the endpoint for the China (Hangzhou) region. Update it for other regions.
dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"

# Define an asynchronous task.
async def task(question):
    print(f"Sending question: {question}")
    response = await AioGeneration.call(
        # If you have not set 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="qwen-plus",  # For a list of models, see https://help.aliyun.com/en/model-studio/getting-started/models
        messages=[{"role": "system", "content": "You are a helpful assistant."},
                  {"role": "user", "content": question}],
        result_format="message",
    )
    print(f"Model response: {response.output.choices[0].message.content}")

# Main asynchronous function.
async def main():
    questions = ["Who are you?", "What can you do?", "What's the weather like?"]
    tasks = [task(q) for q in questions]
    await asyncio.gather(*tasks)

if __name__ == '__main__':
    # Set the event loop policy.
    if platform.system() == 'Windows':
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    # Run the main coroutine.
    asyncio.run(main(), debug=False)
Sample response

Because the calls are asynchronous, the order of the responses may differ from this example.

Sending question: Who are you?
Sending question: What can you do?
Sending question: What's the weather like?
Model response: Hello! I'm Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can help you answer questions and create content, such as writing stories, official documents, emails, and scripts. I can also do logical reasoning, programming, share opinions, play games, and more. If you have any questions or need help, feel free to ask!
Model response: Hello! I am currently unable to access real-time weather information. You can tell me your city or region, and I will do my best to provide you with general weather advice or information. Alternatively, you can use a weather app to check the real-time weather conditions.
Model response: I have many skills, for example:

1. Answering questions: Whether it's academic questions, general knowledge, or professional topics, I can try to help you find answers.
2. Creating text: I can write various types of text, such as stories, official documents, emails, and scripts.
3. Logical reasoning: I can help you solve logical reasoning problems, such as math problems and riddles.
4. Programming: I can provide programming assistance, including code writing, debugging, and optimization.
5. Multilingual support: I support multiple languages, including but not limited to Chinese, English, French, and Spanish.
6. Expressing opinions: I can offer you some perspectives and suggestions to help you make decisions.
7. Playing games: We can play text-based games together, such as riddles or idiom solitaire.

If you have any specific needs or questions, feel free to let me know, and I will do my best to help you!

Production use

Building high-quality context

Feeding large amounts of raw data to a model increases costs and can degrade performance due to context window limitations. Context engineering -- dynamically loading precise knowledge -- improves generation quality and efficiency. Key techniques include:

  • prompt engineering: Design and optimize text prompts to guide the model toward the desired output. For more information, see the Prompt guide for text generation and the Alibaba Cloud Model Studio Prompt Templates page.
  • Retrieval-Augmented Generation (RAG): Lets the model answer questions from an external knowledge base such as product documentation or technical manuals.
  • tool calling: Retrieves real-time information (weather, traffic) or performs actions (API calls, sending emails) on behalf of the model.
  • memory: Provides long-term and short-term memory so the model can recall context across multi-turn conversations.

For a systematic understanding, see the Alibaba Cloud Certified Professional (ACP) course for large model senior engineers.

Controlling response diversity

The temperature and top_p parameters control the diversity of the generated text. Higher values increase diversity; lower values increase determinism. To isolate the effect of each parameter, adjust only one at a time.

  • temperature: Range: [0, 2). Primarily adjusts randomness.
  • top_p: Range: [0, 1]. Filters responses based on a probability threshold.

The following examples show how parameter settings affect output. Input prompt: "Write a three-sentence short story where the main characters are a cat and a sunbeam."

  • High diversity (Example: temperature=0.9): Best for creative writing, brainstorming, or marketing copy.
Sunlight slanted across the windowsill, and the orange cat crept toward the bright patch as its fur turned the color of melted honey.
It reached out and tapped the light, then sank into it as if stepping into a warm pool, and the sunlight flowed up its back in a quiet tide.
The afternoon grew heavy—curled in drifting gold, the cat heard time melt softly inside its purr.
  • High determinism (Example: temperature=0.1): Best for fact-based question answering, code generation, or legal texts.
In the afternoon, an old cat curled on the windowsill and dozed while counting the spots of light.
Sunlight hopped across its mottled back, like turning the pages of an old photo album.
Dust rose and fell, as if time whispered: you were once young, and I was once fierce.

How it works

temperature:

  • A higher temperature flattens the token probability distribution, making less likely tokens more probable and increasing output randomness.
  • A lower temperature sharpens the distribution, making high-probability tokens even more likely and reducing output randomness.

top_p:

top_p (nucleus) sampling selects from the smallest set of tokens whose cumulative probability meets or exceeds the top_p threshold. Tokens are sorted by probability and accumulated until the threshold is met, then the next token is randomly sampled from this reduced set.

  • A higher top_p widens the token selection pool, producing more diverse text.
  • A lower top_p narrows the pool, producing more focused and deterministic text.

Example parameter settings for common scenarios

# Recommended parameter settings for common scenarios
SCENARIO_CONFIGS = {
    # Creative writing
    "creative_writing": {
        "temperature": 0.9,
        "top_p": 0.95
    },
    # Code generation
    "code_generation": {
        "temperature": 0.2,
        "top_p": 0.8
    },
    # Factual Q&A
    "factual_qa": {
        "temperature": 0.1,
        "top_p": 0.7
    },
    # Translation
    "translation": {
        "temperature": 0.3,
        "top_p": 0.8
    }
}

# OpenAI example
# completion = client.chat.completions.create(
#     model="qwen-plus",
#     messages=[{"role": "user", "content": "Write a poem about the moon"}],
#     **SCENARIO_CONFIGS["creative_writing"]
# )
# DashScope example
# response = Generation.call(
#     # If you have not set an 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"),
#     model="qwen-plus",
#     messages=[{"role": "user", "content": "Write a Python function that determines whether the input n is a prime number. Output code only."}],
#     result_format="message",
#     **SCENARIO_CONFIGS["code_generation"]
# )

More features

For more complex scenarios, the following features are available:

  • multi-turn conversation: For continuous interaction such as follow-up questions or information gathering.
  • streaming output: Returns tokens incrementally as they are generated, preventing timeouts for chatbots and real-time code generation.
  • deep thinking: Produces higher-quality, more structured answers for complex reasoning or strategic analysis.
  • structured output: Constrains responses to a consistent JSON format for programmatic use and data parsing.
  • prefix completion: Continues generation from existing text, useful for code completion or long-form writing.

API reference

For all parameters, see the OpenAI-compatible API reference and the DashScope API reference.

FAQ

Q: Why is the input token count higher than the token count of the text I sent?

A: When processing a conversation, the system uses a Chat Template to wrap the raw input text, adding control markers such as role identifiers and message boundaries. These system-generated markers are also counted as tokens.

For example, when you send the message {"role": "user", "content": "Hi"} to qwen3.8-max, the text "Hi" corresponds to only 1 token after tokenization. However, during system processing, the actual full input text is formatted as follows: <|im_start|>user\nHi<|im_end|>\n<|im_start|>assistant\n<think>. After tokenization, this full text increases the total input token count to 11.

A: The Qianwen API cannot access web page content directly. Instead, use function calling and MCP, or a web scraping tool like Python's Beautiful Soup to extract the content and pass it to the model.

Q: Response differences:Qianwen (Web)vs. Qianwen API

A: Qianwen (Web) adds features on top of the Qianwen API, including webpage parsing, web search, image creation, and PPT generation. These are not included in the base API, but you can build similar functionality using web search, function calling, and MCP.

Q: How to handlemodel timeouts

A: Use streaming output to avoid timeouts. Streaming returns tokens incrementally, eliminating the wait for a complete response.

For non-streaming calls, the maximum timeout is at least 300 seconds and varies by region and model. If not completed in time, the service interrupts it and returns the content generated so far without reporting a timeout error. The response header includes x-dashscope-partialresponse: true to indicate a partial result.

This mechanism is supported by the following models:

If the response header is not accessible (for example, when using an SDK), check the finish_reason field. A value of null indicates incomplete content, though not necessarily a timeout.

To resume generation from incomplete output, see Continue Writing from Incomplete Output.

Prefix-based continuation is not yet supported in the Java SDK.

Q: Generating Word, Excel, PDF, or PPT files

A: No. Text generation models only output plain text. Convert the output to the desired format using your own code or third-party libraries, or generate the files using applications like the Model Studio (Bailian) PPT auto-generation application or Qianwen (Web).