Extracting data from documents, classifying requests, and building RAG pipelines all share the same headache: invalid JSON output. The model may forget to close a bracket, mix up a field type, or add an extra key. In production with 10,000+ requests per day, the fraction of invalid responses can reach 15–20%. Each such response triggers a retry, burning extra tokens and time. When parsing 50,000 invoices daily, retries cost approximately $2,500 per month. OpenAI Structured Outputs solves this at generation time: constrained decoding guarantees every token respects a given JSON schema. We've implemented this approach in several large projects—here are the technical details, including Pydantic model configuration and batch processing.
Problems We Solve
Without Structured Outputs, developers spend up to 40% of their time on retries and response validation. Typical scenarios:
- Extracting invoice details: the model returns
total_amount: "12 345.67"(string) while the schema expects a float, or forgets thevat_amountfield. - Ticket classification: instead of
priority: "high"it producespriority: "High"(wrong case)—doesn't match the enum. - Batch processing: 1000 documents, 20% invalid responses—manual correction.
Structured Outputs eliminates these problems: the response always conforms to the schema. We use it for invoice parsing, ticket classification, and automatic CRM population.
Why Structured Outputs Is More Than Just json_object?
The standard response_format: json_object only asks the model to output JSON but does not enforce a schema. Structured Outputs uses constrained decoding—at each generation step, only tokens that lead to valid JSON per the specified schema are allowed. This yields a 99.5%+ first-attempt success rate in our projects, compared to 80–85% with json_object.
How Pydantic Helps in Python Projects
from openai import OpenAI
from pydantic import BaseModel
from typing import Literal, Optional
client = OpenAI()
class Invoice(BaseModel):
vendor_name: str
invoice_number: str
date: str
total_amount: float
currency: str
line_items: list["InvoiceItem"]
vat_amount: Optional[float] = None
class InvoiceItem(BaseModel):
description: str
quantity: float
unit_price: float
total: float
Invoice.model_rebuild()
def extract_invoice(text: str) -> Invoice:
response = client.beta.chat.completions.parse(
model="gpt-4o",
messages=[
{"role": "system", "content": "Extract invoice data from the text"},
{"role": "user", "content": text}
],
response_format=Invoice,
)
return response.choices[0].message.parsed
Pydantic automatically validates types on the client, but with Structured Outputs this is redundant: the model already returned correct data. Nevertheless, we keep validation to log mismatches (e.g., unparseable dates).
How to Implement for Batch Processing
In one project we processed 50,000+ documents per day. We used:
- OpenAI GPT-4o (primary model)
-
gpt-4o-minifor pre‑classification (cheaper, p99 latency < 2s) - Pydantic v2 + LangChain for prompt management
- ChromaDB for semantic search over extracted data
Key finding: Structured Outputs reduced API calls by 25% due to zero retries. In batch processing we achieved 99.7% correct extractions on the first attempt.
Classification with Enum
from enum import Enum
class TicketCategory(str, Enum):
technical = "technical"
billing = "billing"
feature_request = "feature_request"
complaint = "complaint"
general = "general"
class TicketClassification(BaseModel):
category: TicketCategory
priority: Literal["low", "medium", "high", "critical"]
sentiment: Literal["positive", "neutral", "negative", "angry"]
requires_human: bool
summary: str
tags: list[str]
def classify_ticket(text: str) -> TicketClassification:
response = client.beta.chat.completions.parse(
model="gpt-4o-mini",
messages=[{"role": "user", "content": f"Classify this ticket: {text}"}],
response_format=TicketClassification,
temperature=0,
)
return response.choices[0].message.parsed
Structured Outputs via JSON Schema (Without Pydantic)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Product data"}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "product_data",
"strict": True,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"price": {"type": "number"},
"in_stock": {"type": "boolean"},
"categories": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["name", "price", "in_stock", "categories"],
"additionalProperties": False,
}
}
}
)
import json
data = json.loads(response.choices[0].message.content)
What Limitations Must Be Considered?
-
strict: TruerequiresadditionalProperties: Falseat all levels. - Nullable fields via
"type": ["string", "null"]are not supported—useanyOf. - Maximum nesting depth: 5 levels.
- For recursive schemas, use
$ref.
When to Choose Structured Outputs vs json_object?
| Scenario | Method |
|---|---|
| Extracting data from documents | Structured Outputs |
| Classification | Structured Outputs |
| Responses with predictable structure | Structured Outputs |
| Free-form JSON (unknown structure) | json_object mode |
| Simple answers | Plain text |
Comparison of the two approaches: Structured Outputs wins 3–5× in schema accuracy on test samples, but adds ~15% to generation time. For realtime scenarios (chatbots), use gpt-4o-mini—it's fast and cheap.
Latency and Accuracy Comparison
| Model | Average latency | Valid schema rate |
|---|---|---|
| gpt-4o + json_object | 1.2s | 85% |
| gpt-4o + Structured Outputs | 1.5s | 99.5% |
| gpt-4o-mini + json_object | 0.4s | 78% |
| gpt-4o-mini + Structured Outputs | 0.5s | 98% |
Process and Timelines
- Analysis: measure current parsing errors, identify document types (invoices, waybills, tickets).
- Schema design: create Pydantic models, test on sample data.
- Implementation: integrate Structured Outputs, handle edge cases, add logging.
- Testing: A/B test comparing extraction quality before/after.
- Deployment: containerization, monitor latency and valid response rate.
Estimated timelines:
- Basic integration with one schema: 1–2 days.
- Complex pipeline with multiple document types and RAG: 1–2 weeks.
Get a free engineer consultation—discuss your use case. Request Structured Outputs integration.
Common Implementation Mistakes
- Omitting
additionalProperties: False—the model adds extra keys. - Using
temperature > 0.3—increases risk of deviating from the schema. - Ignoring logging—without monitoring, rare failures go undetected.
- Attempting to parse nested objects deeper than 5 levels—API limitation.
These mistakes reduce the rate of correct responses by 10–30%. Our engineers know how to avoid them. Contact us for a project assessment.







