OpenResponses API(HTTP)
OpenClaw 的 Gateway 可以提供一個 OpenResponses 相容的 POST /v1/responses 端點。
這個端點預設是停用的,需要先在設定中啟用。
POST /v1/responses- 與 Gateway 共用相同連接埠(WS + HTTP 多工):
http://<gateway-host>:<port>/v1/responses
底層實作上,請求會以標準的 Gateway 代理執行流程處理(和 openclaw agent 走相同的程式碼路徑),路由、權限、設定全部共用。
認證、安全性與路由
操作行為和 OpenAI Chat Completions 一致:
- 使用
Authorization: Bearer <token>搭配一般的 Gateway 認證設定 - 將此端點視為 gateway 實例的完整操作者存取介面
- 用
model: "openclaw:<agentId>"、model: "agent:<agentId>"或x-openclaw-agent-id選擇代理 - 用
x-openclaw-session-key明確指定 session 路由
透過 gateway.http.endpoints.responses.enabled 啟用或停用此端點。
Session 行為
預設情況下端點是每次請求無狀態(每次呼叫產生新的 session key)。
如果請求中包含 OpenResponses 的 user 字串,Gateway 會從中衍生出穩定的 session key,讓重複呼叫可以共用同一個代理 session。
請求格式(已支援)
請求遵循 OpenResponses API 的 item 型輸入。目前支援:
input:字串或 item 物件陣列。instructions:合併至系統提示詞。tools:用戶端工具定義(function 工具)。tool_choice:篩選或要求用戶端工具。stream:啟用 SSE 串流。max_output_tokens:盡力遵守的輸出上限(依 provider 而定)。user:穩定的 session 路由。
可接受但目前忽略的欄位:
max_tool_callsreasoningmetadatastoreprevious_response_idtruncation
Items(input)
message
角色:system、developer、user、assistant。
system和developer會附加到系統提示詞中。- 最近一筆
user或function_call_outputitem 會成為「當前訊息」。 - 先前的 user/assistant 訊息會作為上下文歷史記錄包含進去。
function_call_output(回合制工具)
將工具結果回傳給模型:
{
"type": "function_call_output",
"call_id": "call_123",
"output": "{\"temperature\": \"72F\"}"
}
reasoning 和 item_reference
為了 schema 相容性而接受,但在建構提示詞時會被忽略。
工具(用戶端 function 工具)
透過 tools: [{ type: "function", function: { name, description?, parameters? } }] 提供工具。
如果代理決定呼叫某個工具,回應中會包含一個 function_call 輸出項目。
接著你需要發送一個帶有 function_call_output 的後續請求來繼續該回合。
圖片(input_image)
支援 base64 或 URL 來源:
{
"type": "input_image",
"source": { "type": "url", "url": "https://example.com/image.png" }
}
允許的 MIME 類型(目前):image/jpeg、image/png、image/gif、image/webp、image/heic、image/heif。
大小上限(目前):10MB。
檔案(input_file)
支援 base64 或 URL 來源:
{
"type": "input_file",
"source": {
"type": "base64",
"media_type": "text/plain",
"data": "SGVsbG8gV29ybGQh",
"filename": "hello.txt"
}
}
允許的 MIME 類型(目前):text/plain、text/markdown、text/html、text/csv、
application/json、application/pdf。
大小上限(目前):5MB。
目前的行為:
- 檔案內容解碼後會加入系統提示詞,而非使用者訊息,因此是暫時性的(不會保存在 session 歷史中)。
- PDF 會被解析取出文字。如果文字過少,會把前幾頁轉成圖片傳給模型。
PDF 解析使用 Node 友好的 pdfjs-dist legacy build(無 worker)。現代版 PDF.js 需要瀏覽器 workers/DOM 全域物件,因此 Gateway 中不使用。
URL 擷取預設值:
files.allowUrl:trueimages.allowUrl:truemaxUrlParts:8(每次請求中 URL 型input_file+input_image的總數上限)- 請求會受到保護(DNS 解析、私有 IP 阻擋、重新導向次數限制、逾時)。
- 可選的 hostname 白名單,依輸入類型分別設定(
files.urlAllowlist、images.urlAllowlist)。- 精確主機名:
"cdn.example.com" - 萬用子網域:
"*.assets.example.com"(不符合 apex 網域)
- 精確主機名:
檔案與圖片限制(設定)
預設值可在 gateway.http.endpoints.responses 下調整:
{
gateway: {
http: {
endpoints: {
responses: {
enabled: true,
maxBodyBytes: 20000000,
maxUrlParts: 8,
files: {
allowUrl: true,
urlAllowlist: ["cdn.example.com", "*.assets.example.com"],
allowedMimes: [
"text/plain",
"text/markdown",
"text/html",
"text/csv",
"application/json",
"application/pdf",
],
maxBytes: 5242880,
maxChars: 200000,
maxRedirects: 3,
timeoutMs: 10000,
pdf: {
maxPages: 4,
maxPixels: 4000000,
minTextChars: 200,
},
},
images: {
allowUrl: true,
urlAllowlist: ["images.example.com"],
allowedMimes: [
"image/jpeg",
"image/png",
"image/gif",
"image/webp",
"image/heic",
"image/heif",
],
maxBytes: 10485760,
maxRedirects: 3,
timeoutMs: 10000,
},
},
},
},
},
}
省略時的預設值:
maxBodyBytes:20MBmaxUrlParts:8files.maxBytes:5MBfiles.maxChars:200kfiles.maxRedirects:3files.timeoutMs:10 秒files.pdf.maxPages:4files.pdf.maxPixels:4,000,000files.pdf.minTextChars:200images.maxBytes:10MBimages.maxRedirects:3images.timeoutMs:10 秒- HEIC/HEIF
input_image來源會在送交 provider 前正規化為 JPEG。
安全注意事項:
- URL 白名單在擷取前和每次重新導向時都會驗證。
- 將 hostname 加入白名單不會繞過私有/內部 IP 封鎖。
- 對於暴露在網路上的 gateway,除了應用層防護外,還應設定網路出口控管。 詳見 安全性。
串流(SSE)
設定 stream: true 接收 Server-Sent Events(SSE):
Content-Type: text/event-stream- 每一行事件格式為
event: <type>和data: <json> - 串流結束時送出
data: [DONE]
目前發送的事件類型:
response.createdresponse.in_progressresponse.output_item.addedresponse.content_part.addedresponse.output_text.deltaresponse.output_text.doneresponse.content_part.doneresponse.output_item.doneresponse.completedresponse.failed(發生錯誤時)
Usage
當底層 provider 回報 token 計數時,usage 欄位會被填入。
錯誤
錯誤以 JSON 物件回傳:
{ "error": { "message": "...", "type": "invalid_request_error" } }
常見情況:
401缺少或無效的認證400無效的請求內容405錯誤的 HTTP 方法
範例
非串流:
curl -sS http://127.0.0.1:18789/v1/responses \
-H 'Authorization: Bearer YOUR_TOKEN' \
-H 'Content-Type: application/json' \
-H 'x-openclaw-agent-id: main' \
-d '{
"model": "openclaw",
"input": "hi"
}'
串流:
curl -N http://127.0.0.1:18789/v1/responses \
-H 'Authorization: Bearer YOUR_TOKEN' \
-H 'Content-Type: application/json' \
-H 'x-openclaw-agent-id: main' \
-d '{
"model": "openclaw",
"stream": true,
"input": "hi"
}'