OpenAI Responses API 参考

更新时间:
复制为 MD 格式

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

相较于OpenAI Chat Completions API 的优势:

  • 内置工具:内置联网搜索、代码解释器、网页抓取工具。建议同时开启这些工具(tools=[{"type": "web_search"}, {"type": "code_interpreter"}, {"type": "web_extractor"}]),以在处理复杂任务时获得最佳效果,详情参考调用内置工具

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

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

兼容性说明与限制

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

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

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

  • 不支持多模态输入:当前仅支持 qwen3-max-2026-01-23文本模型,input 当前仅支持文本格式,不支持图像或其他文件类型输入。

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

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

当前仅支持华北2(北京)地域。

华北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

请求体

基础调用

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-max-2026-01-23",
    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-max-2026-01-23",
        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-max-2026-01-23",
    "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-max-2026-01-23",
    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-max-2026-01-23",
        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-max-2026-01-23",
    "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-max-2026-01-23",
    input="我的名字是张三,请记住。"
)
print(f"第一轮回复: {response1.output_text}")

# 第二轮对话 - 使用 previous_response_id 关联上下文,响应id有效期为7天
response2 = client.responses.create(
    model="qwen3-max-2026-01-23",
    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-max-2026-01-23",
        input: "我的名字是张三,请记住。"
    });
    console.log(`第一轮回复: ${response1.output_text}`);

    // 第二轮对话 - 使用 previous_response_id 关联上下文,响应id有效期为7天
    const response2 = await openai.responses.create({
        model: "qwen3-max-2026-01-23",
        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-max-2026-01-23",
    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-max-2026-01-23",
        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-max-2026-01-23",
    "input": "帮我找一下阿里云官网,并提取首页的关键信息",
    "tools": [
        {
            "type": "web_search"
        },
        {
            "type": "code_interpreter"
        },
        {
            "type": "web_extractor"
        }
    ],
    "enable_thinking": true
}'

model string (必选)

模型名称。当前仅支持 qwen3-max-2026-01-23

input string 或 array (必选)

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

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

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

array消息类型

System Message object (可选)

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

属性

role string (必选)

消息角色,固定为 system

content string (必选)

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

User Message object (必选)

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

属性

role string (必选)

消息角色,固定为 user

content string (必选)

用户输入的文本内容。

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-max-2026-01-23的思考模式。

属性

type string (必选)

必须设置为web_extractor

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

code_interpreter

代码解释器工具,允许模型执行代码并返回结果,支持数据分析。相关文档:代码解释器

仅适用于qwen3-max-2026-01-23的思考模式。

属性

type string (必选)

必须设置为code_interpreter

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

自定义工具 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 (可选)默认值为 false

是否开启思考模式。开启后,模型会在回复前进行思考,思考内容将通过 reasoning 类型的输出项返回。

开启思考模式时,建议开启内置工具,以在处理复杂任务时获得最佳的模型效果。

可选值:

  • true:开启思考模式。使用 code_interpreterweb_extractor 工具时必须开启。

  • false:不开启思考模式。

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

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

{
    "created_at": 1769408284,
    "id": "351e34cc-5f75-483b-b948-35be954dbxxx",
    "model": "qwen3-max-2026-01-23",
    "object": "response",
    "output": [
        {
            "content": [
                {
                    "annotations": [],
                    "text": "你好!我是通义千问(Qwen),由通义实验室研发的超大规模语言模型,能够回答问题、创作文字、编程、表达观点等,致力于为你提供准确、有用和友好的帮助。",
                    "type": "output_text"
                }
            ],
            "id": "msg_59a7339e-77d0-4451-8f51-75fb8dbefxxx",
            "role": "assistant",
            "status": "completed",
            "type": "message"
        }
    ],
    "parallel_tool_calls": false,
    "status": "completed",
    "tool_choice": "auto",
    "tools": [],
    "usage": {
        "input_tokens": 39,
        "input_tokens_details": {
            "cached_tokens": 0
        },
        "output_tokens": 46,
        "output_tokens_details": {
            "reasoning_tokens": 0
        },
        "total_tokens": 85
    }
}

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 工具时返回。需要配合 enable_thinking: true 使用。

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

id string

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

role string

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

status string

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

name string

函数名称。仅当 typefunction_call 时存在。

arguments string

函数调用的参数,JSON 字符串格式。仅当 typefunction_call 时存在。使用前需要通过 JSON.parse() 解析。

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

搜索来源列表。每个元素包含 type(值为 url)和 url(网页 URL)字段。

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 时存在。

urls array

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

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 数。Context Cache 详情请参见上下文缓存

output_tokens_details object

输出 Token 的细粒度分类。

属性

reasoning_tokens integer

思考过程 Token 数。

x_tools object

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

示例:

  • web_search 工具:{"web_search": {"count": 1}}

  • code_interpreter 工具:{"code_interpreter": {"count": 1}}

  • web_extractor 工具:{"web_extractor": {"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}}}

流式输出返回一系列 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.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(推理)等。

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协议本身定义的,我们的服务当前不支持,所以它们为空值。只需关注本文档中描述的字段即可。