Skip to main content
Text generation

Partial mode

For scenarios like code completion and text continuation, you can generate new content starting from an existing text fragment (prefix). Partial Mode ensures the model's output connects seamlessly with your prefix for improved accuracy and control.

How it works

To use Partial Mode, configure the messages array. In the last message of the array, set the role to assistant and provide the prefix in the content field. You must also set the "partial": true parameter in that message. The messages format is as follows:
[
    {
        "role": "user",
        "content": "Complete this Fibonacci function. Do not add anything else."
    },
    {
        "role": "assistant",
        "content": "def calculate_fibonacci(n):\n    if n <= 1:\n        return n\n    else:\n",
        "partial": true
    }
]
The model then starts generating text from the specified prefix.

Supported models

  • Text generation models
    • Qwen-Max (non-thinking mode): Qwen3.7-Max series, Qwen3.6-Max series, Qwen3-Max series, Qwen-Max series
    • Qwen-Plus (non-thinking mode): Qwen3.7-Plus series, Qwen3.6-Plus series, Qwen3.5-Plus series, Qwen-Plus series
    • Qwen-Flash (non-thinking mode): Qwen3.7-Flash series, Qwen3.6-Flash series, Qwen3.5-Flash series, Qwen-Flash series
    • Qwen-Coder: Qwen3-Coder series, Qwen2.5-Coder series
    • Qwen-Turbo (non-thinking mode): Qwen-Turbo series
    • Qwen3.6 open source series (non-thinking mode)
    • Qwen3.5 open source series (non-thinking mode)
    • Qwen3 open source series (non-thinking mode)
    • Qwen2.5 open source series
  • Multimodal models
    • Qwen-VL: Qwen3-VL-Plus series, Qwen3-VL-Flash series, Qwen-VL-Max series, Qwen-VL-Plus series
    • Qwen3-VL open source series (non-thinking mode)

Getting started

Prerequisites

Before you begin, get an API key and set the API key as an environment variable. If you call the service using the OpenAI SDK or DashScope SDK, you must install the SDK. If you are a member of a sub-workspace, ensure that the super administrator has granted model access to your workspace.
The DashScope Java SDK is not supported.

Sample code

Code completion is the core use case for Partial Mode. The following example shows how to complete a Python function.
  • OpenAI compatible
  • DashScope
  • Python
  • Node.js
  • curl
import os
from openai import OpenAI

# 1. Initialize the client
client = OpenAI(
    # API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    # If not set in environment, replace here with your API key
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
    # For Beijing region use: https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# 2. Define the code prefix to complete
prefix = """def calculate_fibonacci(n):
    if n <= 1:
        return n
    else:
"""

# 3. Make a Partial Mode request
# Note: The last message in the messages array must have role "assistant" and include "partial": True
completion = client.chat.completions.create(
    model="qwen3.8-max",
    messages=[
        {"role": "user", "content": "Complete this Fibonacci function. Do not add anything else."},
        {"role": "assistant", "content": prefix, "partial": True},
    ],
)

# 4. Manually join the prefix and the model's generated content
generated_code = completion.choices[0].message.content
complete_code = prefix + generated_code

print(complete_code)

Response

def calculate_fibonacci(n):
    if n <= 1:
        return n
    else:
        return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)

Use cases

Pass images or videos

Qwen-VL models support Partial Mode with image or video data, which is useful for scenarios such as product descriptions, social posts, news articles, and creative copywriting.
  • OpenAI compatible
  • DashScope
  • Python
  • Node.js
  • curl
import os
from openai import OpenAI

client = OpenAI(
    # If not set in environment, replace the next line with: api_key="sk-xxx",
    # API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
    # For Beijing region use: https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)

completion = client.chat.completions.create(
    model="qwen3-vl-plus",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://img.alicdn.com/imgextra/i3/O1CN01zFX2Bs1Q0f9pESgPC_!!6000000001914-2-tps-450-450.png"
                    },
                },
                {"type": "text", "text": "I want to post this on social media. Help me write a caption."},
            ],
        },
        {
            "role": "assistant",
            "content": "Today I discovered a hidden-gem café",
            "partial": True,
        },
    ],
)
print(completion.choices[0].message.content)

Response

— the tiramisu here is pure bliss! Every bite delivers perfect harmony between coffee and cream. Pure joy! #FoodShare #Tiramisu #CoffeeTime

Hope you like this caption! Let me know if you need any changes.

Continue from incomplete output

If the max_tokens value is too small, the LLM may return incomplete content. Use Partial Mode to continue from that point and ensure the output is semantically complete.
  • OpenAI compatible
  • DashScope
  • Python
  • Node.js
import os
from openai import OpenAI

client = OpenAI(
    # API keys differ by region. Get your API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    # If not set in environment, replace here with your API key
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
    # For Beijing region use: https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

def chat_completion(messages,max_tokens=None):
    response = client.chat.completions.create(
        model="qwen-plus",
        messages=messages,
        max_tokens=max_tokens
    )
    print(f"### Reason generation stopped: {response.choices[0].finish_reason}")

    return response.choices[0].message.content

# Example usage
messages = [{"role": "user", "content": "Write a short sci-fi story"}]

# First call with max_tokens set to 40
first_content = chat_completion(messages, max_tokens=40)
print(first_content)
# Add the first response as an assistant message and set partial=True
messages.append({"role": "assistant", "content": first_content, "partial": True})

# Second call
second_content = chat_completion(messages)
print("### Complete content:")
print(first_content+second_content)

Response

length: The max_tokens limit was reached. stop: The model finished naturally or hit a stop word from the stop parameter.
### Reason generation stopped: length
**"The End of Memory"**

In the distant future, Earth is no longer fit for human life. The atmosphere is polluted, oceans are dry, and cities lie in ruins. Humans migrated to a habitable planet named "Eden," with blue skies, fresh air, and endless resources.

However, Eden is not a true paradise. It holds no human history, no past, and no memory.

...
**"If we forget who we are, are we still human?"**

— End —

Billing

Billing covers input and output tokens. The prefix counts as part of the input tokens.

Error codes

If the model call fails and returns an error message, see Error codes for resolution.
Token Plan
Model Playground
Statistics and Monitoring
Support