Implementation:Openai Openai python Shared Params Response Format JSON Schema
| Knowledge Sources | |
|---|---|
| Domains | API_Types, Python |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for specifying a JSON Schema response format for Structured Outputs in the openai-python SDK.
Description
ResponseFormatJSONSchema is a TypedDict that configures Structured Outputs using a JSON Schema. It contains two required fields: json_schema (a nested JSONSchema TypedDict with the schema configuration) and type (always "json_schema"). The nested JSONSchema TypedDict requires a name and optionally accepts a description, schema (the actual JSON Schema object), and strict (whether to enforce strict schema adherence). When strict is true, the model will always follow the exact schema, though only a subset of JSON Schema is supported.
Usage
Import this type when you need to configure Structured Outputs for chat completions or Responses API calls, ensuring the model returns responses conforming to a specific JSON Schema.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/types/shared_params/response_format_json_schema.py
Signature
class JSONSchema(TypedDict, total=False):
"""Structured Outputs configuration options, including a JSON Schema."""
name: Required[str]
description: str
schema: Dict[str, object]
strict: Optional[bool]
class ResponseFormatJSONSchema(TypedDict, total=False):
"""JSON Schema response format."""
json_schema: Required[JSONSchema]
type: Required[Literal["json_schema"]]
Import
from openai.types.shared_params import ResponseFormatJSONSchema
I/O Contract
Fields (ResponseFormatJSONSchema)
| Name | Type | Required | Description |
|---|---|---|---|
| json_schema | JSONSchema | Yes | Structured Outputs configuration options, including the JSON Schema. |
| type | Literal["json_schema"] | Yes | The type of response format. Always "json_schema". |
Fields (JSONSchema)
| Name | Type | Required | Description |
|---|---|---|---|
| name | str | Yes | The name of the response format (a-z, A-Z, 0-9, underscores, dashes; max 64 chars). |
| description | str | No | A description of what the response format is for, used by the model. |
| schema | Dict[str, object] | No | The schema described as a JSON Schema object. |
| strict | Optional[bool] | No | Whether to enable strict schema adherence. Only a subset of JSON Schema is supported when true. |
Usage Examples
from openai import OpenAI
from openai.types.shared_params import ResponseFormatJSONSchema
client = OpenAI()
response_format: ResponseFormatJSONSchema = {
"type": "json_schema",
"json_schema": {
"name": "weather_response",
"description": "A structured weather report",
"schema": {
"type": "object",
"properties": {
"temperature": {"type": "number"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
"description": {"type": "string"},
},
"required": ["temperature", "unit", "description"],
},
"strict": True,
},
}
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What is the weather in Paris?"}],
response_format=response_format,
)