|
|
OpenAI Structured Outputs with Pydantic
Author: Venkata Sudhakar
OpenAI Structured Outputs is a feature that guarantees the model will return a response that exactly matches a JSON schema you define. Unlike prompting the model to "respond in JSON" (which sometimes fails or produces malformed JSON), Structured Outputs constrains the model at the token generation level - it can only emit tokens that are valid according to your schema. This makes it reliable enough to use in production without any error handling for JSON parsing failures. Structured Outputs was introduced in gpt-4o-2024-08-06 and later models. The OpenAI Python SDK integrates directly with Pydantic - you pass your Pydantic model class to client.beta.chat.completions.parse() and the SDK automatically converts it to a JSON schema, sends it to the API, and deserialises the response back into a validated Pydantic model instance. This gives you full type safety end-to-end: the LLM output is automatically a Python object with the correct types, validated by Pydantic, with no manual JSON parsing needed. This is ideal for data extraction, classification, and any task where you need machine-readable structured output from an LLM. The below example shows three practical use cases: extracting migration metadata from unstructured text, classifying support tickets with confidence scores, and extracting multiple entities from a paragraph.
It gives the following output,
Source: Oracle
Target: PostgreSQL
Rows: 50000000
Timeline: 12 weeks
Challenges: ['200+ stored procedures requiring conversion',
'30-minute maximum downtime constraint']
Approach: 'Use AWS DMS with full-load-and-cdc mode for near-zero-downtime migration,
combined with AWS SCT for stored procedure conversion.'
It gives the following output,
Category: MIGRATION | Priority: CRITICAL
Summary: CDC pipeline has stopped syncing orders and customers tables, causing data loss.
Affected tables: ['orders', 'customers']
Sentiment: negative
Action: Escalate to on-call engineer immediately, check Debezium connector status
and Kafka consumer lag for the affected topics.
Category: BILLING | Priority: MEDIUM
Summary: Customer was charged twice for their Pro plan subscription.
Affected tables: []
Sentiment: neutral
Action: Review billing records, issue refund for duplicate charge, send confirmation email.
Structured Outputs vs JSON mode vs function calling: Structured Outputs (response_format=YourPydanticModel) - Guarantees 100% schema compliance. The model cannot deviate from the schema. Use for production data extraction where parsing failures are not acceptable. JSON mode (response_format={"type":"json_object"}) - Guarantees valid JSON but not a specific schema. The model may include or omit fields. Use when you need valid JSON but do not have a strict schema requirement. Function calling / tool calling - The model chooses when to call a function and populates its arguments. Use for agentic workflows where the model needs to decide which action to take, not just extract data from text.
|
|