EsempioscriptintermediateEseguibileschema-validation
Validazione dell'Output
Esempio rieseguibile intermediate di tipo script che usa instructor, openai.
Fatti chiave
- Livello
- intermediate
- Runtime
- Python • API OpenAI
- Pattern
- Flusso ispezionabile con confini di sistema visibili
- Interazione
- Sandbox live • Script
- Aggiornato
- 14 marzo 2026
Naviga questo esempio
Libreria
Sfoglia gli esempiRiapri la libreria completa per confrontare pattern vicini e percorsi collegati.Interazione
Esegui ora nel sandboxProva l'interazione direttamente nella superficie guidata di questo esempio.Sorgente
Apri codice completoLeggi l'implementazione reale, i punti evidenziati e i requisiti runtime.MCP
Chiama via MCPUsa la stessa risorsa dentro agenti, export deterministici e setup MCP.
Contesto del modello
Model-agnosticoEseguibile in localeTool calling wrapped accettabileRagionamento sempliceL'orchestrazione compensa
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},
],
)