POST
/
v1
/
chat
/
completions
Chat Completions Structured Output
curl --request POST \
  --url https://api.example.com/v1/chat/completions \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "<string>",
  "messages": [
    {}
  ],
  "response_format": {},
  "temperature": 123,
  "max_tokens": 123
}
'
Generate responses in a specific JSON schema format using structured outputs.

Overview

Structured outputs allow you to ensure the model’s response adheres to a specific JSON schema. This is useful when you need predictable, parseable output for downstream processing.

Authentication

All requests require a Bearer token in the Authorization header:
Authorization: Bearer YOUR_API_KEY

Request Parameters

model
string
required
ID of the model to use. Example: gpt-4o, gpt-4o-mini
messages
array
required
A list of messages comprising the conversation so far.
response_format
object
required
Specify the output format. Use json_schema type for structured outputs.
temperature
number
default:"1"
Sampling temperature between 0 and 2.
max_tokens
integer
Maximum number of tokens to generate.

Request Example

curl -X POST https://api.example.com/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "system", "content": "Extract user information from the text."},
      {"role": "user", "content": "My name is John Doe, I am 30 years old and live in New York."}
    ],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "user_info",
        "strict": true,
        "schema": {
          "type": "object",
          "properties": {
            "name": {"type": "string"},
            "age": {"type": "integer"},
            "city": {"type": "string"}
          },
          "required": ["name", "age", "city"],
          "additionalProperties": false
        }
      }
    }
  }'

Response Example

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "{\"name\": \"John Doe\", \"age\": 30, \"city\": \"New York\"}"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 45,
    "completion_tokens": 20,
    "total_tokens": 65
  }
}

Response Format Options

TypeDescription
json_objectSimple JSON mode, model outputs valid JSON
json_schemaStructured output with strict schema validation

Available Models

  • gpt-4o
  • gpt-4o-mini
  • gpt-4-turbo