Responses API

Responses 端点的请求参数、输入校验、对象结构、生命周期、流式事件与错误码参考。

查看 Markdown

接口定位

POST /v1/responses 接收 Responses 请求形状:网关把 input 转换成 Chat Completions 消息转发给文本模型,再把助手回复包装成 Responses 响应对象。它适合已按 Responses 形态编写的文本客户端;本端点是格式桥接,不是完整的原生 Responses 服务。

普通 function 工具往返此前已有支持;本批补充单层 namespace、custom 原始字符串与 additional_tools。已实测为 Codex CLI 0.171.0-alpha.13、glm-5.3、流式 custom 工具往返及最终回复,实测发生于隔离候选,不代表该候选已在公网发布。非流式路径和其它模型不能据此视为已实测;本页不承诺完整原生 Responses 协议、托管工具执行或任意 grammar。

端点一览

操作方法与路径返回
创建响应POST /v1/responses同步返回最终 Responses 对象
查询响应GET /v1/responses/{id}已保存的响应对象
删除响应DELETE /v1/responses/{id}删除前的对象;此后再查为 404
取消响应POST /v1/responses/{id}/cancel终态返回 400,非终态标记 cancelled
回读输入GET /v1/responses/{id}/input_items规范化输入列表
项目值
OpenAI SDK base_urlhttps://api.gfw.net/v1
鉴权Authorization 头,值为 Bearer 加空格及自己的 API Key;也接受 x-api-key 头
请求格式JSON

/v1/responses 没有列表端点,也没有专门的 405 响应(对比:GET /v1/chat/completions 返回 405 method_not_allowed)。未挂载的方法与路径按未匹配路由处理,在 API 域名下返回 HTTP 404 api_not_found,例如 GET /v1/responses 或 PUT /v1/responses/{id}。

输入形态与校验

input 支持两种形态,转换前先整体校验;任何一项不合法都返回 HTTP 400,不会跳过非法项后仅发送 instructions。

  • 字符串:转成单条 user 消息。
  • 数组:消息项可以是显式 type: "message" 的消息,或是不带 type 的 {role, content} 简写;保留顺序与角色。role 接受 user、assistant、system、developer,缺省按 user 处理;content 为字符串或内容块数组(input_text / text 文本块、input_image / image_url 图片块)。顶层 input_text(带 text)与 input_image(带字符串 image_url)简写也接受,各自转成一条 user 消息。

数组还接受以下工具项:

项类型必要字段与处理
function_callcall_id、name、arguments;arguments 为编码成字符串的 JSON 对象,转成 assistant 工具调用
function_call_outputcall_id、output;output 为字符串或受支持的文本块数组,转成工具结果
custom_tool_callcall_id、name、原始字符串 input;有命名空间时另带 namespace
custom_tool_call_output同一 call_id 与 output;必须匹配 custom 调用,不能改成 function 结果项
additional_toolsrole: "developer"、tools;声明本次请求可用的 function、namespace 或 custom,不执行工具
reasoning可回放公开形态的元数据;不解密 encrypted_content,不恢复厂商推理状态

assistant 消息的 output_text 文本块也可回放。工具结果必须与前面的调用逐一匹配:同一轮先列全部调用,再列结果;每个 call_id 只能使用一次,结果齐全后再开始下一条消息或声明。namespace/custom 调用必须在当前请求中有对应声明,类型、名称与命名空间须一致。

instructions 转成消息列表开头的 system 消息,但 instructions 不能代替非空 input;仅有声明或 reasoning 元数据也不能代替有效消息或完整工具往返。

以下输入会被拒绝(HTTP 400,通常为 invalid_input;namespace/custom 声明或历史在工具转换阶段失败时为 invalid_tools):

  • input 缺失、空字符串或空数组;数组项不是对象或 type 不是字符串;
  • 未知项类型(如 input_file);把 Chat 格式的 tool_calls、function_call、tool_call_id 混进消息项;
  • 重复、悬空、缺失结果的 call_id,调用与结果类型不符,或 function arguments 不是 JSON 对象字符串;
  • 角色不在上述四种之内;空消息内容,或内容块缺 text / image_url、类型未知。

图片输入只接受 Base64 Data URI(data:image/png;base64,... 形态);外部图片 URL 在转发前被拒绝(HTTP 400,invalid_image_url)。视觉输入还需所选模型支持视觉。

previous_response_id 不会被读取:既不报错,也不会恢复历史。多轮对话由应用把所需历史显式放入 input。

请求参数

只列出网关实际读取并处理的字段;未列出的字段不参与转换:

字段类型必填当前处理方式
modelstring是平台模型 ID;缺失返回 400 missing_model
inputstring / array是见"输入形态与校验"
instructionsstring否转成消息列表开头的 system 消息
max_output_tokensinteger否转成 max_tokens;gpt-5 / o3 / o4 前缀模型进一步改写为 max_completion_tokens
max_tokensinteger否兼容字段;与 max_output_tokens 同时提供时覆盖前者,建议只用 max_output_tokens
temperature、top_pnumber否透传;可接受范围由模型决定
stop、frequency_penalty、presence_penalty—否透传
reasoning、reasoning_effortobject / string否透传;取值与行为依赖模型及通道
toolsarray否function 定义转换;本批增加单层 namespace 与 custom,见下节
tool_choicestring / object否none / auto / required,或指定 {type, name};namespace 内工具加 namespace,custom 的 type 为 custom
parallel_tool_callsboolean否透传;是否正确并行调用依赖模型,历史结果须完整关联
response_formatobject否透传至 Chat Completions,见结构化输出;不等同于支持原生 text.format
service_tier、metadata、store、seed、logprobs、top_logprobs、logit_bias、n—否原样透传;store / metadata 不构成平台持久化或检索承诺
streamboolean否true 时返回 SSE 事件流

glm-5.3 在桥接时使用模型默认的 thinking 行为,不再强制注入 thinking: {"type": "disabled"};其它 glm- 前缀模型保留既有的强制关闭 thinking 行为。响应仍只包含最终文本,不提供推理文本或推理增量。需要推理输出请使用 Chat Completions,参见推理模型。

工具声明与结果回传

tools 中的 function 使用 Responses 平铺字段(type、name、description、parameters、可选 strict),由平台转成 Chat function 定义;strict 的实际约束能力取决于模型与通道。单层 namespace 使用 name 与 tools 包含 function/custom;不接受嵌套 namespace。

custom 使用 format: {"type":"text"}(省略 format 也按 text)。grammar 仅接受本次实测 Codex 版本发送的精确 JavaScript 源码 Lark grammar;这是针对该请求形态的转换,不是通用 Lark、正则或任意语法约束引擎,修改后的 grammar 也可能被拒绝。web_search、file_search、托管代码执行及其它未知工具均拒绝,不会静默跳过。工具执行由客户端负责。

function 完整历史示意(调用 ID、参数和结果均为结构示例;声明放在当前请求的 tools 中):

{
  "model": "glm-5.3",
  "stream": true,
  "tools": [{"type":"function","name":"lookup","parameters":{"type":"object","properties":{"key":{"type":"string"}},"required":["key"]}}],
  "input": [
    {"role":"user","content":"查询 demo 的值。"},
    {"type":"function_call","call_id":"call_example_function","name":"lookup","arguments":"{\"key\":\"demo\"}"},
    {"type":"function_call_output","call_id":"call_example_function","output":"value"}
  ]
}

namespace + custom + additional_tools 的首次请求结构示意:

{
  "model": "glm-5.3",
  "stream": true,
  "tools": [],
  "input": [
    {"type":"additional_tools","role":"developer","tools":[{"type":"namespace","name":"local","tools":[{"type":"custom","name":"code_exec","description":"由客户端运行代码并返回文本。","format":{"type":"text"}}]}]},
    {"role":"user","content":"使用 code_exec 返回 hello。"}
  ]
}

模型选择该工具时,output 中可出现以下调用项;custom 的 input 是原文,不是 JSON arguments。下列内容为结构示例,不是固定模型输出:

{
  "id": "resp_example_fc_0",
  "type": "custom_tool_call",
  "status": "completed",
  "call_id": "call_example_custom",
  "name": "code_exec",
  "namespace": "local",
  "input": "console.log('hello')"
}

客户端执行后,下一次请求携带所需历史、相同声明、模型返回的原调用和实际结果:

{
  "model": "glm-5.3",
  "stream": true,
  "tools": [],
  "input": [
    {"type":"additional_tools","role":"developer","tools":[{"type":"namespace","name":"local","tools":[{"type":"custom","name":"code_exec","description":"由客户端运行代码并返回文本。","format":{"type":"text"}}]}]},
    {"role":"user","content":"使用 code_exec 返回 hello。"},
    {"type":"custom_tool_call","call_id":"call_example_custom","name":"code_exec","namespace":"local","input":"console.log('hello')"},
    {"type":"custom_tool_call_output","call_id":"call_example_custom","output":"hello"}
  ]
}

实际应用必须使用返回的 call_id,不要照抄示例 ID;关联依据是 call_id,不是输出项 id。additional_tools 按输入顺序应用,同一 namespace/name 的后声明覆盖先声明;不能插在尚未回齐结果的一轮工具中间。平台不读取 previous_response_id 来补齐调用或声明。

完整调用示例

在运行环境中设置 GFW_API_KEY。Python 依赖 openai 包,密钥仅从环境变量读取。以下使用流式文本请求;执行会发起真实模型调用并计费。GLM-5.3 的本批真实 Codex 验证范围仅为流式工具往返。

curl --no-buffer --fail-with-body --silent --show-error \
  --max-time 120 \
  https://api.gfw.net/v1/responses \
  -H "Authorization: Bearer ${GFW_API_KEY:?请先设置 GFW_API_KEY}" \
  -H 'Content-Type: application/json' \
  --data '{
    "model": "glm-5.3",
    "instructions": "用中文回答,最多三句话。",
    "input": [
      {"type": "message", "role": "user", "content": "为什么生产请求需要超时设置?"}
    ],
    "max_output_tokens": 512,
    "stream": true
  }'

示例关闭 SDK 自动重试,以免排查时重复提交。生产应用应按错误码与重试制定有界重试策略。

响应对象结构

非流式创建返回最终对象;流式请求在终态事件内携带最终对象。以下仅为文本响应结构示意,ID、时间与 Token 数值均为示例,不是 GLM-5.3 非流式实测记录:

{
  "id": "resp_req_example0000000000000000",
  "object": "response",
  "created_at": 1800000000,
  "model": "glm-5.3",
  "status": "completed",
  "output": [
    {
      "type": "message",
      "role": "assistant",
      "content": [
        {"type": "output_text", "text": "超时让应用能够明确结束等待。"}
      ]
    }
  ],
  "usage": {
    "input_tokens": 30,
    "output_tokens": 18,
    "total_tokens": 48,
    "input_tokens_details": {"cached_tokens": 0, "audio_tokens": 0},
    "output_tokens_details": {
      "reasoning_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    }
  }
}
字段说明
idresp_ 前缀的平台响应 ID(后接 24 位生成字符),用于生命周期端点
object固定为 response
created_at创建时间戳(Unix 秒);当前对象只有这一个时间字段
status见下节状态表
output[]消息与工具调用项数组;消息文本位于 content[] 的 output_text.text;工具项为 function_call 或 custom_tool_call,可只有工具而没有消息
usageToken 用量明细(含 input_tokens_details / output_tokens_details),不是金额
error工具流失败时位于 response.failed 事件的 data.error;不保证失败流携带最终响应对象

未启用 namespace/custom 转换的既有路径在无法解析上游 Chat Completions 时,仍可能直接返回上游原始 JSON,不生成 Responses 对象或保存生命周期记录。namespace/custom 转换路径会拒绝无法恢复的工具调用、空 choices 或没有文本也没有工具的结果,不能把不可交付输出当作成功工具响应。glm- 前缀模型的输出会先剥离 Markdown 代码围栏再写入 output。

状态与生命周期

状态出现场景含义
in_progress流式 response.created / response.in_progress 事件内的对象生成进行中;已保存对象不会处于该状态
completed非流式正常结束;流式 response.completed生成完成
incomplete非流式上游 finish_reason=length达到输出上限,文本可能被截断
failed流式上游读取失败生成失败,对象携带 error
cancelledcancel 端点标记已取消
  • 生成是同步执行的:POST /v1/responses 等待上游完成后才返回,没有后台任务,不需要轮询等待完成。
  • 完整对象保存约 30 天,供查询、删除与输入回读。对象按 API Key 所属用户归属校验:查询他人或不存在的 ID 都返回 404 response_not_found,不会泄露对象是否存在。
  • DELETE 返回删除前的对象,此后查询为 404。
  • cancel:终态(completed / incomplete / failed / cancelled)返回 HTTP 400 cannot_cancel_completed_response,非终态标记为 cancelled 后返回;由于当前实现只在终态保存对象,实际调用通常返回 400。
  • input_items 返回创建时规范化存储的输入项,字符串 input 也会规范化为 message 形态:
{
  "object": "response.input_item.list",
  "data": [
    {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "为什么生产请求需要超时设置?"}]}
  ]
}

流式事件

stream: true 时响应为 text/event-stream。建立 SSE 之前发生的错误(鉴权、参数校验、余额预检、上游连接失败)返回普通 JSON 与对应 HTTP 状态(如 502 upstream_error),不会开始事件流。

事件按 event: <名称> 与 data: <JSON> 输出,每个 data 都携带 type 与从 0 递增的 sequence_number:

事件data 内主要字段说明
response.createdresponse(status: in_progress)流开始,携带响应骨架
response.in_progressresponse生成进行中
response.output_item.addedoutput_index、item添加消息、function_call 或 custom_tool_call 项
response.content_part.addeditem_id、output_index、content_index、part添加 output_text 内容块
response.output_text.deltaitem_id、output_index、content_index、delta文本增量,在 delta 读取
response.output_text.doneitem_id、output_index、content_index、text该内容块的全部文本
response.function_call_arguments.deltaitem_id、output_index、delta函数 JSON 参数字符串增量
response.function_call_arguments.doneitem_id、output_index、name、可选 namespace、arguments完整函数参数
response.custom_tool_call_input.deltaitem_id、output_index、deltacustom 原始字符串增量;可缓冲后一次发出
response.custom_tool_call_input.doneitem_id、output_index、input完整 custom 原始输入
response.output_item.doneoutput_index、item消息或调用项完成,调用的 call_id/name/namespace 在 item 中
response.completedresponse(含 output 与 usage)成功终态
response.failederror(含 code、message;工具恢复失败时另含 request_id、retryable: false)失败终态;在 data.error 读取错误,不在 data.response.error 读取

要点:

  • 流末尾不发送 data: [DONE];以 response.completed 或 response.failed 作为终止事件,此前连接中断应视为结果不完整。
  • 桥接输出文本与工具事件,不转发推理增量;终止事件内的 usage 由网关聚合,上游未回传用量时按本地计数估算。工具项完成表示调用输入已生成,仍须客户端执行并回传结果。
  • 流式同样保存最终对象,之后可用生命周期端点回读。

curl -N 关闭客户端输出缓冲:

curl --no-buffer --fail-with-body --silent --show-error \
  --max-time 120 \
  https://api.gfw.net/v1/responses \
  -H "Authorization: Bearer ${GFW_API_KEY:?请先设置 GFW_API_KEY}" \
  -H 'Content-Type: application/json' \
  -H 'Accept: text/event-stream' \
  --data '{
    "model": "glm-5.3",
    "input": "用三句话说明连接池的作用。",
    "max_output_tokens": 512,
    "stream": true
  }'

事件序列结构示意(节选核心事件,数值与文本均为示例):

event: response.created
data: {"type":"response.created","sequence_number":0,"response":{"id":"resp_req_example0000000000000000","object":"response","created_at":1800000000,"status":"in_progress","model":"glm-5.3"}}

event: response.output_text.delta
data: {"type":"response.output_text.delta","sequence_number":1,"item_id":"resp_req_example0000000000000000_msg","output_index":0,"content_index":0,"delta":"超时"}

event: response.completed
data: {"type":"response.completed","sequence_number":3,"response":{"id":"resp_req_example0000000000000000","object":"response","created_at":1800000000,"status":"completed","model":"glm-5.3","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"超时让应用能够明确结束等待。"}]}],"usage":{"input_tokens":30,"output_tokens":18,"total_tokens":48,"input_tokens_details":{"cached_tokens":0,"audio_tokens":0},"output_tokens_details":{"reasoning_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}}}}

custom 工具流结构示意(完整列出本例事件,ID、文本与用量均为示例;对应上方 local/code_exec 声明):

event: response.created
data: {"type":"response.created","sequence_number":0,"response":{"id":"resp_example","object":"response","created_at":1800000000,"status":"in_progress","model":"glm-5.3"}}

event: response.in_progress
data: {"type":"response.in_progress","sequence_number":1,"response":{"id":"resp_example","object":"response","created_at":1800000000,"status":"in_progress"}}

event: response.output_item.added
data: {"type":"response.output_item.added","sequence_number":2,"output_index":0,"item":{"id":"resp_example_fc_0","type":"custom_tool_call","status":"in_progress","call_id":"call_example_custom","name":"code_exec","namespace":"local","input":""}}

event: response.custom_tool_call_input.delta
data: {"type":"response.custom_tool_call_input.delta","sequence_number":3,"item_id":"resp_example_fc_0","output_index":0,"delta":"console.log('hello')"}

event: response.custom_tool_call_input.done
data: {"type":"response.custom_tool_call_input.done","sequence_number":4,"item_id":"resp_example_fc_0","output_index":0,"input":"console.log('hello')"}

event: response.output_item.done
data: {"type":"response.output_item.done","sequence_number":5,"output_index":0,"item":{"id":"resp_example_fc_0","type":"custom_tool_call","status":"completed","call_id":"call_example_custom","name":"code_exec","namespace":"local","input":"console.log('hello')"}}

event: response.completed
data: {"type":"response.completed","sequence_number":6,"response":{"id":"resp_example","object":"response","created_at":1800000000,"status":"completed","model":"glm-5.3","output":[{"id":"resp_example_fc_0","type":"custom_tool_call","status":"completed","call_id":"call_example_custom","name":"code_exec","namespace":"local","input":"console.log('hello')"}],"usage":{"input_tokens":30,"output_tokens":18,"total_tokens":48}}}

普通函数使用 response.function_call_arguments.delta / .done,最终项携带 JSON 字符串 arguments;custom 使用原始字符串 input。不要把 custom 增量再次当 JSON 参数解码。工具名分片有歧义时,平台可先缓冲以保持 added 与 done 的类型、名称、命名空间和 call_id 一致。

工具恢复失败的流式错误结构示意(序号和 request_id 为示例):

event: response.failed
data: {"type":"response.failed","sequence_number":3,"error":{"code":"invalid_upstream_tool_call","message":"Invalid upstream function call stream; upstream execution is unknown; do not automatically retry","request_id":"req_example","retryable":false}}

该错误的字段位于事件 data.error.code、data.error.message、data.error.request_id 和 data.error.retryable。失败前上游可能已执行,不要自动重放。

客户端逐事件读取与断流处理的通用模式见流式输出。

错误码

本端点与其他 OpenAI 兼容端点共用同一错误信封与权限、下架语义:

{
  "error": {
    "message": "'model' is a required field",
    "type": "invalid_request_error",
    "code": "missing_model",
    "request_id": "req_example",
    "retryable": false
  }
}
HTTPerror.code场景与处理
400missing_model请求缺少 model 字段
400invalid_input消息或普通函数历史校验失败,包括重复/悬空 call_id 或缺失结果
400invalid_tools工具定义、tool_choice、namespace/custom 历史或 additional_tools 无效;托管/未知工具、任意 grammar 或嵌套 namespace 被拒绝
400invalid_image_url图片不是 Base64 Data URI
401missing_api_key、invalid_api_key、api_key_revoked鉴权失败;使用模型 API Key,不要重试
402insufficient_balance账户余额不足;流式在建立前预检
403model_not_allowedKey 的模型白名单不含该模型(type 为 permission_error)
404model_not_found模型不存在或已下架
404wrong_endpoint图片 / 视频模型需改用对应生成端点
404response_not_found查询、删除或取消他人、不存在或已过期的响应
404api_not_found未挂载的方法或路径(API 域名)
429proxy_user_rate_limit、proxy_key_rate_limit、proxy_ip_rate_limit、quota_exceeded/v1 共享限流或 Key 额度耗尽,见错误码与重试
502upstream_error流式建立上游连接失败,可有限重试
502invalid_upstream_tool_callnamespace/custom 输出无法安全恢复;不自动重试,保留 request_id 排查。已开流时在 response.failed 内报告同码
503model_authorization_unavailable授权服务暂不可用

上游执行阶段的错误按上游状态与代码透传到同一信封(type、code、message 保留上游语义),处理建议见错误码与重试。

注意事项与相关文档

  • usage 是返回给客户端的 Token 用量,不是金额;平台结算经过计数、缓存分类与计费处理,以用量与调用日志与计费与充值为准。
  • 图片与视频模型不能通过本端点调用(404 wrong_endpoint),请分别使用图片生成与视频生成端点;部分错误提示会给出应使用的端点路径。视觉输入需要模型支持视觉,且只能通过 Base64 Data URI 提供。
  • 函数和本页列出的 namespace/custom 工具往返可使用本端点;原生 text.format、托管工具与任意 grammar 不在本批支持范围。需要 Chat 格式工具、结构化输出或推理文本时,分别阅读工具调用、结构化输出与推理模型。

相关文档:OpenAI 兼容 API、流式输出、错误码与重试。

本页目录

Responses API — 开发者文档 · Global Flow Web