Skip to main content

PDF understanding

The PDF understanding feature enables models to parse text and images in PDF documents, and analyze them through Chat Completions, the Responses API, DashScope, or the Anthropic-compatible API.

Supported models

qwen4-max、qwen3.8-max、qwen3.8-max-0902、qwen3.8-flash、qwen3.8-27b qwen3.8-27b supports PDF understanding only in the Beijing region via Chat Completions or DashScope.

QuickStart

Run the following code to pass a PDF file to the model.
You must have already obtained and configured an API Key and configured the API Key as an environment variable.
  • OpenAI compatible
  • DashScope
  • Responses API
  • Anthropic
  • Python
  • Node.js
  • HTTP

Sample Code

from openai import OpenAI
import os

client = OpenAI(
    # If the environment variable is not configured, replace the following line with your Model Studio API key (not recommended): api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs differ by region.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    model="qwen4-max",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "file",
                    "file": {
                        "file_url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260616/qmycjl/1506.02640v5.pdf"
                    }
                },
                {
                    "type": "text",
                    "text": "总结一下这个PDF文档的内容"
                }
            ]
        }
    ],
    stream=True,
    stream_options={"include_usage": True},
    # Required to return usage.x_tools.pdf_page_parser.count (PDF parsed page count)
    extra_body={"include_tool_usage": True}
)

for chunk in completion:
    if not chunk.choices:
        print(f"\nUsage: {chunk.usage}")
        continue
    delta = chunk.choices[0].delta
    if hasattr(delta, "content") and delta.content:
        print(delta.content, end="", flush=True)

Use Base64 input

If you cannot provide the file's URL address, you can also pass the PDF file as a Base64-encoded string. When using file_data, the filename field is required.
Base64 encoding increases the data size by about 1/3. For example, a 150MB file becomes about 200MB after encoding, which exceeds the request body size limit. For large files, use the URL method instead.
  • Chat Completions / DashScope
  • Anthropic
import base64
from openai import OpenAI
import os

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs differ by region.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

# Read and encode the PDF file
with open("report.pdf", "rb") as f:
    pdf_base64 = base64.b64encode(f.read()).decode("utf-8")

completion = client.chat.completions.create(
    model="qwen4-max",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "file",
                    "file": {
                        "file_data": f"data:application/pdf;base64,{pdf_base64}",
                        "filename": "report.pdf"
                    }
                },
                {
                    "type": "text",
                    "text": "这份报告的核心结论是什么?"
                }
            ]
        }
    ]
)

print(completion.choices[0].message.content)

Request parameters

The following parameter table applies to Chat Completions. The Responses API uses type: "input_file" and file_url; the Anthropic-compatible API uses type: "document" and sets type: "base64", media_type: "application/pdf", and data (a Base64 string without the Data URI prefix) in source. DashScope passes files via elements in the content array that contain file_url or file_data. For URL input, type is required and fixed to document; source is a required object, where type is a required string set to url, and url is a required string specifying the PDF file URL.
In the protocol, the URL part supports only string input, not the list (array) form.
OpenAI-compatible protocol format:

Parameter

Type

Whether Required

Description

file_urlstringRequired (choose one of the two)The download address of the PDF file. Either this parameter or file_data is required.
file_datastringThe PDF file input in Base64 format, in the format data:application/pdf;base64,xxx. Either this parameter or file_url is required.
filenamestringConditionally requiredThe file name. Required when file_data is used as the input parameter.
file_formatstringNoThe file format. Optional. Currently only pdf is supported, and the default is pdf.

Limits

Item

Limit

Single file size limit150MB
Page limit per document256 pages
PDF parsing may take longer than plain text requests. The first-token timeout can be up to 300 s. We recommend using streaming output to obtain results in real time and avoid long waits.

Billing Details

Billing involves the following aspects:
  • Model invocation fees: The text and images parsed from the PDF file are counted toward the model's input tokens and billed at the model's standard input price.
  • Document parsing fees: Billed by the number of parsed PDF pages, with unit prices varying by region: China (Beijing) $0.003/page, Singapore $0.0033/page. Global deployment mode: $0.003/page.
For the input and output unit prices of each model, see Model invocation billing.

View the number of parsed PDF pages

The Responses API of qwen4-max uses the top-level tool_usage of the response, where num_requests of each tool is the number of billable calls, and pdf_page_parser.num_requests is the number of parsed pages. The Responses examples for Qwen3.8 still use usage.x_tools. Chat Completions and the Responses API (Qwen3.8) can pass in include_tool_usage: true at the top level of the request body and view the number of parsed pages from usage.x_tools.pdf_page_parser.count; for DashScope, view it from usage.plugins.pdf_page_parser.count. The number of parsed pages returned by the Anthropic-compatible API is located at the top level of the response in x_tools.pdf_page_parser.count. If the parsing count is not returned, it does not mean that no parsing fee was incurred for this request.
// 需在请求体顶层传入 "include_tool_usage": true 后才会返回。
// OpenAI Python SDK 请通过 extra_body={"include_tool_usage": True} 传入,
// 直接使用 HTTP/curl 时放在请求体顶层,不要嵌套到 extra_body 或 stream_options。
{
    "usage": {
        "x_tools": {
            "pdf_page_parser": {
                "count": 10,
                "strategy": "normal"
            }
        }
    }
}