Skip to main content
Chat Completions API exposes structured outputs through response_format. Use this API when you need the OpenAI chat protocol rather than the newer Responses shape. Use json_schema when the output must match a known shape. Use json_object when valid JSON is enough.

Request

from openai import OpenAI

client = OpenAI(
    base_url="https://api.naga.ac/v1",
    api_key="YOUR_API_KEY",
)

completion = client.chat.completions.create(
    model="gpt-4.1",
    messages=[
        {
            "role": "user",
            "content": "Extract the event information.",
        }
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "event",
            "schema": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "date": {"type": "string"},
                },
                "required": ["name", "date"],
                "additionalProperties": False,
            },
            "strict": True,
        },
    },
)

print(completion.choices[0].message.content)

Wire Response Shape

Raw HTTP responses keep the result in choices[0].message.content as JSON text.
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "{\"name\":\"Science Fair\",\"date\":\"Friday\"}"
      },
      "finish_reason": "stop"
    }
  ]
}

JSON Mode

If you only need valid JSON and not a full schema, use:
{
  "response_format": {
    "type": "json_object"
  }
}

Which mode should you use?

ModeUse it when
json_schemarequired fields and schema validation matter
json_objectyou only need valid JSON text

Common mistakes

  • expecting a native JSON object instead of parsing choices[0].message.content
  • choosing Chat Completions for new work when Responses structured outputs would be the cleaner default
  • using an open schema when downstream code expects strict shape guarantees