OpenResponses API (HTTP)
OpenClaw 网关可以提供一个兼容 OpenResponses 的 POST /v1/responses 端点。
这个端点默认关闭,需要先在配置中启用。
POST /v1/responses- 与网关共用同一端口(WS + HTTP 复用):
http://<gateway-host>:<port>/v1/responses
底层实现上,请求走的是和 openclaw agent 完全相同的网关 agent 执行路径,路由、权限、配置都一致。
认证、安全与路由
行为与 OpenAI Chat Completions 一致:
- 使用
Authorization: Bearer <token>配合网关认证配置 - 视为对网关实例的完整操作员级访问
- 通过
model: "openclaw:<agentId>"、model: "agent:<agentId>"或x-openclaw-agent-id选择 agent - 使用
x-openclaw-session-key显式控制会话路由
通过 gateway.http.endpoints.responses.enabled 启用或禁用。
会话行为
默认每次请求无状态(每次调用生成新的 session key)。
如果请求中包含 OpenResponses 的 user 字段,网关会从中派生稳定的 session key,多次调用可以共享同一个 agent 会话。
请求格式(已支持)
请求遵循 OpenResponses API 的 item 输入模式。当前支持:
input:字符串或 item 对象数组。instructions:合并到系统提示词中。tools:客户端工具定义(函数工具)。tool_choice:过滤或指定客户端工具。stream:启用 SSE 流式传输。max_output_tokens:尽力输出限制(取决于模型提供商)。user:稳定的会话路由。
已接受但当前忽略的字段:
max_tool_callsreasoningmetadatastoreprevious_response_idtruncation
Item(输入)
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 而接受,但在构建提示词时忽略。
工具(客户端函数工具)
通过 tools: [{ type: "function", function: { name, description?, parameters? } }] 提供工具。
如果 agent 决定调用工具,响应会返回一个 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。
当前行为:
- 文件内容解码后添加到系统提示词中(而非用户消息),因此是临时的(不会持久化到会话历史)。
- PDF 会提取文本。如果提取到的文本很少,会将前几页光栅化为图片传给模型。
PDF 解析使用 Node 兼容的 pdfjs-dist legacy 构建(无 worker)。现代 PDF.js 构建依赖浏览器 worker/DOM 全局对象,网关中不使用。
URL 获取默认值:
files.allowUrl:trueimages.allowUrl:truemaxUrlParts:8(每个请求中基于 URL 的input_file+input_image部分总数)- 请求受到保护(DNS 解析、私有 IP 拦截、重定向次数限制、超时)。
- 支持按输入类型配置主机名白名单(
files.urlAllowlist、images.urlAllowlist)。- 精确主机:
"cdn.example.com" - 通配符子域名:
"*.assets.example.com"(不匹配顶级域名)
- 精确主机:
文件和图片限制(配置)
默认值可在 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:10sfiles.pdf.maxPages:4files.pdf.maxPixels:4,000,000files.pdf.minTextChars:200images.maxBytes:10MBimages.maxRedirects:3images.timeoutMs:10s- HEIC/HEIF
input_image来源会在发送给模型提供商之前归一化为 JPEG。
安全说明:
- URL 白名单在请求发出前和每次重定向时都会校验。
- 即使加了白名单,私有/内网 IP 拦截依然生效。
- 面向互联网的网关,建议在应用层防护之外再加上网络出口控制。详见 安全。
流式传输(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(出错时)
用量
当底层模型提供商返回 token 计数时,usage 字段会被填充。
错误
错误使用如下 JSON 格式:
{ "error": { "message": "...", "type": "invalid_request_error" } }
常见情况:
401缺少或无效的认证400无效的请求体405请求方法错误
示例
非流式:
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"
}'