# OpenAI 响应格式（Responses）

> 官方文档：[创建模型响应（API 参考）](https://developers.openai.com/api/reference/resources/responses/methods/create) · [流式事件](https://developers.openai.com/api/reference/resources/responses/streaming-events) · [迁移到 Responses](https://developers.openai.com/api/docs/guides/migrate-to-responses)

## 📝 简介

Responses API 是 OpenAI 推荐所有新项目使用的接口（Chat Completions 仍受支持）。它在一个接口中支持文本、图像、文件输入，文本或 JSON 输出，函数调用，内置工具（联网搜索、文件检索、代码解释器等），推理配置和多轮对话。Codex CLI 等编码代理也使用这个接口，可以直接接入 BeeRoute。

- **Base URL**：`https://beeroute.ai/v1`，本页接口为 `POST https://beeroute.ai/v1/responses`
- **鉴权**：请求头 `Authorization: Bearer $API_KEY`，密钥形如 `sk-xxx`，在 [令牌页面](https://beeroute.ai/keys) 创建
- **模型与价格**：见 [模型价格页](https://beeroute.ai/modellist/?lang=zh)

运行本页的 curl 示例前，先在终端设置密钥：

```bash
export API_KEY=sk-xxx
```

?> OpenAI 模型（如 `gpt-5.6-terra`、`gpt-5.6-sol`）原生支持本接口。其他模型（如 `claude-sonnet-5-5`、`glm-5.3`）经 BeeRoute 格式转换后也可以用 Responses 格式调用，但 OpenAI 的内置工具（`web_search`、`file_search`、`code_interpreter`、`image_generation`、`computer`、远程 `mcp` 等）只有在上游为 OpenAI 时才可用。

!> `previous_response_id`、`store`、`conversation` 等依赖上游存储的功能取决于实际承接请求的上游渠道，并非所有模型都可用。最稳妥的多轮做法是每次重发完整对话历史（见下方示例）。

## 💡 请求示例

以下响应示例只用于说明结构，省略了部分字段，ID 与 token 数为示意值，实际以接口返回为准。

### 基础文本 ✅

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "input": "用三句话讲一个关于独角兽的睡前故事。"
  }'
```

**响应示例：**

```json
{
  "id": "resp_68a1f2c3d4e5f6a7b8c9d0e1f2a3b4c5",
  "object": "response",
  "created_at": 1795766400,
  "status": "completed",
  "completed_at": 1795766403,
  "error": null,
  "incomplete_details": null,
  "instructions": null,
  "max_output_tokens": null,
  "model": "gpt-5.6-terra",
  "output": [
    {
      "type": "reasoning",
      "id": "rs_68a1f2c41a2b3c4d5e6f7a8b9c0d1e2f",
      "summary": [],
      "encrypted_content": "gAAAAABo...（已截断）"
    },
    {
      "type": "message",
      "id": "msg_68a1f2c55b6c7d8e9f0a1b2c3d4e5f6a",
      "status": "completed",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "月光洒满森林时，小独角兽璐娜发现了一汪会倒映星星的湖水。她把独角轻轻浸入水中，湖面便亮起一条通往星空的小路。她为所有还在做梦的孩子许下愿望，然后踏着闪光的蹄印慢慢回家睡觉。",
          "annotations": [],
          "logprobs": []
        }
      ]
    }
  ],
  "parallel_tool_calls": true,
  "previous_response_id": null,
  "reasoning": {
    "effort": "medium",
    "summary": null
  },
  "store": true,
  "temperature": 1.0,
  "text": {
    "format": {
      "type": "text"
    },
    "verbosity": "medium"
  },
  "tool_choice": "auto",
  "tools": [],
  "top_p": 1.0,
  "truncation": "disabled",
  "usage": {
    "input_tokens": 21,
    "input_tokens_details": {
      "cached_tokens": 0,
      "cache_write_tokens": 0
    },
    "output_tokens": 156,
    "output_tokens_details": {
      "reasoning_tokens": 64
    },
    "total_tokens": 177
  },
  "user": null,
  "metadata": {}
}
```

?> 不要假设 `output[0]` 就是回答：推理模型通常先返回 `reasoning` 条目，再返回 `message` 条目。请遍历 `output`，取 `type` 为 `message` 的条目中的 `output_text`。

### 系统指令 instructions ✅

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "instructions": "你是一名简洁的技术助理，每次回答不超过 50 个字。",
    "input": "什么是 HTTP 幂等性？"
  }'
```

`instructions` 相当于在上下文最前面插入一条 system（developer）消息。响应结构与基础文本示例相同。与 `previous_response_id` 一起使用时，上一轮的 `instructions` 不会被自动带入，需要每次重新传入。

### 图像输入 ✅

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "input": [
      {
        "role": "user",
        "content": [
          {"type": "input_text", "text": "描述这张图片中的内容。"},
          {
            "type": "input_image",
            "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
            "detail": "auto"
          }
        ]
      }
    ]
  }'
```

`image_url` 可以是公网 URL，也可以是 base64 data URL（如 `data:image/png;base64,...`）。BeeRoute 不提供 Files 上传接口，`file_id` 只能引用上游账户中已存在的文件，一般请使用 `image_url`。

### 流式响应 ✅

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "input": "你好！",
    "stream": true
  }'
```

**流式响应示例**（SSE；为便于阅读，`response` 对象做了删减，以 `:` 开头的是 SSE 注释行）：

```text
event: response.created
data: {"type":"response.created","sequence_number":0,"response":{"id":"resp_68a1f3d0e1f2a3b4c5d6e7f8a9b0c1d2","object":"response","created_at":1795766400,"status":"in_progress","model":"gpt-5.6-terra","output":[],"usage":null}}

event: response.in_progress
data: {"type":"response.in_progress","sequence_number":1,"response":{"id":"resp_68a1f3d0e1f2a3b4c5d6e7f8a9b0c1d2","object":"response","created_at":1795766400,"status":"in_progress","model":"gpt-5.6-terra","output":[],"usage":null}}

event: response.output_item.added
data: {"type":"response.output_item.added","sequence_number":2,"output_index":0,"item":{"id":"rs_68a1f3d11a2b3c4d5e6f7a8b9c0d1e2f","type":"reasoning","summary":[]}}

event: response.output_item.done
data: {"type":"response.output_item.done","sequence_number":3,"output_index":0,"item":{"id":"rs_68a1f3d11a2b3c4d5e6f7a8b9c0d1e2f","type":"reasoning","summary":[],"encrypted_content":"gAAAAABo..."}}

event: response.output_item.added
data: {"type":"response.output_item.added","sequence_number":4,"output_index":1,"item":{"id":"msg_68a1f3d25b6c7d8e9f0a1b2c3d4e5f6a","type":"message","status":"in_progress","role":"assistant","content":[]}}

event: response.content_part.added
data: {"type":"response.content_part.added","sequence_number":5,"item_id":"msg_68a1f3d25b6c7d8e9f0a1b2c3d4e5f6a","output_index":1,"content_index":0,"part":{"type":"output_text","text":"","annotations":[],"logprobs":[]}}

event: response.output_text.delta
data: {"type":"response.output_text.delta","sequence_number":6,"item_id":"msg_68a1f3d25b6c7d8e9f0a1b2c3d4e5f6a","output_index":1,"content_index":0,"delta":"你好！","logprobs":[]}

: 中间的 delta 事件省略

event: response.output_text.done
data: {"type":"response.output_text.done","sequence_number":9,"item_id":"msg_68a1f3d25b6c7d8e9f0a1b2c3d4e5f6a","output_index":1,"content_index":0,"text":"你好！有什么可以帮你的吗？","logprobs":[]}

event: response.content_part.done
data: {"type":"response.content_part.done","sequence_number":10,"item_id":"msg_68a1f3d25b6c7d8e9f0a1b2c3d4e5f6a","output_index":1,"content_index":0,"part":{"type":"output_text","text":"你好！有什么可以帮你的吗？","annotations":[],"logprobs":[]}}

event: response.output_item.done
data: {"type":"response.output_item.done","sequence_number":11,"output_index":1,"item":{"id":"msg_68a1f3d25b6c7d8e9f0a1b2c3d4e5f6a","type":"message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"你好！有什么可以帮你的吗？","annotations":[],"logprobs":[]}]}}

event: response.completed
data: {"type":"response.completed","sequence_number":12,"response":{"id":"resp_68a1f3d0e1f2a3b4c5d6e7f8a9b0c1d2","object":"response","created_at":1795766400,"status":"completed","model":"gpt-5.6-terra","output":[{"id":"rs_68a1f3d11a2b3c4d5e6f7a8b9c0d1e2f","type":"reasoning","summary":[],"encrypted_content":"gAAAAABo..."},{"id":"msg_68a1f3d25b6c7d8e9f0a1b2c3d4e5f6a","type":"message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"你好！有什么可以帮你的吗？","annotations":[],"logprobs":[]}]}],"usage":{"input_tokens":12,"input_tokens_details":{"cached_tokens":0,"cache_write_tokens":0},"output_tokens":28,"output_tokens_details":{"reasoning_tokens":16},"total_tokens":40}}}
```

每个事件都带有递增的 `sequence_number`；流以 `response.completed`（或 `response.incomplete`、`response.failed`）事件结束。完整事件列表见下文「流式事件」。

### 函数调用 ✅

**第 1 步：** 声明工具，模型返回 `function_call`。

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "input": "上海今天天气怎么样？",
    "tools": [
      {
        "type": "function",
        "name": "get_weather",
        "description": "获取指定城市的当前天气",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {"type": "string", "description": "城市名，例如 上海"},
            "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
          },
          "required": ["city", "unit"],
          "additionalProperties": false
        },
        "strict": true
      }
    ],
    "tool_choice": "auto"
  }'
```

**响应示例（节选）：**

```json
{
  "id": "resp_68a1f4e0a1b2c3d4e5f6a7b8c9d0e1f2",
  "object": "response",
  "status": "completed",
  "model": "gpt-5.6-terra",
  "output": [
    {
      "type": "reasoning",
      "id": "rs_68a1f4e11a2b3c4d5e6f7a8b9c0d1e2f",
      "summary": [],
      "encrypted_content": "gAAAAABo...（已截断）"
    },
    {
      "type": "function_call",
      "id": "fc_68a1f4e25b6c7d8e9f0a1b2c3d4e5f6a",
      "call_id": "call_Q8xH2kLm3nP4",
      "name": "get_weather",
      "arguments": "{\"city\":\"上海\",\"unit\":\"celsius\"}",
      "status": "completed"
    }
  ]
}
```

**第 2 步：** 在你的代码中执行函数，把调用和结果一起放进 `input` 再请求一次（`function_call_output` 的 `call_id` 必须与模型返回的一致）：

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "input": [
      {"role": "user", "content": "上海今天天气怎么样？"},
      {
        "type": "function_call",
        "call_id": "call_Q8xH2kLm3nP4",
        "name": "get_weather",
        "arguments": "{\"city\":\"上海\",\"unit\":\"celsius\"}"
      },
      {
        "type": "function_call_output",
        "call_id": "call_Q8xH2kLm3nP4",
        "output": "{\"temperature\":18,\"condition\":\"多云\"}"
      }
    ],
    "tools": [
      {
        "type": "function", "name": "get_weather", "description": "获取指定城市的当前天气", "strict": true,
        "parameters": {
          "type": "object",
          "properties": {"city": {"type": "string", "description": "城市名，例如 上海"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}},
          "required": ["city", "unit"], "additionalProperties": false
        }
      }
    ]
  }'
```

模型会基于函数结果返回最终回答（`message` 条目）。

?> 官方推荐的做法是把第 1 步 `output` 中的**全部条目**（包括 `reasoning` 条目及其 `encrypted_content`）原样追加到 `input`，这样能保留推理上下文。示例为简洁只回传了不带 `id` 的 `function_call`；如果回传的条目带有 `id`，请连同它前面的 `reasoning` 条目一起原样回传。一次响应可能包含多个 `function_call`，每个都需要回传对应的 `function_call_output`。

### 结构化输出 ✅

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "input": "从这句话中提取事件：周五下午三点，小李和小王在 3 号会议室评审发布计划。",
    "text": {
      "format": {
        "type": "json_schema",
        "name": "calendar_event",
        "strict": true,
        "schema": {
          "type": "object",
          "properties": {
            "title": {"type": "string"},
            "time": {"type": "string"},
            "participants": {"type": "array", "items": {"type": "string"}}
          },
          "required": ["title", "time", "participants"],
          "additionalProperties": false
        }
      }
    }
  }'
```

**响应示例（`output` 中的 `message` 条目）：**

```json
{
  "type": "message",
  "id": "msg_68a1f5a05b6c7d8e9f0a1b2c3d4e5f6a",
  "status": "completed",
  "role": "assistant",
  "content": [
    {
      "type": "output_text",
      "text": "{\"title\":\"评审发布计划\",\"time\":\"周五 15:00\",\"participants\":[\"小李\",\"小王\"]}",
      "annotations": [],
      "logprobs": []
    }
  ]
}
```

`output_text.text` 是符合 schema 的 JSON 字符串，需要自行解析。模型出于安全原因拒绝时，`content` 中会出现 `refusal` 类型的内容而不是 JSON。

### 推理 ✅

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "input": "一个三位数，各位数字之和为 14，百位比个位大 2，十位是个位的 2 倍。求这个数。",
    "reasoning": {
      "effort": "high",
      "summary": "auto"
    }
  }'
```

**响应示例（节选）：**

```json
{
  "id": "resp_68a1f6b0a1b2c3d4e5f6a7b8c9d0e1f2",
  "object": "response",
  "status": "completed",
  "model": "gpt-5.6-terra",
  "output": [
    {
      "type": "reasoning",
      "id": "rs_68a1f6b11a2b3c4d5e6f7a8b9c0d1e2f",
      "summary": [
        {
          "type": "summary_text",
          "text": "**设未知数求解**\n\n设个位为 x，则百位为 x+2、十位为 2x。由 (x+2)+2x+x=14 得 x=3，所以百位 5、十位 6、个位 3。"
        }
      ],
      "encrypted_content": "gAAAAABo...（已截断）"
    },
    {
      "type": "message",
      "id": "msg_68a1f6b25b6c7d8e9f0a1b2c3d4e5f6a",
      "status": "completed",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "这个数是 563。验证：5+6+3=14，5 比 3 大 2，6 是 3 的 2 倍。",
          "annotations": [],
          "logprobs": []
        }
      ]
    }
  ],
  "reasoning": {
    "effort": "high",
    "summary": "auto"
  },
  "usage": {
    "input_tokens": 52,
    "input_tokens_details": {
      "cached_tokens": 0,
      "cache_write_tokens": 0
    },
    "output_tokens": 612,
    "output_tokens_details": {
      "reasoning_tokens": 548
    },
    "total_tokens": 664
  }
}
```

`reasoning.effort` 支持的取值和默认值因模型而异；只有设置了 `reasoning.summary` 才会返回推理摘要。`output_tokens` 已包含 `reasoning_tokens`，推理 token 按输出计费。

### 联网搜索 ✅

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "input": "查阅 OpenAI 官方文档，Responses API 的 store 参数默认值是什么？请附上来源。",
    "tools": [
      {
        "type": "web_search",
        "filters": {"allowed_domains": ["developers.openai.com"]},
        "search_context_size": "medium",
        "user_location": {"type": "approximate", "country": "CN", "timezone": "Asia/Shanghai"}
      }
    ],
    "include": ["web_search_call.action.sources"]
  }'
```

**响应示例（节选）：**

```json
{
  "id": "resp_68a1f7c0a1b2c3d4e5f6a7b8c9d0e1f2",
  "object": "response",
  "status": "completed",
  "model": "gpt-5.6-terra",
  "output": [
    {
      "type": "web_search_call",
      "id": "ws_68a1f7c11a2b3c4d5e6f7a8b9c0d1e2f",
      "status": "completed",
      "action": {
        "type": "search",
        "query": "OpenAI Responses API store parameter default",
        "sources": [
          {
            "type": "url",
            "url": "https://developers.openai.com/api/reference/resources/responses/methods/create"
          }
        ]
      }
    },
    {
      "type": "message",
      "id": "msg_68a1f7c25b6c7d8e9f0a1b2c3d4e5f6a",
      "status": "completed",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "根据 OpenAI 官方 API 参考，`store` 参数默认为 true：生成的响应会被存储，以便之后通过 API 检索，数据至少保留 30 天（存在数据保留例外）。",
          "annotations": [
            {
              "type": "url_citation",
              "start_index": 0,
              "end_index": 85,
              "url": "https://developers.openai.com/api/reference/resources/responses/methods/create",
              "title": "Create a model response | OpenAI API Reference"
            }
          ],
          "logprobs": []
        }
      ]
    }
  ]
}
```

!> 联网搜索等内置工具由 OpenAI 托管执行，只有上游为 OpenAI 时可用；内置工具调用可能按次额外计费，以 [价格页](https://beeroute.ai/modellist/?lang=zh) 为准。新集成请使用正式版 `{"type": "web_search"}`，旧的 `web_search_preview` 不支持 `filters`。

### 多轮对话 ✅

**方式一（推荐）：每次重发完整历史。** 不依赖上游存储，所有模型都可用：

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "input": [
      {"role": "user", "content": "给我的橘猫起个名字。"},
      {"role": "assistant", "content": "可以叫「橘子」或「南瓜」。"},
      {"role": "user", "content": "再给几个简短的英文名。"}
    ]
  }'
```

对推理模型，建议把上一轮 `output` 的全部条目（`reasoning`、`message` 等）原样追加到 `input`，而不是只回传文字；`gpt-5.3-codex` 及之后的模型回传 assistant 消息时应保留其 `phase` 字段。对话过长时可使用 `context_management` 或 `POST /v1/responses/compact` 压缩上下文。

**方式二：使用 `previous_response_id`。** 只发送新一轮输入，由上游根据已存储的上一轮响应续接：

```bash
curl https://beeroute.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-5.6-terra",
    "previous_response_id": "resp_68a1f2c3d4e5f6a7b8c9d0e1f2a3b4c5",
    "input": "再给几个简短的英文名。"
  }'
```

!> `previous_response_id` 依赖上游存储：上一轮必须是 `store` 为 `true` 的 OpenAI 响应，且能被同一上游找到，否则会报错；经格式转换的非 OpenAI 模型通常不支持。即使使用它，链上之前所有轮次的输入 token 仍会作为输入 token 计费。它不能与 `conversation` 同时使用。

## 📮 请求

### 端点

```text
POST https://beeroute.ai/v1/responses
```

创建模型响应。BeeRoute 还提供：

| 端点 | 说明 |
|------|------|
| `POST /v1/responses/compact` | 无状态压缩长对话：发送完整上下文（`model`、`input`、`instructions` 等），返回 `object` 为 `response.compaction` 的对象；把其中的 `output` 原样作为下一次 `/v1/responses` 请求的 `input`。不支持流式。参数见 [官方 compact 参考](https://developers.openai.com/api/reference/resources/responses/methods/compact)。 |

BeeRoute 不提供响应的查询、删除、取消接口（`GET`/`DELETE /v1/responses/{id}`、`/cancel`、`/input_items`、`/input_tokens`）。

### 鉴权方法

```text
Authorization: Bearer $API_KEY
```

### 请求体参数

以下为常用参数；完整字段与最新定义以 [官方 API 参考](https://developers.openai.com/api/reference/resources/responses/methods/create) 为准。

#### 基础参数

| 参数 | 类型 | 说明 |
|------|------|------|
| `model` | string | **在 BeeRoute 上必填**。模型 ID，如 `gpt-5.6-terra`。OpenAI 规范中它是可选的（可由 `prompt` 提供），但 BeeRoute 需要它来路由渠道和计费。 |
| `input` | string 或 array | 模型输入。字符串等价于一条 `user` 角色的文本消息；数组为输入项列表（见「输入项类型」）。规范中为可选（上下文也可来自 `prompt`、`conversation` 或 `previous_response_id`）。 |
| `instructions` | string 或 null | 插入上下文最前面的 system（developer）消息。与 `previous_response_id` 一起使用时，上一轮的 instructions 不会被带入。 |
| `stream` | boolean 或 null | 默认 `false`。为 `true` 时以 SSE 流式返回。 |
| `stream_options` | object 或 null | 仅在 `stream: true` 时设置。其中 `include_obfuscation` 默认会被 BeeRoute 过滤，即沿用上游默认行为。 |
| `max_output_tokens` | integer 或 null | 本次响应可生成的 token 上限，包含可见输出和推理 token，最小值 16。 |
| `temperature` | number 或 null | 采样温度，0–2，默认 1。建议只调整它或 `top_p` 其中之一。 |
| `top_p` | number 或 null | 核采样，0–1，默认 1。 |
| `top_logprobs` | integer 或 null | 0–20，每个 token 位置返回的最可能 token 数（附对数概率）。 |
| `metadata` | object 或 null | 最多 16 个键值对，键不超过 64 个字符，值不超过 512 个字符。 |
| `include` | array 或 null | 额外返回的数据，取值见下表。 |
| `text` | object | 文本输出配置（`format`、`verbosity`），见下文。 |
| `reasoning` | object 或 null | 推理配置（`effort`、`summary` 等），见下文。 |
| `tools` | array | 模型可调用的工具，见「工具概览」。 |
| `tool_choice` | string 或 object | 工具选择方式，见下文。 |
| `parallel_tool_calls` | boolean 或 null | 是否允许并行调用工具，默认 `true`。 |
| `max_tool_calls` | integer 或 null | 一次响应中内置工具调用的总次数上限（所有内置工具合计），超出后模型的调用会被忽略。 |
| `prompt` | object 或 null | 引用提示词模板：`id`、`variables`、`version`。模板属于上游 OpenAI 账户，通常无法在 BeeRoute 上使用。 |

#### 状态、缓存与安全参数

| 参数 | 类型 | 说明 |
|------|------|------|
| `store` | boolean 或 null | 是否在上游存储响应以便之后检索，默认 `true`（至少保留 30 天，存在例外）。BeeRoute 默认原样转发。 |
| `previous_response_id` | string 或 null | 上一轮响应的 ID，用于串联多轮对话；不能与 `conversation` 同时使用。⚠️ 依赖上游存储，并非所有模型都可用。 |
| `conversation` | string 或 object 或 null | 本次响应所属的会话（会话 ID 或 `{"id": "..."}`）。会话中的条目会加在本次输入之前，本轮输入和输出在完成后自动写回会话。⚠️ 会话需通过 OpenAI Conversations API 创建，BeeRoute 未提供该接口，一般不可用。 |
| `context_management` | array 或 null | 服务端上下文压缩，例如 `[{"type": "compaction", "compact_threshold": 200000}]`（`compact_threshold` 最小 1000）。渲染后的 token 数超过阈值时由上游压缩，输出中会带有加密的 `compaction` 条目。 |
| `prompt_cache_key` | string 或 null | 提示缓存分桶键，用于提高缓存命中率，取代 `user` 字段在缓存方面的作用。 |
| `prompt_cache_options` | object | 提示缓存选项（`gpt-5.6` 及之后的模型）：`mode`（`implicit` 默认，或 `explicit`）、`ttl`（`30m` 默认，或 `12h`）、`prewarm`、`comparison_response_id`。 |
| `moderation` | object 或 null | 对输入和输出运行审核：`model`（如 `omni-moderation-latest`），`policy.input.mode` / `policy.output.mode` 取 `score` 或 `block`。 |
| `safety_identifier` | string 或 null | 标识终端用户的稳定 ID（不超过 128 个字符，建议先做哈希），用于检测违规使用。BeeRoute 默认不转发给上游。 |
| `service_tier` | string 或 null | OpenAI 取值：`auto`、`default`、`flex`、`scale`、`priority`、`fast`、`ultrafast`。BeeRoute 默认过滤此参数，通常不生效。 |

#### 已弃用参数

| 参数 | 说明 |
|------|------|
| `user` | 正被 `safety_identifier` 和 `prompt_cache_key` 取代；如需缓存优化请改用 `prompt_cache_key`。 |
| `truncation` | `auto` 或 `disabled`（默认）。`auto`：输入超出上下文窗口时，从对话**开头**丢弃条目；`disabled`：超出时返回 400。规范已标记弃用，长对话建议改用 `context_management` 或 `/v1/responses/compact`。 |
| `prompt_cache_retention` | `in_memory` 或 `24h`，已弃用，改用 `prompt_cache_options.ttl`。 |
| `reasoning.generate_summary` | 已弃用，改用 `reasoning.summary`。BeeRoute 不会转发该字段。 |
| `include` 中的 `reasoning.encrypted_content` | 推理条目现在默认返回 `encrypted_content`，该取值仍被接受，但已不再必需。 |

#### BeeRoute 不转发的参数

`background`、`background_options`、`access_programs`、`image`（请求级图像选项）目前不会被转发给上游，传入后会被忽略。因此后台模式（以及与之相关的 `queued` 状态和取消操作）在 BeeRoute 上不可用。

#### include 取值

| 值 | 说明 |
|------|------|
| `web_search_call.action.sources` | 返回联网搜索调用使用的来源 |
| `web_search_call.results` | 返回联网搜索调用的结果 |
| `code_interpreter_call.outputs` | 返回代码解释器中 Python 代码的执行输出 |
| `computer_call_output.output.image_url` | 返回 computer 调用输出中的图像 URL |
| `file_search_call.results` | 返回文件检索调用的检索结果 |
| `message.input_image.image_url` | 返回输入消息中的图像 URL |
| `message.output_text.logprobs` | 在 assistant 消息中返回 logprobs |
| `reasoning.encrypted_content` | 在推理条目中返回加密的推理内容（现已默认返回） |

#### reasoning 对象

| 字段 | 说明 |
|------|------|
| `effort` | 推理强度：`none`、`minimal`、`low`、`medium`、`high`、`xhigh`、`max`。支持的取值和默认值因模型而异（`gpt-5.6` 系列默认 `medium`），部分模型对不支持的取值会返回 400。 |
| `summary` | 推理摘要：`auto`、`concise`、`detailed`。设置后摘要出现在 `reasoning` 输出条目的 `summary` 中；不设置则不返回摘要。 |
| `mode` | 执行模式：`standard`（默认）或 `pro`，GPT-5.6 与 GPT-6 系列支持。 |
| `context` | 后续轮次是否把之前轮次的推理渲染给模型：`auto`、`current_turn`、`all_turns`。`gpt-5.6` 系列默认 `all_turns`，更早的模型默认 `current_turn`。 |

#### text 对象

| 字段 | 说明 |
|------|------|
| `format` | 输出格式，默认 `{"type": "text"}`。设为 `json_schema` 启用结构化输出；`{"type": "json_object"}` 是旧版 JSON 模式，需要在提示中明确要求输出 JSON，支持 `json_schema` 的模型不推荐使用。 |
| `verbosity` | 回答详略：`low`、`medium`（默认）、`high`。 |

`format` 为 `json_schema` 时的字段：

| 字段 | 必需 | 说明 |
|------|------|------|
| `type` | 是 | 固定为 `json_schema` |
| `name` | 是 | 格式名称，仅限 a-z、A-Z、0-9、下划线和短横线，最长 64 个字符 |
| `schema` | 是 | 输出需遵循的 JSON Schema |
| `description` | 否 | 格式用途说明，帮助模型理解如何输出 |
| `strict` | 否 | 默认 `false`。为 `true` 时严格遵循 schema（仅支持 JSON Schema 的子集），推荐开启 |

#### tool_choice

| 取值 | 说明 |
|------|------|
| `"auto"` | 模型自行决定生成消息还是调用工具 |
| `"none"` | 不调用任何工具，直接生成消息 |
| `"required"` | 必须调用一个或多个工具 |
| `{"type": "function", "name": "get_weather"}` | 强制调用指定函数 |
| `{"type": "custom", "name": "..."}` | 强制调用指定的自定义工具 |
| `{"type": "mcp", "server_label": "...", "name": "..."}` | 强制调用指定 MCP 服务器上的工具（`name` 可选） |
| `{"type": "allowed_tools", "mode": "auto", "tools": [...]}` | 把可用工具限定为 `tools` 中列出的子集；`mode` 为 `auto` 或 `required` |
| `{"type": "file_search"}` 等内置工具类型 | 强制使用内置工具，`type` 可为 `file_search`、`web_search_preview`、`web_search_preview_2025_03_11`、`computer`、`computer_use_preview`、`computer_use`、`code_interpreter`、`image_generation` |
| `{"type": "apply_patch"}` 或 `{"type": "shell"}` | 强制调用 apply_patch 或 shell 工具 |

### 输入项类型

`input` 为数组时，每一项可以是消息，也可以是上一轮的输出条目或工具结果。

**消息**（`type` 可省略，默认为 `message`）：

| 字段 | 说明 |
|------|------|
| `role` | `user`、`assistant`、`system` 或 `developer`。`developer`/`system` 指令优先于 `user` |
| `content` | 字符串，或内容数组（见下表） |
| `phase` | 仅用于 assistant 消息：`commentary`（中间说明）或 `final_answer`（最终回答）。`gpt-5.3-codex` 及之后的模型回传时应保留 |

**内容类型**：

| 类型 | 字段 |
|------|------|
| `input_text` | `text`：文本内容 |
| `input_image` | `image_url`（公网 URL 或 base64 data URL）、`file_id`、`detail`（`low`、`high`、`auto` 默认、`original`） |
| `input_file` | `file_data`（base64 data URL，如 `data:application/pdf;base64,...`）、`file_url`、`file_id`、`filename`、`detail`（`auto` 默认、`low`、`high`） |

各内容块还可带 `prompt_cache_breakpoint`（`{"mode": "explicit", "ttl": "30m"}`）显式标记可复用的提示前缀。BeeRoute 不提供 Files 接口，`file_id` 只能引用上游账户中已存在的文件。

**其他输入项**：

| 类型 | 用途 |
|------|------|
| `message`（assistant 输出） | 上一轮模型输出的消息，原样回传 |
| `reasoning` | 上一轮的推理条目（含 `encrypted_content`），回传可保留推理上下文 |
| `function_call` | 上一轮模型发起的函数调用 |
| `function_call_output` | 函数执行结果：`call_id` 加 `output`（字符串，或 `input_text`/`input_image`/`input_file` 数组） |
| `custom_tool_call` / `custom_tool_call_output` | 自定义工具的调用与结果 |
| `computer_call_output` | 执行 computer 动作后回传的截图 |
| `mcp_approval_response` | 对 MCP 调用审批请求的答复 |
| `shell_call_output` / `local_shell_call_output` / `apply_patch_call_output` | 客户端执行 shell 或 apply_patch 后回传的结果（编码代理使用） |
| `compaction` | 压缩后的加密上下文条目，原样回传 |
| `compaction_trigger` | 要求压缩当前上下文，必须是最后一个输入项 |
| `item_reference` | 按 ID 引用已存储的条目（依赖上游存储） |

### 工具概览

| 类型 | 说明 | 在 BeeRoute 上 |
|------|------|------|
| `function` | 你自己代码中的函数。模型返回 `function_call`，由你执行后回传 `function_call_output` | 支持工具调用的模型均可用 |
| `custom` | 自定义工具，输入为自由文本（可用语法约束格式），模型返回 `custom_tool_call` | OpenAI 模型 |
| `web_search`（或 `web_search_2025_08_26`） | 联网搜索（正式版） | 仅 OpenAI 上游 |
| `web_search_preview` | 旧版预览搜索工具，新集成请改用 `web_search` | 仅 OpenAI 上游 |
| `file_search` | 在向量库中检索文件，需要 `vector_store_ids` | 仅 OpenAI 上游，且向量库须存在于上游账户 |
| `code_interpreter` | 在容器中运行 Python 代码 | 仅 OpenAI 上游 |
| `image_generation` | 使用 GPT Image 模型生成或编辑图片 | 仅 OpenAI 上游 |
| `mcp` | 通过远程 MCP 服务器或连接器提供更多工具 | 仅 OpenAI 上游 |
| `computer` | 计算机使用（正式版；旧版为 `computer_use_preview`）。模型返回 `computer_call`，由你的环境执行动作并回传截图 | 仅 OpenAI 上游 |
| `shell` / `local_shell` / `apply_patch` | 执行 shell 命令、以 diff 修改文件，供编码代理（如 Codex）使用 | OpenAI 模型 |
| `tool_search` / `namespace` | 延迟加载大量工具（配合 `defer_loading`）、按命名空间分组工具 | OpenAI 模型（`tool_search` 需 `gpt-5.4` 及之后） |

**function 工具字段**：

| 字段 | 说明 |
|------|------|
| `type` | 固定为 `function` |
| `name` | 函数名 |
| `description` | 函数说明，模型据此决定是否调用 |
| `parameters` | 描述参数的 JSON Schema |
| `strict` | `true`：严格按 schema 生成参数，要求每个对象设置 `additionalProperties: false` 且所有属性都列入 `required`（可选字段用包含 `null` 的类型表示）。省略：Responses 会尽量把 schema 规范化为严格模式，无法规范化时回退为非严格（响应中该工具显示 `strict: false`）。`false`：非严格、尽力而为 |
| `defer_loading` | 是否延迟加载（配合 `tool_search`） |

**web_search 工具字段**：

| 字段 | 说明 |
|------|------|
| `type` | `web_search` 或 `web_search_2025_08_26` |
| `filters.allowed_domains` | 允许搜索的域名列表（含其子域名），不传则不限制 |
| `search_context_size` | 搜索占用的上下文量：`low`、`medium`（默认）、`high` |
| `user_location` | 近似位置：`type`（`approximate`）、`city`、`country`（两位 ISO 国家代码）、`region`、`timezone`（IANA 时区）。省略或为 null 时默认按美国处理；只传 `{"type": "approximate"}` 可避免这一回退 |
| `external_web_access` | 默认 `true`；为 `false` 时只使用离线/缓存内容，不抓取新网页 |

## 📥 响应

### 响应对象

| 字段 | 说明 |
|------|------|
| `id` | 响应 ID，以 `resp_` 开头 |
| `object` | 固定为 `response` |
| `created_at` | 创建时间（Unix 秒） |
| `completed_at` | 完成时间（Unix 秒），可能为 null |
| `status` | 响应状态，见下表 |
| `model` | 生成响应所用的模型 |
| `output` | 输出条目数组，见「输出条目」 |
| `usage` | token 用量，见「usage」 |
| `error` | 生成失败时的错误对象（`code`、`message`），否则为 null |
| `incomplete_details` | 响应未完成时的原因（`reason`），否则为 null |
| `service_tier` | 实际使用的服务层级 |
| `tool_usage` | 内置工具的用量（`image_gen`、`web_search`），可能省略 |
| `instructions`、`max_output_tokens`、`max_tool_calls`、`temperature`、`top_p`、`top_logprobs`、`parallel_tool_calls`、`tool_choice`、`tools`、`text`、`reasoning`、`truncation`、`metadata`、`store`、`previous_response_id`、`conversation`、`prompt`、`prompt_cache_key`、`safety_identifier`、`user` | 回显本次请求实际生效的参数 |

?> 官方 Python / JavaScript SDK 提供的 `response.output_text` 是 SDK 汇总所有 `output_text` 的便捷属性，原始 HTTP 响应中不一定有这个字段。直接调用 HTTP 时请遍历 `output`。

### status

| 值 | 说明 |
|------|------|
| `completed` | 生成完成 |
| `in_progress` | 生成中（流式过程中出现） |
| `incomplete` | 未完整生成，原因见 `incomplete_details.reason` |
| `failed` | 生成失败，详见 `error` |
| `queued` | 排队中（后台模式，BeeRoute 未提供） |
| `cancelled` | 已取消（后台模式，BeeRoute 未提供） |

`incomplete_details.reason` 可能为 `max_output_tokens`（达到 `max_output_tokens` 上限）、`content_filter`（内容被过滤）、`max_messages` 或 `steered`（WebSocket 模式下被引导停止）。

### 输出条目

| `type` | 说明 |
|------|------|
| `message` | 模型的回复消息：`id`、`role`（`assistant`）、`status`、`content`，可能带 `phase` |
| `reasoning` | 推理条目：`id`、`summary`（`summary_text` 数组）、`encrypted_content`（默认返回；流式时以 `response.output_item.done` 中的完整值为准）、可选 `content`（`reasoning_text`） |
| `function_call` | 函数调用：`id`、`call_id`、`name`、`arguments`（JSON 字符串）、`status` |
| `custom_tool_call` | 自定义工具调用：`call_id`、`name`、`input` |
| `web_search_call` | 联网搜索：`status`、`action`（`search`、`open_page` 或 `find_in_page`） |
| `file_search_call` | 文件检索：`queries`、`status`、`results` |
| `code_interpreter_call` | 代码解释器执行记录 |
| `image_generation_call` | 图片生成结果（`result` 为 base64 图片） |
| `computer_call` | 需要你执行的计算机动作 |
| `mcp_call` / `mcp_list_tools` / `mcp_approval_request` | MCP 工具调用、工具列表、审批请求 |
| `shell_call` / `local_shell_call` / `apply_patch_call` | 需要客户端执行的 shell 或补丁操作 |
| `tool_search_call` / `tool_search_output` | 工具检索过程 |
| `compaction` | 压缩后的加密上下文 |

`message.content` 中每一项为 `output_text`（`text`、`annotations`、`logprobs`）或 `refusal`（`refusal`：拒绝说明）。

**annotations 类型**：

| 类型 | 字段 |
|------|------|
| `url_citation` | `url`、`title`、`start_index`、`end_index`（引用在文本中的起止字符位置） |
| `file_citation` | `file_id`、`filename`、`index`（在输出文本中插入引用的位置） |
| `container_file_citation` | `container_id`、`file_id`、`filename`、`start_index`、`end_index` |
| `file_path` | `file_id`、`index` |

### usage

| 字段 | 说明 |
|------|------|
| `input_tokens` | 输入 token 数 |
| `input_tokens_details.cached_tokens` | 命中提示缓存的输入 token 数 |
| `input_tokens_details.cache_write_tokens` | 写入缓存的输入 token 数 |
| `input_tokens_details.cache_write_tokens_details` | 可选，按缓存时长细分：`ttl_30m_tokens`、`ttl_12h_tokens` |
| `output_tokens` | 输出 token 数（已包含推理 token） |
| `output_tokens_details.reasoning_tokens` | 推理 token 数 |
| `total_tokens` | 总 token 数 |

经格式转换的非 OpenAI 模型，细分字段可能为 0 或缺省。

### 流式事件

`stream: true` 时以 SSE 返回，每个事件包含 `event:` 行和 `data:` 行，`data` 中都带有 `type` 和递增的 `sequence_number`。主要事件如下，完整列表见 [官方流式事件参考](https://developers.openai.com/api/reference/resources/responses/streaming-events)。

| 事件 | 说明 |
|------|------|
| `response.created` | 响应已创建 |
| `response.in_progress` | 响应生成中 |
| `response.output_item.added` / `response.output_item.done` | 新增 / 完成一个输出条目（`item`、`output_index`） |
| `response.content_part.added` / `response.content_part.done` | 新增 / 完成一个内容块 |
| `response.output_text.delta` | 文本增量（`delta`、`logprobs`） |
| `response.output_text.done` | 文本完成（`text`、`logprobs`） |
| `response.output_text.annotation.added` | 新增引用标注 |
| `response.refusal.delta` / `response.refusal.done` | 拒绝内容的增量 / 完成 |
| `response.function_call_arguments.delta` / `response.function_call_arguments.done` | 函数参数增量 / 完成（`arguments`） |
| `response.reasoning_summary_part.added` / `response.reasoning_summary_part.done` | 推理摘要块新增 / 完成 |
| `response.reasoning_summary_text.delta` / `response.reasoning_summary_text.done` | 推理摘要文本增量 / 完成 |
| `response.web_search_call.in_progress` / `.searching` / `.completed` | 联网搜索进度 |
| `response.completed` | 响应完成，`response` 中含完整 `output` 与 `usage` |
| `response.incomplete` | 响应未完整结束 |
| `response.failed` | 响应失败 |
| `error` | 出错（`code`、`message`、`param`） |

文本增量等 delta 事件可能带有 `obfuscation` 字段（上游的流混淆填充），客户端可以忽略。

## ⚠️ 错误

BeeRoute 网关自身的错误（密钥无效、余额不足、无可用渠道等）使用 OpenAI 风格的错误体，`type` 为 `new_api_error`，`message` 末尾附带请求 ID，联系客服时请提供：

```json
{
  "error": {
    "code": "",
    "message": "无效的令牌 (request id: 20261127080000123456789a1b2c3d4Xy7Kp2Qm)",
    "type": "new_api_error"
  }
}
```

| HTTP 状态码 | 常见原因 |
|------|------|
| 400 | 请求格式错误，例如缺少 `model` |
| 401 | API Key 无效，或令牌已禁用、已过期、令牌额度已用完 |
| 403 | 账户余额不足（`code` 为 `insufficient_user_quota`），或令牌无权使用该模型 / 分组 |
| 429 | 请求过于频繁（网关或上游限流） |
| 503 | 当前分组下该模型没有可用渠道（`code` 为 `model_not_found`） |
| 5xx | 上游服务异常 |

上游返回的错误会以 OpenAI 错误格式（`message`、`type`、`param`、`code`）转发。请求成功受理但生成失败时，响应的 `status` 为 `failed`，原因见 `error.code` 与 `error.message`；流式请求中则会收到 `response.failed` 或 `error` 事件。
