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
See Pydantic documentation for detailed model usage.
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.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.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.