Structured Outputs
Structured outputs make the model emit JSON that conforms to a schema you supply — no parsing failures, no missing fields.
JSON mode
Section titled “JSON mode”The simplest form. Set response_format to json_object:
{ "model": "...", "messages": [...], "response_format": { "type": "json_object" }}The model is guaranteed to return syntactically valid JSON. Shape is up to your prompt.
Strict JSON Schema
Section titled “Strict JSON Schema”For guaranteed shape, pass a schema:
response = client.chat.completions.create( model="...", messages=[{"role": "user", "content": "Extract: Jane Doe, jane@example.com"}], response_format={ "type": "json_schema", "json_schema": { "name": "contact", "strict": True, "schema": { "type": "object", "properties": { "name": {"type": "string"}, "email": {"type": "string"}, }, "required": ["name", "email"], "additionalProperties": False, }, }, },)When strict: true, output is constrained to match the schema exactly — no extra fields, no missing required keys.
With the OpenAI Python SDK
Section titled “With the OpenAI Python SDK”The SDK can derive the schema from a Pydantic model via client.chat.completions.parse() (or responses.parse()).