> ## Documentation Index
> Fetch the complete documentation index at: https://docs.modelstudio.console.alibabacloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 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.

<Note>
  This document applies only to the Chinese mainland (Beijing) region. To use the model, you must use an API key from the[Chinese mainland (Beijing)](https://modelstudio.console.alibabacloud.com/model/settings/api-key) region.
</Note>

## How to use <span id="be65bccf62aa4" /> <span id="6f2da2ea12bda" />

Use Qwen-Long in two steps: upload files, then call the API.

1. <strong>File upload and parsing:</strong>

   - Upload a file using the API. For details about supported file formats and size limits, see [Supported formats](/en/model-studio/long-context-qwen-long).
   - After a successful upload, the system returns a <strong>unique</strong> `file-id` for your account and starts parsing. No fees are charged for file upload, storage, or parsing.
2. <strong>API call and billing:</strong>

   - When you call the model, reference one or more `file-id`s in the `system` message.
   - The model performs inference based on the text content associated with the `file-id`.
   - For <strong>each</strong> API call, the number of tokens in the referenced file content is counted as <strong>input tokens</strong> for that request.

This avoids transferring large files in each request, but note that file tokens are billed per API call.

## Getting started <span id="cdf6cfc2fe61o" /> <span id="72cee64e7ff13" />

### Prerequisites <span id="2d555a4dd61ev" /> <span id="2ba89b56edufp" />

- Obtain an [API key](/en/model-studio/get-api-key) and [configure it as an environment variable](/en/model-studio/get-api-key).
- To call the model via SDK, install the [OpenAI SDK](/en/model-studio/install-sdk).

### Upload a document <span id="887e6378b8mb6" /> <span id="f10289e2f7nza" />

This example uploads [Model\_Studio\_Phone\_Product\_Introduction.docx](https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/en-US/20250805/pzubre/Bailian+Phones+Specifications.docx) to Model Studio's secure storage via the OpenAI-compatible interface and gets a `file-id`. See the [API documentation](/en/model-studio/openai-file-interface) for upload parameters.

<CodeGroup dropdown>
  ```python 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)
  ```

  ```java Java expandable
  import com.openai.client.OpenAIClient;
  import com.openai.client.okhttp.OpenAIOkHttpClient;
  import com.openai.models.files.*;

  import java.nio.file.Path;
  import java.nio.file.Paths;

  public class Main {
      public static void main(String[] args) {
          // Create a client and use the API key from the environment variable.
          OpenAIClient client = OpenAIOkHttpClient.builder()
                  // If you have not configured the environment variable, replace the following line with: .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();
          // Set the file path. Modify the path and filename as needed.
          Path filePath = Paths.get("src/main/java/org/example/Model_Studio_Phone_Product_Introduction.docx");
          // Create file upload parameters.
          FileCreateParams fileParams = FileCreateParams.builder()
                  .file(filePath)
                  .purpose(FilePurpose.of("file-extract"))
                  .build();

          // Upload the file and print the file-id.
          FileObject fileObject = client.files().create(fileParams);
          System.out.println(fileObject.id());
      }
  }
  ```

  ```bash curl
  # The following URL is for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
  curl --location --request POST 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/files' \
    --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
    --form 'file=@"Alibaba Cloud Model Studio Phone Series Product Introduction.docx"' \
    --form 'purpose="file-extract"'
  ```
</CodeGroup>

Run the code to obtain the `file-id` for the uploaded file.

### Pass information and chat using a file ID <span id="7a45f527766p5" /> <span id="24a78d2ab9u48" />

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.

<CodeGroup dropdown>
  ```python Python expandable
  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/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/model-studio/error-code")
  ```

  ```java Java expandable
  import com.openai.client.OpenAIClient;
  import com.openai.client.okhttp.OpenAIOkHttpClient;
  import com.openai.core.http.StreamResponse;
  import com.openai.models.chat.completions.*;

  public class Main {
      public static void main(String[] args) {
          // Create a client and use the API key from the environment variable.
          OpenAIClient client = OpenAIOkHttpClient.builder()
                  // If you have not configured the environment variable, replace the following line with: .apiKey("sk-xxx")
                  .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                  // Endpoint 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 a chat request.
          ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
                  //sys1: Role definition.
                  .addSystemMessage("You are a helpful assistant.")
                  //sys2: Document content (plain text or file-id).
                  //Replace '{FILE_ID}' with the file-id used in your conversation.
                  .addSystemMessage("fileid://{FILE_ID}")
                  //When the request includes a second system message, the user message content is limited to 9,000 tokens.
                  .addUserMessage("What is this article about?")
                  .model("qwen-long")
                  .build();

          StringBuilder fullResponse = new StringBuilder();

          // All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
          try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
              streamResponse.stream().forEach(chunk -> {
                  // Print and concatenate the content of each chunk.
                  System.out.println(chunk);
                  String content = chunk.choices().get(0).delta().content().orElse("");
                  if (!content.isEmpty()) {
                      fullResponse.append(content);
                  }
              });
              System.out.println(fullResponse);
          } catch (Exception e) {
              System.err.println("Error: " + e.getMessage());
              System.err.println("See documentation: https://www.alibabacloud.com/help/model-studio/error-code");
          }
      }
  }
  ```

  ```bash curl
  # Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
  curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
  --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{
      "model": "qwen-long",
      "messages": [
          {"role": "system","content": "You are a helpful assistant."},
          {"role": "system","content": "fileid://file-fe-xxx"},
          {"role": "user","content": "What is this article about?"}
      ],
      "stream": true,
      "stream_options": {
          "include_usage": true
      }
  }'
  ```
</CodeGroup>

### Pass multiple documents <span id="7d4038fa1405o" /> <span id="9899c5c1520j2" />

Pass multiple `file-id`s in one system message or add separate system messages for each document.

<Tabs>
  <Tab title="Pass multiple documents">
    <CodeGroup dropdown>
      ```python Python expandable
      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/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/model-studio/error-code")
      ```

      ```java Java expandable
      import com.openai.client.OpenAIClient;
      import com.openai.client.okhttp.OpenAIOkHttpClient;
      import com.openai.core.http.StreamResponse;
      import com.openai.models.chat.completions.*;

      public class Main {
          public static void main(String[] args) {
              // Create a client and use the API key from the environment variable.
              OpenAIClient client = OpenAIOkHttpClient.builder()
                      .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                      // Endpoint 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 a chat request.
              ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
                      .addSystemMessage("You are a helpful assistant.")
                      //Replace '{FILE_ID1}' and '{FILE_ID2}' with the file-ids used in your conversation.
                      .addSystemMessage("fileid://{FILE_ID1},fileid://{FILE_ID2}")
                      .addUserMessage("What are these two articles about?")
                      .model("qwen-long")
                      .build();

              StringBuilder fullResponse = new StringBuilder();

              // All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
              try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
                  streamResponse.stream().forEach(chunk -> {
                      // The content of each chunk.
                      System.out.println(chunk);
                      String content = chunk.choices().get(0).delta().content().orElse("");
                      if (!content.isEmpty()) {
                          fullResponse.append(content);
                      }
                  });
                  System.out.println("\nFull response content:");
                  System.out.println(fullResponse);
              } catch (Exception e) {
                  System.err.println("Error: " + e.getMessage());
                  System.err.println("See documentation: https://www.alibabacloud.com/help/model-studio/error-code");
              }
          }
      }
      ```

      ```bash curl
      # Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
      curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
      --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
      --header "Content-Type: application/json" \
      --data '{
          "model": "qwen-long",
          "messages": [
              {"role": "system","content": "You are a helpful assistant."},
              {"role": "system","content": "fileid://file-fe-xxx1"},
              {"role": "system","content": "fileid://file-fe-xxx2"},
              {"role": "user","content": "What are these two articles about?"}
          ],
          "stream": true,
          "stream_options": {
              "include_usage": true
          }
      }'
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Append documents">
    <CodeGroup dropdown>
      ```python Python expandable
      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",
      )
      # Initialize the messages list.
      messages = [
          {'role': 'system', 'content': 'You are a helpful assistant.'},
          # Replace '{FILE_ID1}' with the file-id used in your conversation.
          {'role': 'system', 'content': f'fileid://{FILE_ID1}'},
          {'role': 'user', 'content': 'What is this article about?'}
      ]

      try:
          # First-round response
          completion_1 = client.chat.completions.create(
              model="qwen-long",
              messages=messages,
              stream=False
          )
          # Print first-round response.
          # To stream: set stream=True, concatenate segments, and pass to assistant_message content.
          print(f"First-round response: {completion_1.choices[0].message.model_dump()}")
      except BadRequestError as e:
          print(f"Error: {e}")
          print("See documentation: https://www.alibabacloud.com/help/model-studio/error-code")

      # Construct the assistant_message.
      assistant_message = {
          "role": "assistant",
          "content": completion_1.choices[0].message.content}

      # Add assistant_message to messages.
      messages.append(assistant_message)

      # Add the file-id of the appended document to messages.
      # Replace '{FILE_ID2}' with the file-id used in your conversation.
      system_message = {'role': 'system', 'content': f'fileid://{FILE_ID2}'}
      messages.append(system_message)

      # Add the user's question.
      messages.append({'role': 'user', 'content': 'What are the similarities and differences between the methods discussed in these two articles?'})

      # Response after appending the document.
      completion_2 = client.chat.completions.create(
          model="qwen-long",
          messages=messages,
          # All code examples use streaming output to clearly and intuitively show the model output process. For non-streaming output examples, see https://www.alibabacloud.com/help/model-studio/text-generation
          stream=True,
          stream_options={
              "include_usage": True
          }
      )

      # Stream and print the response after appending the document.
      print("Response after appending the document:")
      for chunk in completion_2:
          print(chunk.model_dump())
      ```

      ```java Java expandable
      import com.openai.client.OpenAIClient;
      import com.openai.client.okhttp.OpenAIOkHttpClient;
      import com.openai.models.chat.completions.*;
      import com.openai.core.http.StreamResponse;

      import java.util.ArrayList;
      import java.util.List;

      public class Main {
          public static void main(String[] args) {
              OpenAIClient client = OpenAIOkHttpClient.builder()
                      .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                      // Endpoint 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();
              // Initialize messages list.
              List<ChatCompletionMessageParam> messages = new ArrayList<>();

              // Add information for role setting.
              ChatCompletionSystemMessageParam roleSet = ChatCompletionSystemMessageParam.builder()
                      .content("You are a helpful assistant.")
                      .build();
              messages.add(ChatCompletionMessageParam.ofSystem(roleSet));

              // Replace '{FILE_ID1}' with the file-id used in your conversation.
              ChatCompletionSystemMessageParam systemMsg1 = ChatCompletionSystemMessageParam.builder()
                      .content("fileid://{FILE_ID1}")
                      .build();
              messages.add(ChatCompletionMessageParam.ofSystem(systemMsg1));

              // User question message (USER role).
              ChatCompletionUserMessageParam userMsg1 = ChatCompletionUserMessageParam.builder()
                      .content("Please summarize the article content.")
                      .build();
              messages.add(ChatCompletionMessageParam.ofUser(userMsg1));

              // Construct the first-round request and handle exceptions.
              ChatCompletion completion1;
              try {
                  completion1 = client.chat().completions().create(
                          ChatCompletionCreateParams.builder()
                                  .model("qwen-long")
                                  .messages(messages)
                                  .build()
                  );
              } catch (Exception e) {
                  System.err.println("Request error. See error code page:");
                  System.err.println("https://www.alibabacloud.com/help/model-studio/error-code");
                  System.err.println("Error details: " + e.getMessage());
                  e.printStackTrace();
                  return;
              }

              // First-round response.
              String firstResponse = completion1 != null ? completion1.choices().get(0).message().content().orElse("") : "";
              System.out.println("First-round response: " + firstResponse);

              // Construct AssistantMessage.
              ChatCompletionAssistantMessageParam assistantMsg = ChatCompletionAssistantMessageParam.builder()
                      .content(firstResponse)
                      .build();
              messages.add(ChatCompletionMessageParam.ofAssistant(assistantMsg));

              // Replace '{FILE_ID2}' with the file-id used in your conversation.
              ChatCompletionSystemMessageParam systemMsg2 = ChatCompletionSystemMessageParam.builder()
                      .content("fileid://{FILE_ID2}")
                      .build();
              messages.add(ChatCompletionMessageParam.ofSystem(systemMsg2));

              // Second-round user question (USER role).
              ChatCompletionUserMessageParam userMsg2 = ChatCompletionUserMessageParam.builder()
                      .content("Please compare the structural differences between the two articles.")
                      .build();
              messages.add(ChatCompletionMessageParam.ofUser(userMsg2));

              // All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
              StringBuilder fullResponse = new StringBuilder();
              try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(
                      ChatCompletionCreateParams.builder()
                              .model("qwen-long")
                              .messages(messages)
                              .build())) {

                  streamResponse.stream().forEach(chunk -> {
                      String content = chunk.choices().get(0).delta().content().orElse("");
                      if (!content.isEmpty()) {
                          fullResponse.append(content);
                      }
                  });
                  System.out.println("\nFinal response:");
                  System.out.println(fullResponse.toString().trim());
              } catch (Exception e) {
                  System.err.println("Error: " + e.getMessage());
                  System.err.println("See documentation: https://www.alibabacloud.com/help/model-studio/error-code");
              }
          }
      }
      ```

      ```bash curl
      # Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
      curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
      --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
      --header "Content-Type: application/json" \
      --data '{
          "model": "qwen-long",
          "messages": [
              {"role": "system","content": "You are a helpful assistant."},
              {"role": "system","content": "fileid://file-fe-xxx1"},
              {"role": "user","content": "What is this article about?"},
              {"role": "system","content": "fileid://file-fe-xxx2"},
              {"role": "user","content": "What are the similarities and differences between the methods discussed in these two articles?"}
          ],
          "stream": true,
          "stream_options": {
              "include_usage": true
          }
      }'
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## Pass information as plain text <span id="5e5ece4be56s3" /> <span id="105f283a359ba" />

Instead of using `file-id`s, 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.

<Tabs>
  <Tab title="Simple example">
    You can input the document content directly into the System Message.

    <CodeGroup dropdown>
      ```python Python expandable
      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/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)
      ```

      ```java Java expandable
      import com.openai.client.OpenAIClient;
      import com.openai.client.okhttp.OpenAIOkHttpClient;
      import com.openai.core.http.StreamResponse;

      import com.openai.models.chat.completions.*;

      public class Main {
          public static void main(String[] args) {
              // Create a client using the API key from the environment variable
              OpenAIClient client = OpenAIOkHttpClient.builder()
                      .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                      // Endpoint 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 a chat request
              ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
                      .addSystemMessage("You are a helpful assistant.")
                      .addSystemMessage("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...")
                      .addUserMessage("What does this article talk about?")
                      .model("qwen-long")
                      .build();

              StringBuilder fullResponse = new StringBuilder();

              // All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
              try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
                  streamResponse.stream().forEach(chunk -> {
                      // Print and append each chunk's content
                      System.out.println(chunk);
                      String content = chunk.choices().get(0).delta().content().orElse("");
                      if (!content.isEmpty()) {
                          fullResponse.append(content);
                      }
                  });
                  System.out.println(fullResponse);
              } catch (Exception e) {
                  System.err.println("Error: " + e.getMessage());
                  System.err.println("For more information, see https://www.alibabacloud.com/help/model-studio/error-code");
              }
          }
      }
      ```

      ```bash curl
      # Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
      curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
      --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
      --header "Content-Type: application/json" \
      --data '{
          "model": "qwen-long",
          "messages": [
              {"role": "system","content": "You are a helpful assistant."},
              {"role": "system","content": "Alibaba Cloud Model Studio X1 —— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate, ..."},
              {"role": "user","content": "What does this article talk about?"}
          ],
          "stream": true,
          "stream_options": {
              "include_usage": true
          }
      }'
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Pass multiple documents">
    To pass multiple documents in a single conversation turn, place the content of each document in a separate System Message.

    <CodeGroup dropdown>
      ```python Python expandable
      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 X1————Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate...'},
              {'role': 'system', 'content': 'Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design...'},
              {'role': 'user', 'content': 'What are the similarities and differences between the products discussed in these two articles?'}
          ],
          # 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/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)
      ```

      ```java Java expandable
      import com.openai.client.OpenAIClient;
      import com.openai.client.okhttp.OpenAIOkHttpClient;
      import com.openai.core.http.StreamResponse;

      import com.openai.models.chat.completions.*;

      public class Main {
          public static void main(String[] args) {
              // Create a client using the API key from the environment variable
              OpenAIClient client = OpenAIOkHttpClient.builder()
                      .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                      // Endpoint 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 a chat request
              ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
                      .addSystemMessage("You are a helpful assistant.")
                      .addSystemMessage("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...")
                      .addSystemMessage("Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design...")
                      .addUserMessage("What are the similarities and differences between the products discussed in these two articles?")
                      .model("qwen-long")
                      .build();

              StringBuilder fullResponse = new StringBuilder();

              // All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
              try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
                  streamResponse.stream().forEach(chunk -> {
                      // Print and append each chunk's content
                      System.out.println(chunk);
                      String content = chunk.choices().get(0).delta().content().orElse("");
                      if (!content.isEmpty()) {
                          fullResponse.append(content);
                      }
                  });
                  System.out.println(fullResponse);
              } catch (Exception e) {
                  System.err.println("Error: " + e.getMessage());
                  System.err.println("For more information, see https://www.alibabacloud.com/help/model-studio/error-code");
              }
          }
      }
      ```

      ```bash curl
      # Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
      curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
      --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
      --header "Content-Type: application/json" \
      --data '{
          "model": "qwen-long",
          "messages": [
              {"role": "system","content": "You are a helpful assistant."},
              {"role": "system","content": "Alibaba Cloud Model Studio X1 —— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate..."},
              {"role": "system","content": "Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design..."},
              {"role": "user","content": "What are the similarities and differences between the products discussed in these two articles?"}
          ],
          "stream": true,
          "stream_options": {
              "include_usage": true
          }
      }'
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Append documents">
    During your interaction with the model, you might need to add new document information. To do this, append the new document content as a System Message to the Messages array.

    <CodeGroup dropdown>
      ```python Python expandable
      import os
      from openai import OpenAI, BadRequestError

      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
      messages = [
          {'role': 'system', 'content': 'You are a helpful assistant.'},
          {'role': 'system', 'content': 'Alibaba Cloud Model Studio X1 —— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate...'},
          {'role': 'user', 'content': 'What does this article talk about?'}
      ]

      try:
          # First-round response
          completion_1 = client.chat.completions.create(
              model="qwen-long",
              messages=messages,
              stream=False
          )
          # Print the first-round response
          # For streaming output in the first round, set stream=True and concatenate each segment's content. Pass the concatenated string as the content when constructing assistant_message
          print(f"First-round response: {completion_1.choices[0].message.model_dump()}")
      except BadRequestError as e:
          print(f"Error: {e}")
          print("For more information, see https://www.alibabacloud.com/help/model-studio/error-code")

      # Construct assistant_message
      assistant_message = {
          "role": "assistant",
          "content": completion_1.choices[0].message.content}

      # Append assistant_message to messages
      messages.append(assistant_message)
      # Append new document content to messages
      system_message = {
          'role': 'system',
          'content': 'Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design, delivering an immersive visual experience...'}
      messages.append(system_message)

      # Add user question
      messages.append({
          'role': 'user',
          'content': 'What are the similarities and differences between the products discussed in these two articles?'
      })

      # Response after appending the document
      completion_2 = client.chat.completions.create(
          model="qwen-long",
          messages=messages,
          # 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/model-studio/text-generation
          stream=True,
          stream_options={"include_usage": True}
      )

      # Stream and print the response after appending the document
      print("Response after appending the document:")
      for chunk in completion_2:
          print(chunk.model_dump())
      ```

      ```java Java expandable
      import com.openai.client.OpenAIClient;
      import com.openai.client.okhttp.OpenAIOkHttpClient;
      import com.openai.models.chat.completions.*;
      import com.openai.core.http.StreamResponse;

      import java.util.ArrayList;
      import java.util.List;

      public class Main {
          public static void main(String[] args) {
              OpenAIClient client = OpenAIOkHttpClient.builder()
                      .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                      // Endpoint 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();
              // Initialize the messages list
              List<ChatCompletionMessageParam> messages = new ArrayList<>();

              // Add role-setting information
              ChatCompletionSystemMessageParam roleSet = ChatCompletionSystemMessageParam.builder()
                      .content("You are a helpful assistant.")
                      .build();
              messages.add(ChatCompletionMessageParam.ofSystem(roleSet));

              // First-round content
              ChatCompletionSystemMessageParam systemMsg1 = ChatCompletionSystemMessageParam.builder()
                      .content("Alibaba Cloud Model Studio X1 —— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate, 256GB storage, 12GB RAM, and a 5000mAh long-lasting battery...")
                      .build();
              messages.add(ChatCompletionMessageParam.ofSystem(systemMsg1));

              // User question (USER role)
              ChatCompletionUserMessageParam userMsg1 = ChatCompletionUserMessageParam.builder()
                      .content("Please summarize the article content")
                      .build();
              messages.add(ChatCompletionMessageParam.ofUser(userMsg1));

              // Build the first-round request and handle exceptions
              ChatCompletion completion1;
              try {
                  completion1 = client.chat().completions().create(
                          ChatCompletionCreateParams.builder()
                                  .model("qwen-long")
                                  .messages(messages)
                                  .build()
                  );
              } catch (Exception e) {
                  System.err.println("Error: " + e.getMessage());
                  System.err.println("For more information, see https://www.alibabacloud.com/help/model-studio/error-code");
                  e.printStackTrace();
                  return;
              }

              // First-round response
              String firstResponse = completion1 != null ? completion1.choices().get(0).message().content().orElse("") : "";
              System.out.println("First-round response: " + firstResponse);

              // Construct AssistantMessage
              ChatCompletionAssistantMessageParam assistantMsg = ChatCompletionAssistantMessageParam.builder()
                      .content(firstResponse)
                      .build();
              messages.add(ChatCompletionMessageParam.ofAssistant(assistantMsg));

              // Second-round content
              ChatCompletionSystemMessageParam systemMsg2 = ChatCompletionSystemMessageParam.builder()
                      .content("Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design, delivering an immersive visual experience...")
                      .build();
              messages.add(ChatCompletionMessageParam.ofSystem(systemMsg2));

              // Second-round user question (USER role)
              ChatCompletionUserMessageParam userMsg2 = ChatCompletionUserMessageParam.builder()
                      .content("Please compare the structural differences between the two descriptions")
                      .build();
              messages.add(ChatCompletionMessageParam.ofUser(userMsg2));

              // All examples use streaming output to show the model's response process. For non-streaming examples, see https://www.alibabacloud.com/help/model-studio/text-generation
              StringBuilder fullResponse = new StringBuilder();
              try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(
                      ChatCompletionCreateParams.builder()
                              .model("qwen-long")
                              .messages(messages)
                              .build())) {

                  streamResponse.stream().forEach(chunk -> {
                      String content = chunk.choices().get(0).delta().content().orElse("");
                      if (!content.isEmpty()) {
                          fullResponse.append(content);
                      }
                  });
                  System.out.println("\nFinal response:");
                  System.out.println(fullResponse.toString().trim());
              } catch (Exception e) {
                  System.err.println("Error: " + e.getMessage());
                  System.err.println("For more information, see https://www.alibabacloud.com/help/model-studio/error-code");
              }
          }
      }
      ```

      ```bash curl
      # Endpoint for the China (Beijing) region. Replace {WorkspaceId} with your Workspace ID. URLs vary by region.
      curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
      --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
      --header "Content-Type: application/json" \
      --data '{
          "model": "qwen-long",
          "messages": [
                  {"role": "system","content": "You are a helpful assistant."},
                  {"role": "system","content": "Alibaba Cloud Model Studio X1 —— Enjoy an ultimate visual experience: features a 6.7-inch 1440 x 3200 pixel ultra-clear screen with a 120Hz refresh rate..."},
                  {"role": "user","content": "What does this article talk about?"},
                  {"role": "system","content": "Stardust S9 Pro —— A revolutionary visual feast: breakthrough 6.9-inch 1440 x 3088 pixel under-display camera design, delivering an immersive visual experience..."},
                  {"role": "user","content": "What are the similarities and differences between the products discussed in these two articles"}
              ],
          "stream": true,
          "stream_options": {
              "include_usage": true
          }
      }'
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## Model pricing <span id="eb5a1ae09ec37" /> <span id="1b35a93d4c46f" />

<table style={{ display: "table", tableLayout: "fixed", width: "100%" }}>
  <colgroup>
    <col style={{ width: "22.383721%" }} />

    <col style={{ width: "8.866279%" }} />

    <col style={{ width: "13.517442%" }} />

    <col style={{ width: "13.517442%" }} />

    <col style={{ width: "8.866279%" }} />

    <col style={{ width: "17.44186%" }} />

    <col style={{ width: "15.406977%" }} />
  </colgroup>

  <thead>
    <tr>
      <th rowSpan={2}>
        <strong>Model name</strong>
      </th>

      <th rowSpan={2}>
        <strong>Version</strong>
      </th>

      <th>
        <strong>Context length</strong>
      </th>

      <th>
        <strong>Max input</strong>
      </th>

      <th>
        <strong>Max output</strong>
      </th>

      <th>
        <strong>Input cost</strong>
      </th>

      <th>
        <strong>Output cost</strong>
      </th>
    </tr>

    <tr>
      <th colSpan={3}>
        <strong>(Tokens)</strong>
      </th>

      <th colSpan={2}>
        <strong>(per 1 million tokens)</strong>
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        <strong>qwen-long-latest</strong>

        > Always has the same capabilities as the latest snapshot version.
      </td>

      <td>
        Latest
      </td>

      <td rowSpan={2}>
        10,000,000
      </td>

      <td rowSpan={2}>
        10,000,000
      </td>

      <td rowSpan={2}>
        32,768
      </td>

      <td rowSpan={2}>
        \$0.072
      </td>

      <td rowSpan={2}>
        \$0.287
      </td>
    </tr>

    <tr>
      <td>
        qwen-long-2025-01-25

        > Also known as qwen-long-0125.
      </td>

      <td>
        Snapshot
      </td>
    </tr>
  </tbody>
</table>

## FAQ <span id="43e63b66b2i5s" /> <span id="d88f1e3e68u8q" />

1. Does the Qwen-Long model support submitting batch jobs?

   Yes. Qwen-Long supports the [OpenAI Batch API](/en/model-studio/batch-interfaces-compatible-with-openai) at 50% of real-time call rates. Submit batch jobs as files; jobs run <strong>asynchronously</strong> 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](/en/model-studio/openai-file-interface) 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 ensure the model outputs a JSON string in a standard format?

   `qwen-long` and all snapshots support [structured output](/en/model-studio/qwen-structured-output). Specify a JSON Schema to ensure valid JSON that matches your structure.

## API reference <span id="a059c25c159a5" /> <span id="fa9e326f8btcw" />

Refer to [Qwen API details](/en/model-studio/qwen-api-reference) for the input and output parameters of the Qwen-Long model.

## Error codes <span id="1c0fa4611a1h1" /> <span id="2eff15e6b81b7" />

If the model call fails and returns an error message, see [Error codes](/en/model-studio/error-code) for resolution.

## Limits <span id="4798ba361eox3" /> <span id="2583d92b804v3" />

- <strong>SDK dependencies:</strong>

  - File operations (upload, delete, query) require an OpenAI-compatible SDK.
  - Invoke models using an OpenAI-compatible SDK or Dashscope SDK.
- <strong>File upload:</strong>

  - 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](/en/model-studio/openai-file-interface).
  - Storage period: Currently, there is no expiration limit for stored files.
- <strong>API inputs:</strong>

  - 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.
- <strong>API outputs:</strong>

  - The maximum output length is 32,768 tokens.
- <strong>File sharing:</strong>

  - `file-id`s are account-specific and cannot be used cross-account or with RAM user API keys.
- <strong>Throttling</strong>: For information about model throttling conditions, see [Throttling](/en/model-studio/rate-limit).
