結構化輸出實驗場

無需呼叫模型 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:
{
  "priority": "high",
  "owner": {
    "name": "Jane",
    "team": "support"
  },
  "steps": [
    "reply",
    "escalate"
  ]
}
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": {
                "priority": {
                  "type": "string"
                },
                "owner": {
                  "type": "object",
                  "properties": {
                    "name": {
                      "type": "string"
                    },
                    "team": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "name",
                    "team"
                  ],
                  "additionalProperties": false
                },
                "steps": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                }
              },
              "required": [
                "priority",
                "owner",
                "steps"
              ],
              "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:
{
  "priority": "high",
  "owner": {
    "name": "Jane",
    "team": "support"
  },
  "steps": [
    "reply",
    "escalate"
  ]
}