TOOL WIKI · OUTPUT & VALIDATION
Instructor
Uses typed schemas to work with structured model outputs.
Jump to a section 10 sections
EXAMPLE USE CASE
Extract a typed status from an explicit source statement
An application needs a checked output contract before model-generated values can influence a business operation.
- Input
- Text saying order A104 is packed and an OrderResult response model.
- Expected output
- A validated order_id/status object produced by the configured provider.
- Success check: The returned object validates against OrderResult and preserves the fixture’s packed status.
- Try a failure: Missing facts must not be replaced by a plausible enum value just to satisfy the schema.
Example scope: Actual structured-generation API call with bounded retries; factual/source checks remain necessary.
Official reference · Instructor
Define required fields and domain constraints before calling the model.
inputTo 02 · InstructorAn extraction or generation task that returns application objects. Evaluate factual fidelity in addition to schema conformance.
processTo 03 · Model generationGenerate under the selected library’s supported structured-output mechanism.
resultTo 04 · Validated objectHandle parsing errors and bounded retries rather than silently accepting malformed output.
A schema-valid object still needs factual and permission checks.
outputTo 06 · ConsumerSave or act only after the application accepts the validated result.
Result or stopping point
An extraction or generation task that returns application objects. Evaluate factual fidelity in addition to schema conformance.
When to consider it
- An extraction or generation task that returns application objects.
A design decision to make
- Evaluate factual fidelity in addition to schema conformance.
Prerequisites and setup
- An OpenAI API key/model available to your project. Running extraction makes provider calls.
python3 -m venv .venv
source .venv/bin/activate
pip install instructor
export OPENAI_API_KEY="YOUR_KEY"
export OPENAI_MODEL="YOUR_ENABLED_MODEL"Try a small integration
extract.py
import os
from typing import Literal
import instructor
from pydantic import BaseModel
class OrderResult(BaseModel):
order_id: str
status: Literal["packed","shipped","not_found"]
client = instructor.from_provider("openai/"+os.environ["OPENAI_MODEL"])
result = client.create(response_model=OrderResult,max_retries=2,
messages=[{"role":"user","content":"Extract only this fixture: order A104 is packed. Do not infer a delivery date."}])
print(result.model_dump())Run and check the result
python extract.py- The returned object validates against OrderResult and preserves the fixture’s packed status.
- Validation retries consume additional usage; inspect the retry count and terminal error.
Common failures
- A healthy HTTP server does not prove the model loaded or a request completed. Check model readiness and an actual response separately.
- Model identifiers, context length and supported parameters differ by backend; do not assume an OpenAI-compatible endpoint supports every API feature.
Production decisions
- A valid schema proves structure and selected constraints; it does not prove factual truth, user permission or safe business intent.
- Handle invalid, refused, truncated and unavailable results before acting. Keep resource authorization and evidence checks at the tool/service boundary.
Keep exploring
Explore the same layer
Compare candidates on your own workload. This entry describes a product’s role; it is not a claim that products in the category are interchangeable.