# Responses

> OpenAI Responses 格式，Codex CLI 使用這個格式。

以 OpenAI Responses 格式產生模型回應，Codex CLI 使用這個格式。

## 方法與路徑

`POST /v1/responses`

base URL 為 `https://api.relayvia.dev/v1`，Key 放在 `Authorization: Bearer sk-...`。

## 常用參數

| 參數 | 型別 | 必填 | 說明 |
| --- | --- | --- | --- |
| `model` | `string` | 是 | 官方模型名。 |
| `input` | `string \| array` | 是 | 文字，或輸入項目的清單：訊息、圖片、檔案與工具輸出。 |
| `instructions` | `string` | 否 | 系統指示。 |
| `stream` | `boolean` | 否 | `true` 時以 Server-Sent Events 串流回傳。 |
| `max_output_tokens` | `integer` | 否 | 輸出上限，決定呼叫前預扣多少。 |
| `tools` | `array` | 否 | 由你的程式執行的工具（function 與 custom）。 |
| `tool_choice` | `string \| object` | 否 | 模型必須、可以或不可呼叫工具。 |
| `reasoning` | `object` | 否 | 推理模型的推理強度與摘要。 |
| `include` | `array` | 否 | 要額外回傳的內容；帶 `reasoning.encrypted_content` 才能把推理內容接到下一輪。 |

請求照原廠格式轉發，除了下一節列出的差異，原廠的參數都能用。這裡只列常用參數，完整說明見[原廠文件](https://platform.openai.com/docs/api-reference/responses/create)。

## 與原廠不同之處

- 沒有服務端保存的對話狀態。`previous_response_id`、`conversation`、以 id 引用的輸入項目（`item_reference`，或帶 `id` 但沒有 `type` 的項目）、沒有 `encrypted_content` 的 `reasoning` 項目，以及引用已存提示範本的 `prompt`，都會回 400 `unsupported_parameter`。每一輪請在 `input` 送出完整對話。`store` 一律為 false，也不提供 `GET /v1/responses/<id>`。
- `tools[]` 接受 `function`、`custom`，以及 `execution: client` 的 `tool_search`（Codex CLI 在本機執行）。其他由原廠執行的工具回 400 `unsupported_parameter`。
- 網頁搜尋不開放。`web_search` 工具會在轉送前移除，請求照常執行、只是沒有搜尋，所以 Codex CLI 不受影響。若網頁搜尋是唯一的工具，或 `tool_choice` 指定了它，則回 400。
- 遠端檔案（`input_file.file_url`）回 400 `unsupported_parameter`，請改用 `file_data` 直接帶檔案內容。
- `user`、`safety_identifier` 與 `prompt_cache_key` 轉送前會加上會員的區隔，提示快取與使用者 id 不會和其他會員共用。
- 事件收到就轉送，`response.completed` 帶用量。每 15 秒沒有資料時送一行 `: keepalive` 註解。回應裡的模型名是你送出的名稱。
- 非串流請求 95 秒內沒有回應會收到 500 `upstream_timeout`，長輸出請改用串流。也可以帶 header `X-Robi-Keepalive: 1`：30 秒內沒有回應時先收到 200，之後每 15 秒一個空白字元，直到 JSON 送達；之後若失敗，錯誤物件會出現在 body 裡。

## 請求範例

**cURL**

```bash
curl https://api.relayvia.dev/v1/responses \
  -H "Authorization: Bearer sk-..." \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-5.5","input":"Hello!"}'
```

**Python**

```python
from openai import OpenAI

client = OpenAI(base_url="https://api.relayvia.dev/v1", api_key="sk-...")
resp = client.responses.create(model="gpt-5.5", input="Hello!")
print(resp.output_text)
```

**JavaScript**

```javascript
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.relayvia.dev/v1", apiKey: "sk-..." });
const resp = await client.responses.create({ model: "gpt-5.5", input: "Hello!" });
console.log(resp.output_text);
```

## 回應與串流範例

**回應**

```json
{
  "id": "resp_abc123",
  "object": "response",
  "status": "completed",
  "model": "gpt-5.5",
  "output": [
    {
      "type": "message",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "Hello! How can I help?"
        }
      ]
    }
  ],
  "usage": {
    "input_tokens": 9,
    "output_tokens": 8,
    "total_tokens": 17
  }
}
```

**串流**

```text
event: response.created
data: {"type":"response.created","response":{"id":"resp_abc123","status":"in_progress","model":"gpt-5.5"}}

event: response.output_text.delta
data: {"type":"response.output_text.delta","delta":"Hello!"}

event: response.completed
data: {"type":"response.completed","response":{"id":"resp_abc123","status":"completed","model":"gpt-5.5","usage":{"input_tokens":9,"output_tokens":8,"total_tokens":17}}}
```

## 這個端點會出現的錯誤

| HTTP | code | 情境 |
| --- | --- | --- |
| `400` | `invalid_request` | 參數錯誤。若是模型拒絕，訊息是這一句；有指出欄位時是 `Invalid value for parameter <param>.`，並帶 `param`。 |
| `400` | `unsupported_parameter` | 請求用到 Relayvia 不提供的功能：服務端保存的狀態、由原廠執行的工具或遠端檔案。`param` 指出是哪一個。 |
| `400` | `invalid_media` | 模型回報輸入的圖片或檔案格式不支援。 |
| `400` | `media_unreachable` | 模型讀不到你給的素材網址。 |
| `400` | `content_policy` | 輸入沒有通過模型的安全審核。若模型沒說是輸入還是輸出被拒，訊息是 `Content did not pass safety review.` |
| `400` | `content_policy_output` | 模型已經產生結果，但結果沒有通過安全審核；不是你的輸入違規。 |
| `401` | `invalid_api_key` | API Key 不存在、已停用或已過期。 |
| `402` | `insufficient_quota` | 額度不足：會員額度、Key 上限、團隊成員上限或會員花費上限不夠這次請求，或會員已凍結。 |
| `403` | `forbidden` | 這把 Key 不能用該模型、來源 IP 不在白名單，或會員已停用。 |
| `404` | `model_not_found` | 模型沒有上架。 |
| `413` | `request_too_large` | 請求 body 超過 20 MB，或模型回報輸入超過上下文或大小上限（此時訊息是這一句）。 |
| `429` | `rate_limited` | 請求太頻繁：每 IP、每把 Key、每個會員，或同時進行中的請求太多。 |
| `500` | `model_price_error` | 模型尚未設定價格，暫時不能呼叫。 |
| `500` | `upstream_error` | Relayvia 已改用其他模型服務重試，每一次都失敗。 |
| `503` | `no_available_channel` | 這個模型目前沒有可用的服務。 |
| `503` | `service_busy` | 這個模型的每個服務都回報忙碌。 |
| `500` | `upstream_timeout` | 每一次嘗試都逾時。 |

每個錯誤碼的固定訊息與處理方式見[錯誤碼](https://relayvia.dev/docs/errors)頁。
