Skip to main content
Specialized models

Long context (Qwen-Long)

Qwen-Long handles documents up to 10 million tokens through a file upload and reference mechanism, overcoming standard model context limits.

This document applies only to the Chinese mainland (Beijing) region. To use the model, you must use an API key from theChinese mainland (Beijing) region.

How to use

Use Qwen-Long in two steps: upload files, then call the API.
  1. File upload and parsing:
    • Upload a file using the API. For details about supported file formats and size limits, see Supported formats.
    • After a successful upload, the system returns a unique file-id for your account and starts parsing. No fees are charged for file upload, storage, or parsing.
  2. API call and billing:
    • When you call the model, reference one or more file-ids in the system message.
    • The model performs inference based on the text content associated with the file-id.
    • For each API call, the number of tokens in the referenced file content is counted as input tokens for that request.
This avoids transferring large files in each request, but note that file tokens are billed per API call.

Getting started

Prerequisites

Upload a document

This example uploads Model_Studio_Phone_Product_Introduction.docx to Model Studio's secure storage via the OpenAI-compatible interface and gets a file-id. See the API documentation for upload parameters.
Python
import os
from pathlib import Path
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),  # If not configured, replace with your 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",
)

file_object = client.files.create(file=Path("Model_Studio_Phone_Product_Introduction.docx"), purpose="file-extract")
print(file_object.id)
Run the code to obtain the file-id for the uploaded file.

Pass information and chat using a file ID

Pass the file-id in system messages: first message defines the role, second contains the file-id, then add user questions.
Longer documents need more parsing time. Wait for parsing to complete before calling.
Python
import os
from openai import OpenAI, BadRequestError

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),  # If not configured, replace with your API key.
    # Endpoint 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",
)
try:
    # Initialize messages list.
    completion = client.chat.completions.create(
        model="qwen-long",
        messages=[
            # sys1: Role definition.
            {'role': 'system', 'content': 'You are a helpful assistant.'},
            # sys2: Document content (plain text or file-id).
            # Replace '{FILE_ID}' with the file-id used in your conversation.
            {'role': 'system', 'content': f'fileid://{FILE_ID}'},
            # When the request includes a second system message, the user message content is limited to 9,000 tokens.
            {'role': 'user', 'content': 'What is this article about?'}
        ],
        # All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/en/model-studio/text-generation
        stream=True,
        stream_options={"include_usage": True}
    )

    full_content = ""
    for chunk in completion:
        if chunk.choices and chunk.choices[0].delta.content:
            # Concatenate the output content.
            full_content += chunk.choices[0].delta.content
            print(chunk.model_dump())

        # Get token usage.
        if chunk.usage:
            print(f"Total tokens: {chunk.usage.total_tokens}")

    print(full_content)

except BadRequestError as e:
    print(f"Error: {e}")
    print("See documentation: https://www.alibabacloud.com/help/en/model-studio/error-code")

Pass multiple documents

Pass multiple file-ids in one system message or add separate system messages for each document.
  • Pass multiple documents
  • Append documents
Python
import os
from openai import OpenAI, BadRequestError

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),  # If not configured, replace with your API key.
    # Endpoint 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",
)
try:
    # Initialize messages list.
    completion = client.chat.completions.create(
        model="qwen-long",
        messages=[
            {'role': 'system', 'content': 'You are a helpful assistant.'},
            # Replace '{FILE_ID1}' and '{FILE_ID2}' with the file-ids used in your conversation.
            {'role': 'system', 'content': f"fileid://{FILE_ID1},fileid://{FILE_ID2}"},
            {'role': 'user', 'content': 'What are these articles about?'}
        ],
        # All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/en/model-studio/text-generation
        stream=True,
        stream_options={"include_usage": True}
    )

    full_content = ""
    for chunk in completion:
        if chunk.choices and chunk.choices[0].delta.content:
            # Concatenate the output content.
            full_content += chunk.choices[0].delta.content
            print(chunk.model_dump())

    print(full_content)

except BadRequestError as e:
    print(f"Error: {e}")
    print("See documentation: https://www.alibabacloud.com/help/en/model-studio/error-code")

Pass information as plain text

Instead of using file-ids, pass document content directly as a string. Add role settings in the first message to prevent confusion with document content.
If document content exceeds 1 million tokens, use a file ID instead due to API size limits.
  • Simple example
  • Pass multiple documents
  • Append documents
You can input the document content directly into the System Message.
Python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),  # Replace your API key here if you haven't set the environment variable
    # Endpoint 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",
)
# Initialize the messages list
completion = client.chat.completions.create(
    model="qwen-long",
    messages=[
        {'role': 'system', 'content': 'You are a helpful assistant.'},
        {'role': 'system', 'content': 'Alibaba Cloud Model Studio smartphone product introduction: Alibaba Cloud Model Studio X1 —————— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen...'},
        {'role': 'user', 'content': 'What does the article talk about?'}
    ],
    # All code examples use streaming output to clearly and intuitively show the model's output process. For non-streaming examples, see https://www.alibabacloud.com/help/en/model-studio/text-generation
    stream=True,
    stream_options={"include_usage": True}
)

full_content = ""
for chunk in completion:
    if chunk.choices and chunk.choices[0].delta.content:
        # Append output content
        full_content += chunk.choices[0].delta.content
        print(chunk.model_dump())

print(full_content)

Model pricing

Model nameVersionContext lengthMax inputMax outputInput costOutput cost
(Tokens)(per 1 million tokens)
qwen-long-latest
Always has the same capabilities as the latest snapshot version.
Latest10,000,00010,000,00032,768$0.072$0.287
qwen-long-2025-01-25
Also known as qwen-long-0125.
Snapshot

FAQ

  1. Does the Qwen-Long model support submitting batch jobs? Yes. Qwen-Long supports the OpenAI Batch API at 50% of real-time call rates. Submit batch jobs as files; jobs run asynchronously and return results on completion or timeout.
  2. Where are files saved after they are uploaded using the OpenAI-compatible file API? Files are uploaded to your Model Studio bucket at no cost. See the OpenAI File API for querying and managing files.
  3. What is qwen-long-2025-01-25? This is a version snapshot frozen at a specific point in time. More stable than latest, with no expiration date.
  4. How can I know when a file has finished parsing? To check parsing progress without repeated trial calls, query the file's status: call the retrieve file interface and check the status field—processing means parsing is still in progress, processed means parsing is complete and the file can be referenced, and error means parsing failed. Once the status is processed, you can call the model. For details, see the OpenAI File API. Alternatively, call the model with the file-id directly: if parsing is incomplete, you'll get error 400: "File parsing in progress, please try again later."; a successful response means parsing is complete.
  5. How can I ensure the model outputs a JSON string in a standard format? qwen-long and all snapshots support structured output. Specify a JSON Schema to ensure valid JSON that matches your structure.

API reference

Refer to Qwen API details for the input and output parameters of the Qwen-Long model.

Error codes

If the model call fails and returns an error message, see Error codes for resolution.

Limits

  • SDK dependencies:
    • File operations (upload, delete, query) require an OpenAI-compatible SDK.
    • Invoke models using an OpenAI-compatible SDK or Dashscope SDK.
  • File upload:
    • Supported formats: TXT, DOCX, PDF, XLSX, EPUB, MOBI, MD, CSV, JSON, BMP, PNG, JPG/JPEG, and GIF.
    • File size: The maximum size for image files is 20 MB. The maximum size for other file formats is 150 MB.
    • Account quota: Maximum 10,000 files or 100 GB per account. Uploads fail when either limit is reached. Delete files to free quota. See OpenAI compatible - File.
    • Storage period: Currently, there is no expiration limit for stored files.
  • API inputs:
    • The first system message defines the role. The second contains document content or fileid://xxx. The user message contains the query.
    • When referencing files using a file-id, a single request can reference a maximum of 100 files.
    • With a second system message, user message limit is 9,000 tokens. No limit with only one system message.
    • The total context length is limited to 10 million tokens.
  • API outputs:
    • The maximum output length is 32,768 tokens.
  • File sharing:
    • file-ids are account-specific and cannot be used cross-account or with RAM user API keys.
  • Throttling: For information about model throttling conditions, see Throttling.
Token Plan
Statistics and Monitoring
Support