Structured Output Using Pydantic
In this lesson, you’ll learn how to extract structured data from user input using a Pydantic model with OpenAI’s parse() method.
Prerequisites
Install the required packages:
pip install openai pydantic
The Code
from openai import OpenAI
from pydantic import BaseModel
client = OpenAI()
class ContactInfo(BaseModel):
name: str
email: str
phone: str
response = client.responses.parse(
model="gpt-4o-mini",
input=[
{"role": "system", "content": "Extract contact details from the message."},
{
"role": "user",
"content": "You can reach out to John Doe at john@example.com or call him at +1-555-1234."
},
],
text_format=ContactInfo,
)
contact = response.output_parsed
print(f"Name: {contact.name}")
print(f"Email: {contact.email}")
print(f"Phone: {contact.phone}")
Explanation
BaseModel: Defines the expected output format using Pydantic.client.responses.parse(...): Parses the model output directly into theContactInfoobject.response.output_parsed: Structured data you can use immediately.
Use Case
Best for:
- Information extraction (contacts, product specs, summaries)
- Creating structured datasets from unstructured prompts
- Automating form-filling and data ingestion