结构化输出实验场

无需调用模型 API,即可验证受模式约束的 JSON 示例并复制各提供商的请求模式。

结构化输出工作台
示例预设
验证结果
示例输出符合目标结构。
提供商请求模式
跨提供商通用提示词
将目标 JSON 结构视为契约,并拒绝额外字段。
Return JSON only.
Use the target JSON structure as the contract for keys, nesting, arrays, and value types.
Do not wrap the result in markdown.
Do not add extra keys.

Target structure:
{
  "title": "Launch summary",
  "score": 10,
  "tags": [
    "alpha"
  ],
  "meta": {
    "published": true
  }
}
OpenAI Responses API
使用 text.format 和严格的 JSON schema 来请求直接的结构化输出。
from openai import OpenAI

client = OpenAI()

response = client.responses.create(
    model="gpt-5",
    input="Return the response in the required JSON shape.",
    text={
        "format": {
            "type": "json_schema",
            "name": "structured_output",
            "strict": True,
            "schema": {
              "type": "object",
              "properties": {
                "title": {
                  "type": "string"
                },
                "score": {
                  "type": "number"
                },
                "tags": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                },
                "meta": {
                  "type": "object",
                  "properties": {
                    "published": {
                      "type": "boolean"
                    }
                  },
                  "required": [
                    "published"
                  ],
                  "additionalProperties": false
                }
              },
              "required": [
                "title",
                "score",
                "tags",
                "meta"
              ],
              "additionalProperties": false
            },
        }
    },
)

print(response.output_text)
Anthropic 提示词模式
原样粘贴目标结构,并要求模型仅输出 JSON。
You are returning structured data for a downstream parser.
Output JSON only.
Match the exact keys, nesting, and value types shown below.
If a field is unknown, return a valid placeholder of the same type.

Target structure:
{
  "title": "Launch summary",
  "score": 10,
  "tags": [
    "alpha"
  ],
  "meta": {
    "published": true
  }
}