Example
instructor — structured output from an LLM

A real, runnable mini-project. Download it and run with Docker.

June 28, 2026

instructor — structured output from an LLM

A tiny instructor script: it extracts a typed Person (name, age, occupation) from free text. You define a Pydantic model and get a validated object back — not a string to parse. from_litellm routes the call, so MODEL picks the provider.

Configure

cd samples/instructor_1
cp .env.sample .env
# edit .env: set MODEL and the matching provider key

MODEL is a LiteLLM-style name:

Provider MODEL example Key in .env
Anthropic Claude anthropic/claude-opus-4-8 ANTHROPIC_API_KEY
OpenAI openai/gpt-4o OPENAI_API_KEY
Google AI Studio gemini/gemini-2.5-flash GEMINI_API_KEY

.env is gitignored — only .env.sample is committed.

Run with Docker

cd samples/instructor_1
docker build -t aas-instructor .
docker run --rm --env-file .env aas-instructor \
  "Ada Lovelace, 36, was a mathematician and the first programmer."

Run with Docker (in a devcontainer with DooD)

In a dev container that talks to the host Docker daemon (Docker-outside-of-Docker), the foreground docker run above often prints nothing and exits 0 — but the run itself succeeds. The script runs to completion and Docker captures all of its output; only the live attached stream drops it over the VM boundary. Run detached and follow the logs instead:

cd samples/instructor_1
docker build -t aas-instructor .
docker logs -f "$(docker run -d --env-file .env aas-instructor \
  "Ada Lovelace, 36, was a mathematician and the first programmer.")"

Run locally

cd samples/instructor_1
pip install -r requirements.txt
python app.py "Ada Lovelace, 36, was a mathematician and the first programmer."

python-dotenv loads .env automatically.


Example run

Output varies by model and run — LLMs are non-deterministic. The shape is fixed by the Pydantic model; the values are extracted. Below is one run with anthropic/claude-opus-4-8.

{'name': 'Ada Lovelace', 'age': 36, 'occupation': 'Mathematician'}

Files

.env.sample
# Copy this file to `.env` and fill in the key for the provider you want.
#   cp .env.sample .env
#
# instructor's from_provider routes via LiteLLM, so MODEL picks the provider.

# Pick the model. Examples:
#   anthropic/claude-opus-4-8    Anthropic Claude   -> needs ANTHROPIC_API_KEY
#   openai/gpt-4o                OpenAI             -> needs OPENAI_API_KEY
#   gemini/gemini-2.5-flash      Google AI Studio   -> needs GEMINI_API_KEY
MODEL=anthropic/claude-opus-4-8

# Set only the key for the provider you chose; leave the rest blank.
ANTHROPIC_API_KEY=
OPENAI_API_KEY=
GEMINI_API_KEY=