ExamplescriptintermediateRunnableschema-validation
Ticket System
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.
04-ticket-system.py
python
import instructor
from pydantic import BaseModel, Field
from openai import OpenAI
from enum import Enum
# --------------------------------------------------------------
# Ticket System Example with Structured Output
# --------------------------------------------------------------
# Patch the OpenAI client
client = instructor.from_openai(OpenAI())
class TicketCategory(str, Enum):
"""Enumeration of categories for incoming tickets."""
GENERAL = "general"
ORDER = "order"
BILLING = "billing"
class CustomerSentiment(str, Enum):
"""Enumeration of customer sentiment labels."""
NEGATIVE = "negative"
NEUTRAL = "neutral"
POSITIVE = "positive"
class Ticket(BaseModel):
reply: str = Field(description="Your reply that we send to the customer.")
category: TicketCategory
confidence: float = Field(ge=0, le=1)
sentiment: CustomerSentiment
def process_ticket(customer_message: str) -> Ticket:
reply = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=Ticket,
max_retries=3,
messages=[
{
"role": "system",
"content": "Analyze the incoming customer message and predict the values for the ticket.",
},
{"role": "user", "content": customer_message},
],
)
return reply
# --------------------------------------------------------------
# Billing Issue Example
# --------------------------------------------------------------
ticket = process_ticket("Hi there, I have a question about my bill. Can you help me?")
assert ticket.category == TicketCategory.BILLING
ticket.reply
ticket.category
ticket.confidence
ticket.sentiment
# --------------------------------------------------------------
# Order-Related Example
# --------------------------------------------------------------
ticket = process_ticket("I would like to place an order.")
assert ticket.category == TicketCategory.ORDER