Reference · API

OpenAI structured outputs

Last updated

Return **JSON matching a schema** — reliable parsing for agents and APIs.

Return **JSON matching a schema** — reliable parsing for agents and APIs.

Endpoint

POST https://api.openai.com/v1/chat/completions

Request

{
  "model": "gpt-4o-mini",
  "messages": [{ "role": "user", "content": "Extract order fields from: Order #8821, $42, shipped" }],
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "order",
      "schema": {
        "type": "object",
        "properties": {
          "order_id": { "type": "string" },
          "amount_usd": { "type": "number" },
          "status": { "type": "string" }
        },
        "required": ["order_id", "amount_usd", "status"],
        "additionalProperties": false
      },
      "strict": true
    }
  }
}

Tips

  • Validate parsed JSON before side effects
  • Pair with function calling when you need tools **and** structured final answers
  • Fall back to retry on schema validation errors
  • **Try the lesson:** `structured-outputs` in Lane C