Principle:Openai Openai python Structured Output Definition
| Knowledge Sources | |
|---|---|
| Domains | NLP, Data_Extraction |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
A schema definition pattern that converts typed data models into JSON schemas for constraining language model outputs and defining tool interfaces.
Description
Structured output definition bridges typed Python code and the OpenAI API's schema-based features. By defining Pydantic models, developers specify the exact shape of data they want the model to produce. The SDK automatically converts these models to strict JSON schemas compatible with the API's structured output and tool calling features.
This enables two key capabilities: (1) response format constraints that guarantee the model's output matches a schema, and (2) tool definitions that describe function signatures the model can invoke.
Usage
Use this principle when you need the model to produce structured data (JSON matching a schema) rather than free-form text, or when defining tools for function calling. Define a Pydantic model representing the desired output structure and pass it as response_format or convert it to a tool definition.
Theoretical Basis
The pattern follows a Schema Derivation approach:
# 1. Define typed structure
class Output(BaseModel):
field_a: str
field_b: int
# 2. SDK converts to JSON Schema
schema = to_strict_json_schema(Output)
# {"type": "object", "properties": {"field_a": {"type": "string"}, ...}, "required": [...], "additionalProperties": false}
# 3. Schema sent to API constrains generation
# 4. Response parsed back into Output instance
The strict mode ensures all properties are required and no additional properties are allowed, guaranteeing exact schema conformance.