# OpenAI Responses 接口协议
针对文本生成类模型,说明无状态 OpenAI Responses 接口的上下文传递、流式事件、深度思考、工具调用、结构化输出及响应结构。
根据本次请求携带的完整上下文生成有序输出项,适用于使用 OpenAI Responses 接口进行文本对话、推理回传、函数工具调用和结构化输出的应用。
# 快速开始
输入
请用三句话介绍一下什么是大模型。
调用
export AIYUN_API_KEY="YOUR_API_KEY"
curl https://tokens.xf-yun.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AIYUN_API_KEY" \
-d '{
"model": "deepseek-v41-flash",
"input": "请用三句话介绍一下什么是大模型。"
}'
import os
import requests
resp = requests.post(
"https://tokens.xf-yun.com/v1/responses",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer " + os.environ["AIYUN_API_KEY"],
},
json={
"model": "deepseek-v41-flash",
"input": "请用三句话介绍一下什么是大模型。",
},
timeout=60,
)
resp.raise_for_status()
body = resp.json()
texts = []
for item in body["output"]:
if item.get("type") != "message":
continue
for part in item.get("content", []):
if part.get("type") == "output_text":
texts.append(part["text"])
print("".join(texts))
返回示例
{
"id": "dx_1793141215000000000",
"object": "response",
"created_at": 1793141215,
"model": "deepseek-v41-flash",
"status": "completed",
"store": false,
"output": [
{
"type": "message",
"id": "message_1",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "大模型是基于大规模语料预训练的深度学习模型。它能够理解自然语言指令,完成问答、摘要、代码编写与逻辑推理等任务。通过提示词与工具调用,大模型可以接入不同业务系统。",
"annotations": []
}
]
}
],
"error": null,
"incomplete_details": null,
"usage": {
"input_tokens": 28,
"output_tokens": 96,
"total_tokens": 124
}
}
判断生成结果请读取顶层 status 和完整的 output。读取正文时遍历全部 type 为 message 的项并提取 output_text,不要只取 output[0]。id 仅标识当次响应,不能用于查询历史。
# 接口说明
| 项目 | 说明 |
|---|---|
| 请求地址 | POST https://tokens.xf-yun.com/v1/responses |
| 鉴权 | 请求头 Authorization: Bearer $AIYUN_API_KEY,见 接口鉴权 |
| 请求格式 | UTF-8 JSON 对象,Content-Type: application/json |
| 必填字段 | model、input |
| 非流式响应 | HTTP 200,application/json |
| 流式响应 | HTTP 200,text/event-stream |
| 请求 ID | 错误对象中的 request_id 与响应头请求标识相同,用于日志关联 |
本接口无会话状态,每次请求均需在 input 中传入完整上下文,store 仅允许省略或设为 false。model 须与平台能力 ID 一致,不存在时返回 404 / model_not_found。
# 不支持的主要功能与参数
以下功能与参数在平台 Responses 接口中整体不支持:
| 功能 / 参数 | 说明 |
|---|---|
图片、视频、音频与文件输入(input_image、input_video、input_audio、input_file) | 不支持。输入内容仅支持文本,传入非文本类型返回 400 / 20003。 |
服务端会话与历史续聊(store: true、previous_response_id、conversation、background: true) | 不支持。平台不保存历史响应,传入 store: true、非空 previous_response_id 或 background: true 返回 400 / 20003 |
采样参数(temperature、top_p) | 不支持。传入返回 400 / 20003。 |
内置托管工具(web_search、code_interpreter、file_search、mcp 等) | 不支持。tools 仅支持 type: "function" 的客户端函数工具,传入其他类型返回 400 / 20003 |
按名称指定函数(tool_choice 指定函数对象) | 不支持。tool_choice 仅支持 auto、none、required 及对应 {"type":"..."} 对象形式 |
流式结构化输出(stream: true 与 json_object / json_schema 组合) | 不支持。同时传入返回 400 / 20003。需要结构化输出请使用非流式请求 |
# 多轮对话
调用方在本地保存有序历史,下一次请求通过 input 完整回传。上一轮的模型、instructions、tools 和 text 配置不会自动继承,每次请求均需重新传入。
input 支持消息简写与完整消息项。角色可为 system、developer、user、assistant(developer 独立保留)。完整结构中,system、developer、user 的文本内容使用 input_text,assistant 的历史输出使用 output_text。顶层 instructions 非空时作为位于最前的系统指令生效,不与 input 中已有的 system 或 developer 消息合并。
{
"model": "deepseek-v41-flash",
"instructions": "请用中文简洁作答。",
"input": [
{"role": "user", "content": "我的订单号是 A123"},
{
"type": "message",
"id": "message_1",
"role": "assistant",
"status": "completed",
"content": [{"type": "output_text", "text": "已记录。", "annotations": []}]
},
{"role": "user", "content": "我的订单号是什么?"}
]
}
多轮上下文处理规则:
- 仅当响应
status为completed时,才将本次output追加到历史;incomplete或failed的部分输出不写入正常历史。 status为completed且output含有function_call时,执行函数并追加function_call_output后,用完整input发起下一轮请求。- 回传历史项时,消息角色、顺序、文本、
reasoning_text以及工具调用配对关系均会保留;输出项上的id、status、annotations等字段保留在请求中不会报错。 - 裁剪历史时,同一轮的
function_call、function_call_output与对应reasoning项须成对保留或一并移除。超出模型上下文上限时返回 400 /invalid_parameter。
# 流式输出
stream 为 true 时返回 text/event-stream。每个事件包含 JSON 格式的 data: 行(带 type 与严格递增的 sequence_number)和可选的同名 event: 行,以空行结束。
正常结束时不发送 data: [DONE],而是以终态事件(response.completed、response.incomplete 或 response.failed)收尾,客户端不要等待 [DONE]。
| 事件 | 说明 |
|---|---|
response.created、response.in_progress | 建立或更新响应快照 |
response.output_item.added、response.output_item.done | 按 output_index 建立或完成一个输出项 |
response.content_part.added、response.content_part.done | 按 item_id 与 content_index 建立或完成一个内容块 |
response.output_text.delta、response.output_text.done | delta 为正文增量,text 为该块完整文本 |
response.reasoning_text.delta、response.reasoning_text.done | 思考正文的增量与完整文本 |
response.function_call_arguments.delta、response.function_call_arguments.done | 按 item_id 拼接参数字符串,arguments 为完整参数 |
response.refusal.delta、response.refusal.done | 拒绝说明的增量与完整文本 |
response.completed、response.incomplete、response.failed | 最终响应快照,status 与事件名一致 |
error | 流内错误,包含 code、message、param |
文本按 item_id 与 content_index 聚合,函数参数按 item_id 聚合,output_index 决定最终 output 的顺序。收到 response.completed 后,再将完整 output 写入历史。
import json
import os
import requests
with requests.post(
"https://tokens.xf-yun.com/v1/responses",
headers={"Authorization": "Bearer " + os.environ["AIYUN_API_KEY"]},
json={
"model": "deepseek-v41-flash",
"input": "写一首关于秋天的短诗。",
"stream": True,
},
stream=True,
timeout=120,
) as resp:
resp.raise_for_status()
for line in resp.iter_lines(decode_unicode=True):
if not line or not line.startswith("data: "):
continue
event = json.loads(line[len("data: "):])
etype = event.get("type")
if etype == "response.output_text.delta":
print(event.get("delta", ""), end="", flush=True)
elif etype == "response.completed":
print("\nusage:", event["response"].get("usage"))
break
elif etype in ("response.failed", "response.incomplete", "error"):
raise RuntimeError(f"{etype}: {event}")
# 控制输出长度
max_output_tokens 为正整数,限制单次生成的总输出预算,同时包含思考内容和可见输出。生成达到该上限时,status 为 incomplete,incomplete_details.reason 为 max_output_tokens。
# 深度思考
reasoning.effort 为字符串,用于指定思考强度:
- 省略
reasoning、传入{}或effort为空字符串时,不指定固定强度,按模型的默认行为处理。 - 传入目标模型支持的档位(如
none、low、max等)时按该档位处理,各模型支持的档位见 DeepSeek 调用指南 和 Qwen 调用指南。模型不接受所传档位时返回 400 /invalid_parameter。 - 显式传入
null返回 400。
{
"model": "deepseek-v41-flash",
"input": "一个笼子里有鸡和兔共 35 只,脚共 94 只,各有多少只?",
"reasoning": {"effort": "low"}
}
响应和历史中的推理项结构如下,多轮回传时保留 content 中的 reasoning_text 即可:
{
"type": "reasoning",
"id": "reasoning_1",
"content": [{"type": "reasoning_text", "text": "设鸡有 x 只,兔有 y 只……"}]
}
# 工具调用
在 tools 中声明客户端函数工具。type(固定为 function)、name、parameters 必填,description 和 strict 可选。parameters 为对象类型的参数结构定义;结构定义非法或包含外部引用时返回 400。strict 为 true 时,平台在返回 completed 前校验参数是否满足该结构定义,失败返回 502 / output_validation_failed。
| tool_choice | 行为 |
|---|---|
| 省略 | 按模型默认工具行为处理 |
auto 或 {"type":"auto"} | 由模型决定是否调用 |
none 或 {"type":"none"} | 不调用工具 |
required 或 {"type":"required"} | 必须调用工具,此时 tools 不能为空 |
parallel_tool_calls 为 false 时,单次完整响应最多包含一个函数调用,超出时返回 502 / output_validation_failed;为 true 时允许多个。
模型输出的函数调用项为 function_call(id 标识输出项,call_id 关联调用与结果,arguments 为 JSON 对象字符串)。调用方仅在响应 status 为 completed 时执行函数,并在下一轮 input 中回传 function_call_output(output 为字符串,结构化数据请先序列化为 JSON 字符串):
{
"model": "deepseek-v41-flash",
"tools": [
{
"type": "function",
"name": "lookup_order",
"description": "查询订单状态",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"],
"additionalProperties": false
},
"strict": true
}
],
"input": [
{"role": "user", "content": "查询订单 A123"},
{
"type": "function_call",
"id": "item_1",
"call_id": "call_1",
"name": "lookup_order",
"arguments": "{\"order_id\":\"A123\"}",
"status": "completed"
},
{
"type": "function_call_output",
"call_id": "call_1",
"output": "{\"status\":\"shipped\"}"
}
]
}
input 中的工具调用配对规则如下,不满足时返回 400 / tool_call_mismatch:
- 每个
function_call的call_id在本次历史中唯一。 - 每个
function_call_output对应此前已出现且尚未配对的调用。 - 不允许出现孤立结果、未知
call_id、重复结果,以及结果出现在调用之前。 - 同一批并行调用先全部列出,随后列出全部结果;结果之间的顺序不限。
- 存在未配对调用时,中间不能插入新的
user消息。
# 结构化输出
text.format 取值如下,仅约束助手答案文本,不作用于推理项或函数调用项:
text.format.type | 说明 |
|---|---|
text | 普通文本,默认配置 |
json_object | 输出可解析为 JSON 对象的字符串 |
json_schema | 必填 name 和 schema,strict 默认 false;strict 为 true 时,答案须满足该结构定义,否则返回 502 / output_validation_failed |
stream: true 不能与 json_object 或 json_schema 同时使用,组合传入时返回 400 / 20003。需要 JSON 输出时请使用非流式请求。
{
"model": "deepseek-v41-flash",
"input": "提取「订单 A123 已发货」中的状态",
"text": {
"format": {
"type": "json_schema",
"name": "order_status",
"strict": true,
"schema": {
"type": "object",
"properties": {"status": {"type": "string"}},
"required": ["status"],
"additionalProperties": false
}
}
},
"max_output_tokens": 256
}
# 主要请求参数一览
| 参数 | 类型 | 必填与默认 | 说明 |
|---|---|---|---|
model | string | 必填 | 能力 ID,区分大小写 |
input | string | array | 必填 | 本次所需的全部文本输入与历史;字符串等价于单条 user 消息 |
instructions | string | 可选 | 顶层系统指令,位于所有消息之前 |
store | boolean | 默认 false | 只能省略或为 false |
stream | boolean | 默认 false | true 返回 SSE;不可与 JSON 输出格式组合 |
max_output_tokens | integer | 可选 | 正整数,包含思考和可见输出 |
reasoning | object | 可选 | reasoning.effort 指定思考强度 |
tools、tool_choice、parallel_tool_calls | array / string / boolean | 可选 | 见 工具调用 |
text | object | 默认普通文本 | 通过 text.format 指定输出格式 |
# 返回结果
响应顶层字段:
| 字段 | 说明 |
|---|---|
id | 本次响应标识,不能恢复历史 |
object | 固定为 response |
created_at | Unix 时间戳(秒) |
model | 本次请求的 model |
status | completed、incomplete 或 failed;流式中间快照可为 in_progress |
store | 固定为 false |
output | 有序输出项数组,可包含 reasoning、function_call、message |
error | 正常为 null;failed 时包含 code 和 message |
incomplete_details | 正常为 null;incomplete 时包含 reason(如 max_output_tokens) |
usage | 包含 input_tokens、output_tokens、total_tokens;output_tokens_details.reasoning_tokens 已包含在 output_tokens 内 |