TOOL WIKI · OUTPUT & VALIDATION

Instructor

Uses typed schemas to work with structured model outputs.

Download example files Code + setup + checks in a ZIP
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

WHAT IT ISStructured-output library
WHERE IT FITSOutput & validation
SEE HOW THE SYSTEM FITS TOGETHERSELECT A COMPONENT
inputprocessresultcheckoutputPrompt + schemaDesired output contractInstructorStructured-output libraryModel generationConfigured provider or modelValidated objectParse or explicit failureBusiness checksTruth + authorizationConsumerTrusted application record
  1. Define required fields and domain constraints before calling the model.

  2. An extraction or generation task that returns application objects. Evaluate factual fidelity in addition to schema conformance.

  3. Generate under the selected library’s supported structured-output mechanism.

  4. Handle parsing errors and bounded retries rather than silently accepting malformed output.

  5. A schema-valid object still needs factual and permission checks.

  6. Save or act only after the application accepts the validated result.

    Result or stopping point
Instructor

An extraction or generation task that returns application objects. Evaluate factual fidelity in addition to schema conformance.

Follow the arrows, then select a component to inspect its responsibility. On mobile, downward arrows connect the next step; bent arrows identify branches and return paths. Select a destination to jump to that component.

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.
Shell
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

Python · 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

Shell
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

Design tools an agent can use wellAI agent evaluation: frameworks, tools and an example

Explore the same layer

PydanticZodOutlines

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.