Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Openai Openai python Shared Response Format JSON Schema

From Leeroopedia
Knowledge Sources
Domains API_Types, Python
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for defining a JSON Schema response format for structured outputs provided by the openai-python SDK.

Description

ResponseFormatJSONSchema is a Pydantic BaseModel used to configure structured JSON response generation. It contains two fields: json_schema (a nested JSONSchema model with the schema configuration) and type (always "json_schema"). The nested JSONSchema model includes:

  • name - Required name for the response format (alphanumeric with underscores/dashes, max 64 chars).
  • description - Optional description used by the model to determine how to respond.
  • schema_ - Optional JSON Schema object (aliased from "schema" in the API) defining the structure.
  • strict - Optional boolean for strict schema adherence using Structured Outputs.

Usage

Import ResponseFormatJSONSchema when configuring chat completions or responses API calls to return structured JSON output conforming to a specific schema.

Code Reference

Source Location

Signature

class JSONSchema(BaseModel):
    name: str
    description: Optional[str] = None
    schema_: Optional[Dict[str, object]] = FieldInfo(alias="schema", default=None)
    strict: Optional[bool] = None

class ResponseFormatJSONSchema(BaseModel):
    json_schema: JSONSchema
    type: Literal["json_schema"]

Import

from openai.types.shared 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. Must be a-z, A-Z, 0-9, underscores or dashes, max 64 characters.
description Optional[str] No Description of the response format, used by the model to determine how to respond.
schema_ Optional[Dict[str, object]] No The schema described as a JSON Schema object (aliased from "schema" in the API).
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

client = OpenAI()

# Use JSON Schema response format for structured output
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "List 3 colors with hex codes"}],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "color_list",
            "description": "A list of colors with hex codes",
            "schema": {
                "type": "object",
                "properties": {
                    "colors": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "name": {"type": "string"},
                                "hex": {"type": "string"},
                            },
                            "required": ["name", "hex"],
                        },
                    },
                },
                "required": ["colors"],
            },
            "strict": True,
        },
    },
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment