Responses 接口
POST /v1/responses
OpenAI Responses API 兼容接口,支持有状态多轮对话、推理模型和工具调用。请求与响应为字节级透传,官方参数原样支持,无需适配。
Codex CLI、OpenAI Agents SDK 等基于 Responses API 的客户端可直接把 base_url 指向本服务使用。
与
/v1/chat/completions的区别:Responses 接口用previous_response_id在服务端串联上下文,无需每轮重传完整历史;对推理模型可保留思维链上下文。若只做单轮问答,用 基础文本对话 更简单。
端点
| 方法 | 路径 | 说明 |
|---|---|---|
POST |
/v1/responses |
创建响应 |
GET |
/v1/responses/{response_id} |
按 ID 取回响应 |
DELETE |
/v1/responses/{response_id} |
删除响应 |
POST |
/v1/responses/compact |
压缩上下文(Codex 使用) |
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model |
string | 是 | 模型 ID,如 gpt-5.5 / gpt-5.6-sol。 |
input |
string | array | 是 | 输入内容。字符串为单轮提问;数组可传多条消息或工具结果。 |
instructions |
string | 否 | 系统指令,等价于 chat 接口的 system 消息。 |
max_output_tokens |
integer | 否 | 生成的最大 token 数。 |
previous_response_id |
string | 否 | 上一轮的 id,用于串联多轮上下文。详见下文。 |
stream |
boolean | 否 | 是否流式返回(SSE),默认 false。 |
tools |
array | 否 | 可调用的工具(function calling)。 |
tool_choice |
string | object | 否 | 工具选择策略:auto / none / 指定工具。 |
reasoning |
object | 否 | 推理配置,如 {"effort": "low"}。仅推理模型有效。 |
temperature |
number | 否 | 采样温度。 |
store |
boolean | 否 | 是否在服务端保留本次响应,默认 true。 |
请求示例
cURL
curl https://api.openbili.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-5.5",
"input": "你好,请介绍一下自己"
}'Python
import openai
client = openai.OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.openbili.com/v1",
)
response = client.responses.create(
model="gpt-5.5",
input="你好,请介绍一下自己",
)
print(response.output_text)Node.js
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.openbili.com/v1",
});
const response = await client.responses.create({
model: "gpt-5.5",
input: "你好,请介绍一下自己",
});
console.log(response.output_text);多轮对话
把上一轮返回的 id 作为下一轮的 previous_response_id,服务端会自动串联上下文,无需重传历史消息。
first = client.responses.create(
model="gpt-5.5",
input="我叫小明",
)
second = client.responses.create(
model="gpt-5.5",
input="我叫什么名字?",
previous_response_id=first.id, # 串联上一轮
)
print(second.output_text) # 你叫小明响应
| 字段 | 类型 | 说明 |
|---|---|---|
id |
string | 本次响应的唯一 ID,以 nxrsp_ 开头。用于 previous_response_id 或按 ID 取回。 |
object |
string | 固定为 response。 |
created_at |
integer | Unix 时间戳。 |
model |
string | 实际使用的模型。 |
status |
string | completed / incomplete / failed。 |
output |
array | 输出项列表,含消息、工具调用等。 |
output_text |
string | 便捷字段,拼接后的纯文本输出(SDK 提供)。 |
usage |
object | token 用量:input_tokens / output_tokens / total_tokens。 |
{
"id": "nxrsp_...",
"object": "response",
"created_at": 1730000000,
"model": "gpt-5.5",
"status": "completed",
"output": [
{
"type": "message",
"role": "assistant",
"content": [{ "type": "output_text", "text": "你好!我是..." }]
}
],
"usage": { "input_tokens": 12, "output_tokens": 28, "total_tokens": 40 }
}关于 response_id
本服务返回的 id 以 nxrsp_ 开头,与 OpenAI 官方的 resp_ 格式不同。该 ID 经过签名并绑定到你的组织,用于隔离不同组织的会话数据。
使用时请注意:
- 原样回传即可。把返回的
id直接用于previous_response_id、GET或DELETE,服务会自动还原为上游 ID。 - 不要自行拼接或修改 ID,否则会返回
400 invalid_request_error。 - ID 不能跨组织使用。用其他组织的 ID 会返回
403 permission_error。 - 请勿假设 ID 与 OpenAI 官方 ID 一致——若你的系统同时对接官方与本服务,两边的 ID 不可互换。
关于 /compact
POST /v1/responses/compact 用于压缩长对话历史,主要由 Codex CLI 在会话轮次之间自动调用,通常无需手动请求。
该端点在部分上游线路上不可用,本服务会在这种情况下自动降级:改用常规 Responses 流程合成一份历史摘要后返回,保证 Codex 长会话不中断。降级对客户端透明,返回结构一致。
错误码
| 状态码 | type |
说明 |
|---|---|---|
400 |
invalid_request_error |
response_id 格式错误,或请求体不合法。 |
402 |
billing_error |
余额不足或账户欠费。 |
403 |
permission_error |
response_id 不属于当前组织,或 API Key 未通过 IP 白名单。 |
403 |
subscription_error |
订阅状态异常。 |
503 |
overloaded_error |
暂无可用线路,请稍后重试。 |
完整错误码见错误码说明。