Skip to main content
Structured outputs ensure your application receives well-defined, machine-readable responses every time. By enforcing formats such as JSON, CSV, or custom Pydantic models, you gain:

1. Structured Outputs with Pydantic

Define a Pydantic model for your desired schema and instruct the API to parse directly into that model.
Make sure you’re using the openai Python package version that supports .parse() (e.g., v0.27+).
See Pydantic documentation for detailed model usage.

2. Step-by-Step Reasoning as Structured Output

You can capture a chain of thought or intermediate steps by defining a Pydantic model that holds your reasoning.

3. Building a Recipe Generator with JSON Output

Prompt the model to output strict JSON so downstream services can ingest it without additional parsing.
Run the script:
Example JSON response:
Never commit your API keys to public repositories. Use environment variables or a secure vault.

4. Switching to CSV Output

Simply update the system prompt to request CSV, then process the comma-separated response.
Sample CSV output:
You can even mix formats—return cooking steps as CSV:

By specifying your desired output format in the system and assistant messages, you guarantee consistency and simplify downstream processing. For more advanced examples, see the OpenAI Structured Outputs Guide.

Watch Video