主题
语言

结构化输出

使用 response_format 强制输出 JSON。两种模式:json_object(任意有效 JSON)和 json_schema(严格遵循 schema)。提供 curl 和 Python 示例。

response_format 参数会强制模型返回有效 JSON。支持两种模式。

json_object mode

模型保证输出为有效 JSON。您可以在 system prompt 中定义结构。

curl https://getvibeapi.com/v1/chat/completions \
  -H "Authorization: Bearer $GETVIBE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-6.1-sol",
    "response_format": { "type": "json_object" },
    "messages": [
      {"role": "system", "content": "Reply with JSON with fields: name (string), score (number)."},
      {"role": "user", "content": "Rate this answer: very good"}
    ]
  }'

json_schema mode

严格遵循 JSON Schema,即模型恰好填充指定的字段。GPT-5.x 及兼容模型支持此功能。

curl https://getvibeapi.com/v1/chat/completions \
  -H "Authorization: Bearer $GETVIBE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-6.1-sol",
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "review",
        "strict": true,
        "schema": {
          "type": "object",
          "properties": {
            "name": { "type": "string" },
            "score": { "type": "number", "minimum": 1, "maximum": 10 },
            "summary": { "type": "string" }
          },
          "required": ["name", "score", "summary"],
          "additionalProperties": false
        }
      }
    },
    "messages": [
      {"role": "user", "content": "Rate this product: fast delivery, careful packaging"}
    ]
  }'
使用 strict: true 后,网关只会将请求路由到实际支持该 schema 的模型变体,并且每次尝试都会如此,包括切换到备用供应商。如果当前没有合适的模型变体可用,您会收到明确的 structured_output_unsupported 错误,而不是在请求 JSON 时得到纯文本。此保护机制基于测量结果构建,仅涵盖我们测量过的模型变体:对于尚未测量的模型变体,供应商仍可能静默丢弃 schema。
不同模型保持 JSON 的能力不同,请检查你使用的模型。于 2026-09-12 在 deepseek-v4-flash-0731 上测量,每个单元格调用 10 次:json_object 在 :cxa 和 :cxbc 变体上均解析成功 10/10 次,在 :network 上解析成功 8/10 次;而严格的 json_schema 在两个变体上均解析成功 0/10 次(:cxa 的响应为 400,:cxbc 的响应为 200 且内容为纯文本),在 :network 上解析成功 3/10 次。DeepSeek 自有 API 没有 json_schema 模式,请使用 json_object;当你需要严格固定为一种结构时,强制函数调用(tools + tool_choice)在每个变体上都返回了可解析参数,成功率为 10/10。每个变体的数值位于 GET /v1/model-capabilities 的 structuredOutput 字段中。每个数值都是带有日期标记(measurement: "point-in-time")的一次性测量结果,并非承诺:变体背后的供应商可能发生变化,我们也不会按固定计划重新测量。

响应示例

{
  "id": "chatcmpl-...",
  "choices": [{
    "message": {
      "role": "assistant",
      "content": "{\"name\":\"product X\",\"score\":9,\"summary\":\"Fast and convenient\"}"
    },
    "finish_reason": "stop"
  }],
  "usage": { "prompt_tokens": 42, "completion_tokens": 18, "total_tokens": 60 }
}
GetVibe API 结构化输出 — JSON Schema、json_object、Python | GetVibe API