OpenAI Responses API 参考

更新时间:
复制为 MD 格式

本文介绍如何通过兼容 OpenAI 格式的 Responses API 调用通义千问模型,包括输入输出参数说明及调用示例。

相较于OpenAI Chat Completions API 的优势:

  • 内置工具:内置联网搜索、网页抓取、代码解释器、文搜图、图搜图、知识库搜索等工具,可在处理复杂任务时获得更优效果,详情参考工具调用

  • 更灵活的输入:支持直接传入字符串作为模型输入,也兼容 Chat 格式的消息数组。

  • 简化上下文管理:通过传递上一轮响应的 previous_response_id,无需手动构建完整的消息历史数组。

兼容性说明与限制

本 API 在接口设计上兼容 OpenAI,以降低开发者迁移成本,但在参数、功能和具体行为上存在差异。

核心原则:请求将仅处理本文档明确列出的参数,任何未提及的 OpenAI 参数都会被忽略。

以下是几个关键的差异点,以帮助您快速适配:

  • 部分参数不支持:不支持部分 OpenAI Responses API 参数,例如系统消息指令instructions、异步执行参数background(当前仅支持同步调用)等。

  • 额外支持的参数:本 API 还额外支持 enable_thinking 等参数,具体用法请参考相应参数的说明。

华北2(北京)

SDK 调用配置的base_urlhttps://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1

HTTP 请求地址:POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses

新加坡

SDK 调用配置的base_urlhttps://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1

HTTP 请求地址:POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses

请求体

基础调用

Python

import os
from openai import OpenAI

client = OpenAI(
    # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)

response = client.responses.create(
    model="qwen3.5-plus",
    input="你能做些什么?"
)

# 获取模型回复
print(response.output_text)

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    // 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey: "sk-xxx"
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    const response = await openai.responses.create({
        model: "qwen3.5-plus",
        input: "你能做些什么?"
    });

    // 获取模型回复
    console.log(response.output_text);
}

main();

curl

curl -X POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3.5-plus",
    "input": "你能做些什么?"
}'

流式输出

Python

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)

stream = client.responses.create(
    model="qwen3.5-plus",
    input="请简单介绍一下人工智能。",
    stream=True
)

print("开始接收流式输出:")
for event in stream:
    if event.type == 'response.output_text.delta':
        print(event.delta, end='', flush=True)
    elif event.type == 'response.completed':
        print("\n流式输出完成")
        print(f"总Token数: {event.response.usage.total_tokens}")

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    const stream = await openai.responses.create({
        model: "qwen3.5-plus",
        input: "请简单介绍一下人工智能。",
        stream: true
    });

    console.log("开始接收流式输出:");
    for await (const event of stream) {
        if (event.type === 'response.output_text.delta') {
            process.stdout.write(event.delta);
        } else if (event.type === 'response.completed') {
            console.log("\n流式输出完成");
            console.log(`总Token数: ${event.response.usage.total_tokens}`);
        }
    }
}

main();

curl

curl -X POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
--no-buffer \
-d '{
    "model": "qwen3.5-plus",
    "input": "请简单介绍一下人工智能。",
    "stream": true
}'

多轮对话

Python

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)

# 第一轮对话
response1 = client.responses.create(
    model="qwen3.5-plus",
    input="我的名字是张三,请记住。"
)
print(f"第一轮回复: {response1.output_text}")

# 第二轮对话 - 使用 previous_response_id 关联上下文,响应id有效期为7天
response2 = client.responses.create(
    model="qwen3.5-plus",
    input="你还记得我的名字吗?",
    previous_response_id=response1.id
)
print(f"第二轮回复: {response2.output_text}")

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    // 第一轮对话
    const response1 = await openai.responses.create({
        model: "qwen3.5-plus",
        input: "我的名字是张三,请记住。"
    });
    console.log(`第一轮回复: ${response1.output_text}`);

    // 第二轮对话 - 使用 previous_response_id 关联上下文,响应id有效期为7天
    const response2 = await openai.responses.create({
        model: "qwen3.5-plus",
        input: "你还记得我的名字吗?",
        previous_response_id: response1.id
    });
    console.log(`第二轮回复: ${response2.output_text}`);
}

main();

调用内置工具

Python

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)

response = client.responses.create(
    model="qwen3.5-plus",
    input="帮我找一下阿里云官网,并提取首页的关键信息",
    # 建议同时开启内置工具以取得最佳效果
    tools=[
        {"type": "web_search"},
        {"type": "code_interpreter"},
        {"type": "web_extractor"}
    ],
    extra_body={"enable_thinking": True}
)

# 取消以下注释查看中间过程输出
# print(response.output)
print(response.output_text)

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    const response = await openai.responses.create({
        model: "qwen3.5-plus",
        input: "帮我找一下阿里云官网,并提取首页的关键信息",
        tools: [
            { type: "web_search" },
            { type: "code_interpreter" },
            { type: "web_extractor" }
        ],
        enable_thinking: true
    });

    for (const item of response.output) {
        if (item.type === "reasoning") {
            console.log("模型正在思考...");
        } else if (item.type === "web_search_call") {
            console.log(`搜索查询: ${item.action.query}`);
        } else if (item.type === "web_extractor_call") {
            console.log("正在抽取网页内容...");
        } else if (item.type === "message") {
            console.log(`回复内容: ${item.content[0].text}`);
        }
    }
}

main();

curl

curl -X POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3.5-plus",
    "input": "帮我找一下阿里云官网,并提取首页的关键信息",
    "tools": [
        {
            "type": "web_search"
        },
        {
            "type": "code_interpreter"
        },
        {
            "type": "web_extractor"
        }
    ],
    "enable_thinking": true
}'

model string (必选)

模型名称。当前支持 qwen3-maxqwen3-max-2026-01-23qwen3.5-plusqwen3.5-plus-2026-02-15qwen3.5-397b-a17b

input string 或 array (必选)

模型输入,支持以下格式:

  • string:纯文本输入,如 "你好"

  • array:消息数组,按对话顺序排列。

array消息类型

System Message object (可选)

系统消息,用于设定模型的角色、语气、任务目标或约束条件等。如需使用,必须作为消息数组的第一个元素。

属性

role string (必选)

消息角色,固定为 system

content string (必选)

系统指令内容,用于明确模型的角色、行为规范、回答风格和任务约束等。

User Message object (必选)

用户消息,用于向模型传递问题、指令或上下文等。

属性

role string (必选)

消息角色,固定为 user

content string 或 array (必选)

消息内容。若输入只有文本,则为 string 类型;若输入包含图像或启用显式缓存,则为 array 类型。

当前Responses API暂不支持传入视频或语音,您可以通过Chat Completions APIDashScope API传入。

属性

type string (必选)

可选值:

  • text

    输入文本时需设为text

  • image_url

    输入图片时需设为image_url

text string

输入的文本。当typetext时,是必选参数。

image_url string

输入图片的公网 URLBase64编码数据。当typeimage_url时是必选参数。传入本地文件请参考图像与视频理解上传文件获取临时URL

Assistant Message object (可选)

助手消息,包含模型之前的回复。在多轮对话中用于提供上下文。

属性

role string (必选)

消息角色,固定为 assistant

content string (必选)

助手回复的文本内容。

previous_response_id string (可选)

上一个响应的唯一 ID,当前响应id有效期为7天。使用此参数可创建多轮对话,服务端会自动检索并组合该轮次的输入与输出作为上下文。当同时提供 input 消息数组和 previous_response_id 时,input 中的新消息会追加到历史上下文之后。

stream boolean (可选)默认值为 false

是否开启流式输出。设置为 true 时,模型响应数据将实时流式返回给客户端。

tools array (可选)

模型在生成响应时可调用的工具数组。支持内置工具和自定义 function 工具,可混合使用。

为了获得最佳回复效果,建议同时开启 code_interpreterweb_searchweb_extractor 工具。

属性

web_search

联网搜索工具,允许模型搜索互联网上的最新信息。相关文档:联网搜索

属性

type string (必选)

固定为web_search

使用示例:[{"type": "web_search"}]

web_extractor

网页抽取工具,允许模型访问并提取网页内容。当前必须配合web_search工具一起使用。qwen3-maxqwen3-max-2026-01-23需要同时开启思考模式。相关文档:网页抓取

属性

type string (必选)

固定为web_extractor

使用示例:[{"type": "web_search"}, {"type": "web_extractor"}]

code_interpreter

代码解释器工具,允许模型执行代码并返回结果,支持数据分析。qwen3-maxqwen3-max-2026-01-23需要同时开启思考模式。相关文档:代码解释器

属性

type string (必选)

固定为code_interpreter

使用示例:[{"type": "code_interpreter"}]

web_search_image

根据文本描述搜索图片。相关文档:文搜图

属性

type string (必选)

固定为web_search_image

使用示例:[{"type": "web_search_image"}]

image_search

根据图片搜索相似或相关图片,输入中需要包含图片的URL。相关文档:图搜图

属性

type string (必选)

固定为image_search

使用示例:[{"type": "image_search"}]

file_search

在已上传或关联的知识库中搜索。相关文档:知识检索

属性

type string (必选)

固定为file_search

vector_store_ids array (必选)

要检索的知识库 ID。当前仅支持传入一个知识库 ID

使用示例:[{"type": "file_search", "vector_store_ids": ["your_knowledge_base_id"]}]

MCP调用

通过 MCP(Model Context Protocol)调用外部服务,相关文档:MCP

属性

type string (必选)

固定为mcp

server_protocol string (必选)

与 MCP 服务的通信协议,如 "sse"

server_label string (必选)

服务标签,用于标识该 MCP 服务。

server_description string (可选)

服务描述,帮助模型理解其功能与适用场景。

server_url string (必选)

MCP 服务端点的 URL。

headers object (可选)

请求头,用于携带身份验证等信息,如 Authorization

使用示例:

mcp_tool = {
    "type": "mcp",
    "server_protocol": "sse",
    "server_label": "amap-maps",
    "server_description": "高德地图MCP Server现已覆盖15大核心接口,提供全场景覆盖的地理信息服务,包括生成专属地图、导航到目的地、打车、地理编码、逆地理编码、IP定位、天气查询、骑行路径规划、步行路径规划、驾车路径规划、公交路径规划、距离测量、关键词搜索、周边搜索、详情搜索等。",
    "server_url": "https://dashscope.aliyuncs.com/api/v1/mcps/amap-maps/sse",
    "headers": {
        "Authorization": "Bearer <your-mcp-server-token>"
    }
}

自定义工具 function

自定义函数工具,允许模型调用您定义的函数。当模型判断需要调用工具时,响应会返回 function_call 类型的输出。相关文档:Function Calling

属性

type string (必选)

必须设置为function

name string (必选)

工具名称。仅允许字母、数字、下划线(_)和短划线(-),最长 64 个 Token。

description string (必选)

工具描述信息,帮助模型判断何时以及如何调用该工具。

parameters object (可选)

工具的参数描述,需要是一个合法的 JSON Schema。若parameters参数为空,表示该工具没有入参(如时间查询工具)。

为提高工具调用的准确性,建议传入 parameters

使用示例:

[{
  "type": "function",
  "name": "get_weather",
  "description": "获取指定城市的天气信息",
  "parameters": {
    "type": "object",
    "properties": {
      "city": {
        "type": "string",
        "description": "城市名称"
      }
    },
    "required": ["city"]
  }
}]

tool_choice string or object (可选)默认值为 auto

控制模型如何选择和调用工具。此参数支持两种赋值格式:字符串模式对象模式

字符串模式

  • auto:模型自动决定是否调用工具。

  • none:禁止模型调用任何工具。

  • required:强制模型调用工具(仅当 tools 列表中只有一个工具时可用)。

对象模式

为模型设定可用的工具范围,仅限在预定义的工具列表中进行选择和调用。

属性

mode string (必选)

  • auto:模型自动决定是否调用工具。

  • required:强制模型调用工具(仅当 tools 列表中只有一个工具时可用)。

tools array(必选)

一个包含工具定义的列表,模型将被允许调用这些工具。

[
  { "type": "function", "name": "get_weather" }
]

type string (必选)

允许的工具配置类型,固定为 allowed_tools

temperature float (可选)

采样温度,控制模型生成文本的多样性。

temperature越高,生成的文本更多样,反之,生成的文本更确定。

取值范围: [0, 2)

temperaturetop_p均可以控制生成文本的多样性,建议只设置其中一个值。更多说明,请参见文本生成模型概述

top_p float (可选)

核采样的概率阈值,控制模型生成文本的多样性。

top_p越高,生成的文本更多样。反之,生成的文本更确定。

取值范围:(0,1.0]

temperaturetop_p均可以控制生成文本的多样性,建议只设置其中一个值。更多说明,请参见文本生成模型概述

enable_thinking boolean (可选)

是否开启思考模式。开启后,模型会在回复前进行思考,思考内容将通过 reasoning 类型的输出项返回。开启思考模式时,建议开启内置工具,以在处理复杂任务时获得最佳的模型效果。

可选值:

  • true:开启

  • false:不开启

不同模型的默认值:支持的模型

该参数非OpenAI标准参数。Python SDK 通过 extra_body={"enable_thinking": True} 传递;Node.js SDK 和 curl 直接使用 enable_thinking: true 作为顶层参数。

Response 响应对象(非流式输出)

{
    "created_at": 1771165743,
    "id": "c9f9c06b-032d-4525-a422-ac8ab5eccxxx",
    "model": "qwen3.5-plus",
    "object": "response",
    "output": [
        {
            "content": [
                {
                    "annotations": [],
                    "text": "你好!我是 Qwen3.5,阿里巴巴最新推出的通义千问大语言模型,具备强大的语言理解、逻辑推理、代码生成及多模态处理能力,旨在为用户提供精准高效的智能服务。",
                    "type": "output_text"
                }
            ],
            "id": "msg_544b2907-e88e-40d2-9a83-c30d6d1f9xxx",
            "role": "assistant",
            "status": "completed",
            "type": "message"
        }
    ],
    "parallel_tool_calls": false,
    "status": "completed",
    "tool_choice": "auto",
    "tools": [],
    "usage": {
        "input_tokens": 55,
        "input_tokens_details": {
            "cached_tokens": 0
        },
        "output_tokens": 43,
        "output_tokens_details": {
            "reasoning_tokens": 0
        },
        "total_tokens": 98,
        "x_details": [
            {
                "input_tokens": 55,
                "output_tokens": 43,
                "total_tokens": 98,
                "x_billing_type": "response_api"
            }
        ]
    }
}

id string

本次响应的唯一标识符,为 UUID 格式的字符串,有效期为7天。可用于 previous_response_id 参数以创建多轮对话。

created_at integer

本次请求的 Unix 时间戳(秒)。

object string

对象类型,固定为 response

status string

响应生成的状态。枚举值:

  • completed:生成完成

  • failed:生成失败

  • in_progress:生成中

  • cancelled:已取消

  • queued:请求排队中

  • incomplete:生成不完整

model string

用于生成响应的模型 ID。

output array

模型生成的输出项数组。数组中的元素类型和顺序取决于模型的响应。

数组元素属性

type string

输出项类型。枚举值:

  • message:消息类型,包含模型最终生成的回复内容。

  • reasoning:推理类型,开启思考模式(enable_thinking: true)时返回。推理 Token 会被计入 output_tokens_details.reasoning_tokens 中,按推理 Token 计费。

  • function_call:函数调用类型,使用自定义 function 工具时返回。需要处理函数调用并返回结果。

  • web_search_call:搜索调用类型,使用 web_search 工具时返回。

  • code_interpreter_call:代码执行类型,使用 code_interpreter 工具时返回。

  • web_extractor_call:网页抽取类型,使用 web_extractor 工具时返回。需要配合 web_search 工具一起使用。

  • web_search_image_call:文搜图调用类型,使用 web_search_image 工具时返回。包含搜索到的图片列表。

  • image_search_call:图搜图调用类型,使用 image_search 工具时返回。包含搜索到的相似图片列表。

  • mcp_call:MCP 调用类型,使用 mcp 工具时返回。包含 MCP 服务的调用结果。

  • file_search_call:知识库搜索调用类型,使用 file_search 工具时返回。包含知识库的检索查询和结果。

id string

输出项的唯一标识符。所有类型的输出项都包含此字段。

role string

消息角色,固定为 assistant。仅当 typemessage 时存在。

status string

输出项状态。可选值:completed(完成)、in_progress(生成中)。当 type bu时存在。

name string

工具或函数名称。当 typefunction_callweb_search_image_callimage_search_callmcp_call 时存在。

对于 web_search_image_callimage_search_call,值分别固定为 "web_search_image""image_search"

对于 mcp_call,值为 MCP 服务中被调用的具体函数名(如 amap-maps-maps_geo)。

arguments string

工具调用的参数,JSON 字符串格式。当 typefunction_callweb_search_image_callimage_search_callmcp_call 时存在。使用前需要通过 JSON.parse() 解析。不同工具类型的 arguments 内容:

  • web_search_image_call{"queries": ["搜索关键词1", "搜索关键词2"]},其中 queries 为模型根据用户输入自动生成的搜索关键词列表。

  • image_search_call{"img_idx": 0, "bbox": [0, 0, 1000, 1000]},其中 img_idx 为输入图片的索引(从 0 开始),bbox 为搜索区域的边界框坐标 [x1, y1, x2, y2],坐标范围 0-1000。

  • function_call:按用户定义的函数参数 schema 生成的参数对象。

  • mcp_call:MCP 服务中被调用函数的参数对象。

call_id string

函数调用的唯一标识符。仅当 typefunction_call 时存在。在返回函数调用结果时,需要通过此 ID 关联请求与响应。

content array

消息内容数组。仅当 typemessage 时存在。

数组元素属性

type string

内容类型,固定为 output_text

text string

模型生成的文本内容。

annotations array

文本注释数组。通常为空数组。

summary array

推理摘要数组。仅当 typereasoning 时存在。每个元素包含 type(值为 summary_text)和 text(摘要文本)字段。

action object

搜索动作信息。仅当 typeweb_search_call 时存在。

属性

query string

搜索查询关键词。

type string

搜索类型,固定为 search

sources array

搜索来源列表。每个元素包含 typeurl字段。

code string

模型生成并执行的代码。仅当 typecode_interpreter_call 时存在。

outputs array

代码执行输出数组。仅当 typecode_interpreter_call 时存在。每个元素包含 type(值为 logs)和 logs(代码执行日志)字段。

container_id string

代码解释器容器标识符。仅当 typecode_interpreter_call 时存在。用于关联同一会话中的多次代码执行。

goal string

抽取目标描述,说明需要从网页中提取哪些信息。仅当 typeweb_extractor_call 时存在。

output string

工具调用的输出结果,字符串格式。

  • typeweb_extractor_call 时为网页抽取的内容摘要

  • typeweb_search_image_callimage_search_call 时为 JSON 字符串,包含图片搜索结果数组,每个元素包含 title(图片标题)、url(图片 URL)和 index(序号)字段

  • typemcp_call 时为 MCP 服务返回的 JSON 字符串结果。

urls array

被抽取的网页 URL 列表。仅当 typeweb_extractor_call 时存在。

server_label string

MCP 服务标签。仅当 typemcp_call 时存在。标识本次调用所使用的 MCP 服务。

queries array

知识库检索使用的查询列表。仅当 typefile_search_call 时存在。数组元素为字符串,表示模型生成的搜索查询词。

results array

知识库检索结果数组。仅当 typefile_search_call 时存在。

数组元素属性

file_id string

匹配文档的文件 ID。

filename string

匹配文档的文件名。

score float

匹配相关度评分,取值范围 0-1,值越大表示相关度越高。

text string

匹配到的文档内容片段。

usage object

本次请求的 Token 消耗信息。

属性

input_tokens integer

输入的 Token 数。

output_tokens integer

模型输出的 Token 数。

total_tokens integer

消耗的总 Token 数,为 input_tokens 与 output_tokens 的总和。

input_tokens_details object

输入 Token 的细粒度分类。

属性

cached_tokens integer

命中缓存的 Token 数。详情请参见上下文缓存

output_tokens_details object

输出 Token 的细粒度分类。

属性

reasoning_tokens integer

思考过程 Token 数。

x_details object

属性

input_tokens integer

输入的 Token 数。

output_tokens integer

模型输出的 Token 数。

total_tokens integer

消耗的总 Token 数,为 input_tokens 与 output_tokens 的总和。

x_billing_type string

固定为response_api

x_tools object

工具使用统计信息。当使用内置工具时,包含各工具的调用次数。

示例:{"web_search": {"count": 1}}

error object

当模型生成响应失败时返回的错误对象。成功时为 null

tools array

回显请求中 tools 参数的完整内容,结构与请求体中的 tools 参数相同。

tool_choice string

回显请求中 tool_choice 参数的值,枚举值为 autononerequired

Response 响应 chunk 对象(流式输出)

基础调用

// response.created - 响应创建
{"response":{"id":"428c90e9-9cd6-90a6-9726-c02b08ebexxx","created_at":1769082930,"object":"response","status":"queued",...},"sequence_number":0,"type":"response.created"}

// response.in_progress - 响应进行中
{"response":{"id":"428c90e9-9cd6-90a6-9726-c02b08ebexxx","status":"in_progress",...},"sequence_number":1,"type":"response.in_progress"}

// response.output_item.added - 新增输出项
{"item":{"id":"msg_bcb45d66-fc34-46a2-bb56-714a51e8exxx","content":[],"role":"assistant","status":"in_progress","type":"message"},"output_index":0,"sequence_number":2,"type":"response.output_item.added"}

// response.content_part.added - 新增内容块
{"content_index":0,"item_id":"msg_bcb45d66-fc34-46a2-bb56-714a51e8exxx","output_index":0,"part":{"annotations":[],"text":"","type":"output_text","logprobs":null},"sequence_number":3,"type":"response.content_part.added"}

// response.output_text.delta - 增量文本(多次触发)
{"content_index":0,"delta":"人工智能","item_id":"msg_bcb45d66-fc34-46a2-bb56-714a51e8exxx","logprobs":[],"output_index":0,"sequence_number":4,"type":"response.output_text.delta"}
{"content_index":0,"delta":"(Artificial Intelligence,","item_id":"msg_bcb45d66-fc34-46a2-bb56-714a51e8exxx","logprobs":[],"output_index":0,"sequence_number":6,"type":"response.output_text.delta"}

// response.output_text.done - 文本完成
{"content_index":0,"item_id":"msg_bcb45d66-fc34-46a2-bb56-714a51e8exxx","logprobs":[],"output_index":0,"sequence_number":53,"text":"人工智能(Artificial Intelligence,简称 AI)是指由计算机系统模拟人类智能行为的技术和科学...","type":"response.output_text.done"}

// response.content_part.done - 内容块完成
{"content_index":0,"item_id":"msg_bcb45d66-fc34-46a2-bb56-714a51e8exxx","output_index":0,"part":{"annotations":[],"text":"...完整文本...","type":"output_text","logprobs":null},"sequence_number":54,"type":"response.content_part.done"}

// response.output_item.done - 输出项完成
{"item":{"id":"msg_bcb45d66-fc34-46a2-bb56-714a51e8exxx","content":[{"annotations":[],"text":"...完整文本...","type":"output_text","logprobs":null}],"role":"assistant","status":"completed","type":"message"},"output_index":0,"sequence_number":55,"type":"response.output_item.done"}

// response.completed - 响应完成(包含完整响应和 usage)
{"response":{"id":"428c90e9-9cd6-90a6-9726-c02b08ebexxx","created_at":1769082930,"model":"qwen3-max-2026-01-23","object":"response","output":[...],"status":"completed","usage":{"input_tokens":37,"output_tokens":243,"total_tokens":280,...}},"sequence_number":56,"type":"response.completed"}

网页抓取

id:1
event:response.created
:HTTP_STATUS/200
data:{"sequence_number":0,"type":"response.created","response":{"output":[],"parallel_tool_calls":false,"created_at":1769435906,"tool_choice":"auto","model":"","id":"863df8d9-cb29-4239-a54f-3e15a2427xxx","tools":[],"object":"response","status":"queued"}}

id:2
event:response.in_progress
:HTTP_STATUS/200
data:{"sequence_number":1,"type":"response.in_progress","response":{"output":[],"parallel_tool_calls":false,"created_at":1769435906,"tool_choice":"auto","model":"","id":"863df8d9-cb29-4239-a54f-3e15a2427xxx","tools":[],"object":"response","status":"in_progress"}}

id:3
event:response.output_item.added
:HTTP_STATUS/200
data:{"sequence_number":2,"item":{"summary":[],"type":"reasoning","id":"msg_5bd0c6df-19b8-4a04-bc00-8042a224exxx"},"output_index":0,"type":"response.output_item.added"}

id:4
event:response.reasoning_summary_text.delta
:HTTP_STATUS/200
data:{"delta":"用户想要我:\n1. 搜索阿里云官网\n2. 提取官网首页的关键信息\n\n我需要先搜索阿里云官网的URL,然后使用web_extractor工具访问官网并提取关键信息。","sequence_number":3,"output_index":0,"type":"response.reasoning_summary_text.delta","item_id":"msg_5bd0c6df-19b8-4a04-bc00-8042a224exxx","summary_index":0}

id:14
event:response.reasoning_summary_text.done
:HTTP_STATUS/200
data:{"sequence_number":13,"text":"用户想要我:\n1. 搜索阿里云官网\n2. 提取官网首页的关键信息\n\n我需要先搜索阿里云官网的URL,然后使用web_extractor工具访问官网并提取关键信息。","output_index":0,"type":"response.reasoning_summary_text.done","item_id":"msg_5bd0c6df-19b8-4a04-bc00-8042a224exxx","summary_index":0}

id:15
event:response.output_item.done
:HTTP_STATUS/200
data:{"sequence_number":14,"item":{"summary":[{"type":"summary_text","text":"用户想要我:\n1. 搜索阿里云官网\n2. 提取官网首页的关键信息\n\n我需要先搜索阿里云官网的URL,然后使用web_extractor工具访问官网并提取关键信息。"}],"type":"reasoning","id":"msg_5bd0c6df-19b8-4a04-bc00-8042a224exxx"},"output_index":1,"type":"response.output_item.done"}

id:16
event:response.output_item.added
:HTTP_STATUS/200
data:{"sequence_number":15,"item":{"action":{"type":"search","query":"Web search"},"id":"msg_a8a686b1-0a57-40e1-bb55-049a89cd4xxx","type":"web_search_call","status":"in_progress"},"output_index":1,"type":"response.output_item.added"}

id:17
event:response.web_search_call.in_progress
:HTTP_STATUS/200
data:{"sequence_number":16,"output_index":1,"type":"response.web_search_call.in_progress","item_id":"msg_a8a686b1-0a57-40e1-bb55-049a89cd4xxx"}

id:19
event:response.web_search_call.completed
:HTTP_STATUS/200
data:{"sequence_number":18,"output_index":1,"type":"response.web_search_call.completed","item_id":"msg_a8a686b1-0a57-40e1-bb55-049a89cd4xxx"}

id:20
event:response.output_item.done
:HTTP_STATUS/200
data:{"sequence_number":19,"item":{"action":{"sources":[{"type":"url","url":"https://cn.aliyun.com/"},{"type":"url","url":"https://www.aliyun.com/"}],"type":"search","query":"Web search"},"id":"msg_a8a686b1-0a57-40e1-bb55-049a89cd4xxx","type":"web_search_call","status":"completed"},"output_index":1,"type":"response.output_item.done"}

id:33
event:response.output_item.added
:HTTP_STATUS/200
data:{"sequence_number":32,"item":{"urls":["https://cn.aliyun.com/"],"goal":"提取阿里云官网首页的关键信息,包括:公司定位/简介、核心产品与服务、主要业务板块、特色功能/解决方案、最新动态/活动、免费试用/优惠信息、导航菜单结构等","id":"msg_8c2cf651-48a5-460c-aa7a-bea5b09b4xxx","type":"web_extractor_call","status":"in_progress"},"output_index":3,"type":"response.output_item.added"}

id:34
event:response.output_item.done
:HTTP_STATUS/200
data:{"sequence_number":33,"item":{"output":"The useful information in https://cn.aliyun.com/ for user goal 提取阿里云官网首页的关键信息,包括:公司定位/简介、核心产品与服务、主要业务板块、特色功能/解决方案、最新动态/活动、免费试用/优惠信息、导航菜单结构等 as follows: \n\nEvidence in page: \n## 通义大模型,企业拥抱 AI 时代首选\n\n## 完整的产品体系,为企业打造技术创新的云\n\n全部云产品## 依托大模型与云计算协同发展,让 AI 触手可及\n\n全部 AI 解决方案\n\nSummary: \nAlibaba Cloud positions itself as a leading enterprise AI solution provider centered around the Tongyi large model...","urls":["https://cn.aliyun.com/"],"goal":"提取阿里云官网首页的关键信息,包括:公司定位/简介、核心产品与服务、主要业务板块、特色功能/解决方案、最新动态/活动、免费试用/优惠信息、导航菜单结构等","id":"msg_8c2cf651-48a5-460c-aa7a-bea5b09b4xxx","type":"web_extractor_call","status":"completed"},"output_index":3,"type":"response.output_item.done"}

id:50
event:response.output_item.added
:HTTP_STATUS/200
data:{"sequence_number":50,"item":{"content":[{"type":"text","text":""}],"type":"message","id":"msg_final","role":"assistant"},"output_index":5,"type":"response.output_item.added"}

id:51
event:response.output_text.delta
:HTTP_STATUS/200
data:{"delta":"我已经找到阿里云官网并提取了首页的关键信息:\n\n","sequence_number":51,"output_index":5,"type":"response.output_text.delta"}

id:60
event:response.completed
:HTTP_STATUS/200
data:{"type":"response.completed","response":{"id":"863df8d9-cb29-4239-a54f-3e15a2427xxx","status":"completed","usage":{"input_tokens":45,"output_tokens":320,"total_tokens":365}}}

文搜图

// 1. response.created - 响应创建
id:1
event:response.created
data:{"sequence_number":0,"type":"response.created","response":{"output":[],"status":"queued",...}}

// 2. response.in_progress - 响应进行中
id:2
event:response.in_progress
data:{"sequence_number":1,"type":"response.in_progress","response":{"status":"in_progress",...}}

// 3. response.output_item.added - 推理开始(reasoning)
id:3
event:response.output_item.added
data:{"sequence_number":2,"item":{"summary":[],"type":"reasoning","id":"msg_xxx"},"output_index":0,"type":"response.output_item.added"}

// 4. response.reasoning_summary_text.delta - 推理摘要增量
id:4
event:response.reasoning_summary_text.delta
data:{"delta":"用户想要找一张猫咪图片。我需要使用web_search_image工具来搜索...","sequence_number":3,"output_index":0,"type":"response.reasoning_summary_text.delta","item_id":"msg_xxx","summary_index":0}

// 5. response.reasoning_summary_text.done - 推理摘要完成
id:10
event:response.reasoning_summary_text.done
data:{"sequence_number":9,"text":"用户想要找一张猫咪图片。我需要使用web_search_image工具来搜索猫咪图片。","output_index":0,"type":"response.reasoning_summary_text.done","item_id":"msg_xxx","summary_index":0}

// 6. response.output_item.done - 推理项完成
id:11
event:response.output_item.done
data:{"sequence_number":10,"item":{"summary":[{"type":"summary_text","text":"..."}],"type":"reasoning","id":"msg_xxx"},"output_index":0,"type":"response.output_item.done"}

// 7. response.output_item.added - 文搜图工具调用开始(status: in_progress,此时已包含 name 和 arguments)
id:12
event:response.output_item.added
data:{"sequence_number":11,"item":{"name":"web_search_image","arguments":"{\"queries\": [\"猫咪图片\", \"cute cat\"]}","id":"msg_xxx","type":"web_search_image_call","status":"in_progress"},"output_index":1,"type":"response.output_item.added"}

// 8. response.output_item.done - 文搜图工具调用完成(包含完整的 output 搜索结果)
id:13
event:response.output_item.done
data:{"sequence_number":12,"item":{"name":"web_search_image","output":"[{\"title\": \"可爱的小猫咪...\", \"url\": \"https://example.com/cat.jpg\", \"index\": 1}, ...]","arguments":"{\"queries\": [\"猫咪图片\", \"cute cat\"]}","id":"msg_xxx","type":"web_search_image_call","status":"completed"},"output_index":1,"type":"response.output_item.done"}

// 9-12. 第二轮推理 + 最终消息输出(与基础调用相同)
// response.output_item.added (reasoning) → reasoning_summary_text.delta/done → response.output_item.done (reasoning)
// response.output_item.added (message) → response.content_part.added → response.output_text.delta → response.output_text.done → response.content_part.done → response.output_item.done (message)

// 13. response.completed - 响应完成
id:118
event:response.completed
data:{"sequence_number":117,"type":"response.completed","response":{"output":[...],"status":"completed","usage":{"input_tokens":7895,"output_tokens":318,"total_tokens":8213,"x_tools":{"web_search_image":{"count":1}}}}}

图搜图

// 1-6. 推理阶段(与文搜图相同)

// 7. response.output_item.added - 图搜图工具调用开始
// 注意 arguments 中包含 img_idx(图片索引)和 bbox(搜索区域边界框)
id:29
event:response.output_item.added
data:{"sequence_number":29,"item":{"name":"image_search","arguments":"{\"img_idx\": 0, \"bbox\": [0, 0, 1000, 1000]}","id":"msg_xxx","type":"image_search_call","status":"in_progress"},"output_index":1,"type":"response.output_item.added"}

// 8. response.output_item.done - 图搜图工具调用完成
id:30
event:response.output_item.done
data:{"sequence_number":30,"item":{"name":"image_search","output":"[{\"title\": \"水墨山背景...\", \"url\": \"https://example.com/landscape.jpg\", \"index\": 1}, ...]","arguments":"{\"img_idx\": 0, \"bbox\": [0, 0, 1000, 1000]}","id":"msg_xxx","type":"image_search_call","status":"completed"},"output_index":1,"type":"response.output_item.done"}

// 9-12. 第二轮推理 + 最终消息输出(与基础调用相同)

// 13. response.completed
id:408
event:response.completed
data:{"sequence_number":407,"type":"response.completed","response":{"output":[...],"status":"completed","usage":{"input_tokens":8371,"output_tokens":417,"total_tokens":8788,"x_tools":{"image_search":{"count":1}}}}}

MCP

// 1-6. 推理阶段(与其他工具相同)

// 7. response.mcp_call_arguments.delta - MCP 参数增量(MCP 独有事件)
id:27
event:response.mcp_call_arguments.delta
data:{"delta":"{\"city\": \"北京\"}","sequence_number":26,"output_index":1,"type":"response.mcp_call_arguments.delta","item_id":"msg_xxx"}

// 8. response.mcp_call_arguments.done - MCP 参数完成(MCP 独有事件)
id:28
event:response.mcp_call_arguments.done
data:{"sequence_number":27,"arguments":"{\"city\": \"北京\"}","output_index":1,"type":"response.mcp_call_arguments.done","item_id":"msg_xxx"}

// 9. response.output_item.added - MCP 工具调用开始(包含 name、server_label、arguments)
id:29
event:response.output_item.added
data:{"sequence_number":28,"item":{"name":"amap-maps-maps_weather","server_label":"MCP Server","arguments":"{\"city\": \"北京\"}","id":"msg_xxx","type":"mcp_call","status":"in_progress"},"output_index":1,"type":"response.output_item.added"}

// 10. response.mcp_call.completed - MCP 调用完成(MCP 独有事件)
id:30
event:response.mcp_call.completed
data:{"sequence_number":29,"output_index":1,"type":"response.mcp_call.completed","item_id":"msg_xxx"}

// 11. response.output_item.done - MCP 输出项完成(包含完整 output)
id:31
event:response.output_item.done
data:{"sequence_number":30,"item":{"output":"{\"city\":\"北京市\",\"forecasts\":[...]}","name":"amap-maps-maps_weather","server_label":"MCP Server","arguments":"{\"city\": \"北京\"}","id":"msg_xxx","type":"mcp_call","status":"completed"},"output_index":1,"type":"response.output_item.done"}

// 12-15. 第二轮推理 + 最终消息输出

// 16. response.completed
id:172
event:response.completed
data:{"sequence_number":171,"type":"response.completed","response":{"output":[...],"status":"completed","usage":{"input_tokens":5019,"output_tokens":539,"total_tokens":5558}}}

知识库搜索

// 1-6. 推理阶段(与其他工具相同)

// 7. response.output_item.added - 知识库搜索开始(包含 queries,无 results)
id:19
event:response.output_item.added
data:{"sequence_number":18,"item":{"id":"msg_xxx","type":"file_search_call","queries":["阿里云百炼X1手机","Alibaba Cloud Bailian X1 phone","百炼X1"],"status":"in_progress"},"output_index":1,"type":"response.output_item.added"}

// 8. response.file_search_call.in_progress - 搜索进行中(file_search 独有事件)
id:20
event:response.file_search_call.in_progress
data:{"sequence_number":19,"output_index":1,"type":"response.file_search_call.in_progress","item_id":"msg_xxx"}

// 9. response.file_search_call.searching - 正在搜索(file_search 独有事件)
id:21
event:response.file_search_call.searching
data:{"sequence_number":20,"output_index":1,"type":"response.file_search_call.searching","item_id":"msg_xxx"}

// 10. response.file_search_call.completed - 搜索完成(file_search 独有事件)
id:22
event:response.file_search_call.completed
data:{"sequence_number":21,"output_index":1,"type":"response.file_search_call.completed","item_id":"msg_xxx"}

// 11. response.output_item.done - 输出项完成(包含 queries + results)
id:23
event:response.output_item.done
data:{"sequence_number":22,"item":{"id":"msg_xxx","type":"file_search_call","queries":["阿里云百炼X1手机","Alibaba Cloud Bailian X1 phone","百炼X1"],"results":[{"score":0.7519,"filename":"阿里云百炼系列手机产品介绍","text":"阿里云百炼 X1 ——畅享极致视界...","file_id":"file_xxx"}],"status":"completed"},"output_index":1,"type":"response.output_item.done"}

// 12-15. 第二轮推理 + 最终消息输出

// 16. response.completed
id:146
event:response.completed
data:{"sequence_number":145,"type":"response.completed","response":{"output":[...],"status":"completed","usage":{"input_tokens":1576,"output_tokens":722,"total_tokens":2298,"x_tools":{"file_search":{"count":1}}}}}

流式输出返回一系列 JSON 对象。每个对象包含 type 字段标识事件类型,sequence_number 字段标识事件顺序。response.completed 事件标志着流式传输的结束。

type string

事件类型标识符。枚举值:

  • response.created:响应创建时触发,状态为 queued

  • response.in_progress:响应开始处理时触发,状态变为 in_progress

  • response.output_item.added:新的输出项(如 message、web_extractor_call)被添加到 output 数组时触发。当 item.typeweb_extractor_call 时,表示网页抽取工具调用开始。

  • response.content_part.added:输出项的 content 数组中新增内容块时触发。

  • response.output_text.delta:增量文本生成时触发,多次触发,delta 字段包含新增文本片段。

  • response.output_text.done:文本生成完成时触发,text 字段包含完整文本。

  • response.content_part.done:内容块完成时触发,part 对象包含完整内容块。

  • response.output_item.done:输出项生成完成时触发,item 对象包含完整输出项。当 item.typeweb_extractor_call 时,表示网页抽取工具调用完成。

  • response.reasoning_summary_text.delta:(开启思考模式时)推理摘要增量文本,delta 字段包含新增摘要片段。

  • response.reasoning_summary_text.done:(开启思考模式时)推理摘要完成,text 字段包含完整摘要。

  • response.web_search_call.in_progress / searching / completed:(使用 web_search 工具时)搜索状态变化事件。

  • response.code_interpreter_call.in_progress / interpreting / completed:(使用 code_interpreter 工具时)代码执行状态变化事件。

  • 注意:使用 web_extractor 工具时,没有专门的事件类型标识符。网页抽取工具调用通过通用的 response.output_item.addedresponse.output_item.done 事件传递,通过 item.type 字段(值为 web_extractor_call)来识别。

  • response.mcp_call_arguments.delta / response.mcp_call_arguments.done:(使用 mcp 工具时)MCP 调用参数的增量和完成事件。

  • response.mcp_call.completed:(使用 mcp 工具时)MCP 服务调用完成。

  • response.file_search_call.in_progress / searching / completed:(使用 file_search 工具时)知识库搜索状态变化事件。

  • 注意:使用 web_search_imageimage_search 工具时,没有专门的中间状态事件。工具调用通过 response.output_item.added(调用开始)和 response.output_item.done(调用完成)事件传递。

  • response.completed:响应生成完成时触发,response 对象包含完整响应(含 usage)。此事件标志流式传输结束。

sequence_number integer

事件序列号,从 0 开始递增。用于确保客户端按正确顺序处理事件。

response object

响应对象。出现在 response.createdresponse.in_progressresponse.completed 事件中。在 response.completed 事件中包含完整的响应数据(包括 outputusage),其结构与非流式响应的 Response 对象一致。

item object

输出项对象。出现在 response.output_item.addedresponse.output_item.done 事件中。在 added 事件中为初始骨架(content 为空数组),在 done 事件中为完整对象。

属性

id string

输出项的唯一标识符(如 msg_xxx)。

type string

输出项类型。枚举值:message(消息)、reasoning(推理)、web_search_call(搜索)、web_search_image_call(文搜图)、image_search_call(图搜图)、mcp_call(MCP 调用)、file_search_call(知识库搜索)。

role string

消息角色,固定为 assistant。仅当 type 为 message 时存在。

status string

生成状态。在 added 事件中为 in_progress,在 done 事件中为 completed

content array

消息内容数组。在 added 事件中为空数组 [],在 done 事件中包含完整的内容块对象(结构与 part 对象相同)。

part object

内容块对象。出现在 response.content_part.addedresponse.content_part.done 事件中。

属性

type string

内容块类型,固定为 output_text

text string

文本内容。在 added 事件中为空字符串,在 done 事件中为完整文本。

annotations array

文本注释数组。通常为空数组。

logprobs object | null

Token 的对数概率信息。当前固定返回 null

delta string

增量文本内容。出现在 response.output_text.delta 事件中,包含本次新增的文本片段。客户端应将所有 delta 拼接以获得完整文本。

text string

完整文本内容。出现在 response.output_text.done 事件中,包含该内容块的完整文本,可用于校验 delta 拼接结果。

item_id string

输出项的唯一标识符。用于关联同一输出项的相关事件。

output_index integer

输出项在 output 数组中的索引位置。

content_index integer

内容块在 content 数组中的索引位置。

summary_index integer

摘要数组索引。出现在 response.reasoning_summary_text.deltaresponse.reasoning_summary_text.done 事件中。

常见问题

Q:如何传递多轮对话的上下文?

A:在发起新一轮对话请求时,请将上一轮模型响应成功返回的id作为 previous_response_id 参数传入。

Q:为什么响应示例中的某些字段未在本文说明?

A:如果使用OpenAI的官方SDK,它可能会根据其自身的模型结构输出一些额外的字段(通常为null)。这些字段是OpenAI协议本身定义的,我们的服务当前不支持,所以它们为空值。只需关注本文档中描述的字段即可。