When to use structured outputs
Use structured outputs when you need the language model’s response to follow a specific schema. This is ideal for:- Generating structured data for databases
- Creating consistent API responses
- Enforcing a predefined output format
- Structured outputs can be specified as either a JSON object or a Pydantic model.
Using JSON object as response format
To request a JSON object, passresponse_format={"type": "json_object"}
to the create
method. The response will be a valid JSON string.
json.loads()
in Python or JSON.parse()
in TypeScript.
Using Pydantic model as response format
For more complex structures, define a Pydantic model and pass it as theresponse_format
. The response will be an instance of that model.
EventsList
instance, allowing direct access to its attributes.
Note: Pydantic models are Python-specific. In TypeScript, use JSON schema or other validation tools.