Skip to content

OpenResponses API(HTTP)

OpenClaw 的 Gateway 可提供 OpenResponses 兼容的 POST /v1/responses 接口。

该接口默认禁用,需先在配置中启用。

  • POST /v1/responses
  • 与 Gateway 使用同一端口(WS + HTTP 复用):http://<gateway-host>:<port>/v1/responses

底层请求作为普通 Gateway Agent 运行执行(与 openclaw agent 代码路径相同),因此路由/权限/配置与你的 Gateway 保持一致。

认证、安全与路由

操作行为与 OpenAI Chat Completions 相同:

  • 使用 Authorization: Bearer <token> 和正常的 Gateway 认证配置
  • 将此接口视为 Gateway 实例的完全操作员权限访问面
  • 通过 model: "openclaw"model: "openclaw/default"model: "openclaw/<agentId>"x-openclaw-agent-id 选择 Agent
  • 需要覆盖选定 Agent 的后端模型时使用 x-openclaw-model
  • 显式会话路由使用 x-openclaw-session-key
  • 需要非默认合成入站渠道上下文时使用 x-openclaw-message-channel

通过 gateway.http.endpoints.responses.enabled 启用或禁用此接口。

同一兼容接口还包括:

  • GET /v1/models
  • GET /v1/models/{id}
  • POST /v1/embeddings
  • POST /v1/chat/completions

关于 Agent 目标模型、openclaw/default、嵌入透传和后端模型覆盖的权威说明,参见 OpenAI Chat Completions模型列表与 Agent 路由

会话行为

默认情况下,接口每次请求都是无状态的(每次调用生成新的会话键)。

若请求包含 OpenResponses 的 user 字符串,Gateway 会从中派生稳定的会话键,使重复调用能共享一个 Agent 会话。

请求格式(已支持)

请求遵循基于 item 的 OpenResponses API 输入格式,当前支持:

  • input:字符串或 item 对象数组。
  • instructions:合并到系统提示中。
  • tools:客户端工具定义(function tools)。
  • tool_choice:过滤或强制使用客户端工具。
  • stream:启用 SSE 流式输出。
  • max_output_tokens:尽力限制输出(依赖 provider)。
  • user:稳定的会话路由。

已接受但当前忽略

  • max_tool_calls
  • reasoning
  • metadata
  • store
  • truncation

已支持:

  • previous_response_id:当请求在相同 agent/user/请求会话范围内时,OpenClaw 复用之前响应的会话。

Items(输入)

message

角色:systemdeveloperuserassistant

  • systemdeveloper 追加到系统提示中。
  • 最新的 userfunction_call_output item 作为"当前消息"。
  • 较早的 user/assistant 消息作为历史上下文包含在内。

function_call_output(基于轮次的工具)

将工具结果发回给模型:

json
{
  "type": "function_call_output",
  "call_id": "call_123",
  "output": "{\"temperature\": \"72F\"}"
}

reasoningitem_reference

为模式兼容性而接受,但构建提示时忽略。

工具(客户端 Function Tools)

提供工具:tools: [{ type: "function", function: { name, description?, parameters? } }]

若 Agent 决定调用工具,响应返回 function_call 输出 item。你再发送包含 function_call_output 的后续请求继续对话。

图片(input_image

支持 base64 或 URL 来源:

json
{
  "type": "input_image",
  "source": { "type": "url", "url": "https://example.com/image.png" }
}

允许的 MIME 类型(当前):image/jpegimage/pngimage/gifimage/webpimage/heicimage/heif。 最大尺寸(当前):10MB。

文件(input_file

支持 base64 或 URL 来源:

json
{
  "type": "input_file",
  "source": {
    "type": "base64",
    "media_type": "text/plain",
    "data": "SGVsbG8gV29ybGQh",
    "filename": "hello.txt"
  }
}

允许的 MIME 类型(当前):text/plaintext/markdowntext/htmltext/csvapplication/jsonapplication/pdf

最大尺寸(当前):5MB。

当前行为:

  • 文件内容被解码并添加到系统提示中,而非用户消息,因此是临时性的(不保存在会话历史中)。
  • PDF 会解析文本。若文本提取量过少,前几页会被光栅化为图片传给模型。

PDF 解析使用 Node 友好的 pdfjs-dist legacy 构建(无 worker)。现代 PDF.js 构建需要浏览器 worker/DOM globals,因此不在 Gateway 中使用。

URL 请求默认值:

  • files.allowUrltrue
  • images.allowUrltrue
  • maxUrlParts8(每次请求中 URL 类型的 input_file + input_image 部件总数)
  • 请求受到保护(DNS 解析、私有 IP 阻断、重定向上限、超时)。
  • 可按输入类型配置可选的主机名白名单(files.urlAllowlistimages.urlAllowlist)。
    • 精确主机:"cdn.example.com"
    • 通配子域名:"*.assets.example.com"(不匹配 apex 域名)
    • 空或省略白名单表示不限制主机名。
  • 若需完全禁用 URL 请求,设置 files.allowUrl: false 和/或 images.allowUrl: false

文件与图片限制(配置)

默认值可在 gateway.http.endpoints.responses 下调整:

json5
{
  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:20MB
  • maxUrlParts:8
  • files.maxBytes:5MB
  • files.maxChars:20 万字符
  • files.maxRedirects:3
  • files.timeoutMs:10s
  • files.pdf.maxPages:4
  • files.pdf.maxPixels:4,000,000
  • files.pdf.minTextChars:200
  • images.maxBytes:10MB
  • images.maxRedirects:3
  • images.timeoutMs:10s
  • HEIC/HEIF input_image 来源会被接受并在发送给 provider 前归一化为 JPEG。

安全说明:

  • 白名单在请求前和重定向跳转时均强制执行。
  • 主机名白名单不绕过私有/内部 IP 阻断。
  • 对于面向公网的 Gateway,除应用层守护外,还应在网络出口层添加控制。参见 安全

流式输出(SSE)

设置 stream: true 接收 Server-Sent Events:

  • Content-Type: text/event-stream
  • 每个事件行为 event: <type>data: <json>
  • 流以 data: [DONE] 结束

当前发出的事件类型:

  • response.created
  • response.in_progress
  • response.output_item.added
  • response.content_part.added
  • response.output_text.delta
  • response.output_text.done
  • response.content_part.done
  • response.output_item.done
  • response.completed
  • response.failed(出错时)

用量统计

底层 provider 报告 token 数时,usage 字段会被填充。

错误格式

错误使用如下 JSON 对象:

json
{ "error": { "message": "...", "type": "invalid_request_error" } }

常见情况:

  • 401:缺少/无效的认证
  • 400:无效的请求体
  • 405:HTTP 方法错误

示例

非流式:

bash
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"
  }'

流式:

bash
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"
  }'