结构化输出

最后更新

需要模型返回机器可读的 JSON 时,有两种约束强度:

模式 OpenAI Chat OpenAI Responses Gemini 报价需要的能力
JSON 模式:保证输出是合法 JSON "response_format": {"type": "json_object"} "text": {"format": {"type": "json_object"}} generationConfig.responseMimeType 为 application/json json_mode
结构化输出:按给定 JSON Schema 输出 "response_format": {"type": "json_schema", "json_schema": {...}} "text": {"format": {"type": "json_schema", ...}} generationConfig.responseSchema 或 responseJsonSchema structured_output
curl https://modelportal.ai/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "MODEL_ID",
    "messages": [{"role": "user", "content": "列出三种水果和它们的颜色"}],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "fruits",
        "schema": {
          "type": "object",
          "properties": {"items": {"type": "array", "items": {"type": "object", "properties": {"name": {"type": "string"}, "color": {"type": "string"}}, "required": ["name", "color"]}}},
          "required": ["items"]
        }
      }
    }
  }'

请求用到哪种模式,就只路由到声明了对应能力的报价;一个都没有时返回 400 unsupported_parameter,不会降级成普通文本输出。Anthropic 协议没有单独的 JSON 模式参数,可以用工具调用约束输出格式。