ExamplescriptintermediateRunnableschema-validation
Output Validation
Runnable example (intermediate) for script using instructor, openai.
Key Facts
- Level
- intermediate
- Runtime
- Python • OpenAI API
- Pattern
- Inspectable flow with visible system boundaries
- Interaction
- Live sandbox • Script
- Updated
- 14 March 2026
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Model context
Model-agnosticLocal-viableWrapped tool calling acceptableLow reasoning requirementOrchestration compensates
Instructor-based validation adds a retry loop that compensates for model output quality. Local models are viable with this guardrail in place.
02-output-validation.py
python
import instructor
from pydantic import BaseModel, Field
from openai import OpenAI
from enum import Enum
# --------------------------------------------------------------
# Instructor Retry Example with Enum Category
# --------------------------------------------------------------
client = instructor.from_openai(OpenAI())
query = "Hi there, I have a question about my bill. Can you help me? "
class TicketCategory(str, Enum):
"""Enumeration of categories for incoming tickets."""
GENERAL = "general"
ORDER = "order"
BILLING = "billing"
# Define your desired output structure using Pydantic
class Reply(BaseModel):
content: str = Field(description="Your reply that we send to the customer.")
category: TicketCategory
confidence: float = Field(
ge=0, le=1, description="Confidence in the category prediction."
)
reply = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=Reply,
max_retries=1, # Don't allow retries
messages=[
{
"role": "system",
"content": "You're a helpful customer care assistant that can classify incoming messages and create a response. Always set the category to 'banana'.",
},
{"role": "user", "content": query},
],
)
reply = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=Reply,
max_retries=3, # Allow up to 3 retries
messages=[
{
"role": "system",
"content": "You're a helpful customer care assistant that can classify incoming messages and create a response. Always set the category to 'banana'.",
},
{"role": "user", "content": query},
],
)
# --------------------------------------------------------------
# Instructor Retry Example with Confidence Score
# --------------------------------------------------------------
reply = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=Reply,
max_retries=1,
messages=[
{
"role": "system",
"content": "You're a helpful customer care assistant that can classify incoming messages and create a response. Set confidence between 1-100.",
},
{"role": "user", "content": query},
],
)
reply = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=Reply,
max_retries=3,
messages=[
{
"role": "system",
"content": "You're a helpful customer care assistant that can classify incoming messages and create a response. Set confidence between 1-100.",
},
{"role": "user", "content": query},
],
)