Instructor

Trace Instructor structured LLM outputs with Respan.
  1. Sign up — Create an account at platform.respan.ai
  2. Create an API key — Generate one on the API keys page
  3. Add credits or a provider key — Add credits on the Credits page or connect your own provider key on the Integrations page

Add the Docs MCP to your AI coding tool to get help building with Respan. No API key needed.

1{
2 "mcpServers": {
3 "respan-docs": {
4 "url": "https://mcp.respan.ai/mcp/docs"
5 }
6 }
7}

What is Instructor?

Instructor is a Python library for getting structured data (JSON) from LLMs using Pydantic models. It patches OpenAI and other LLM clients to return validated, typed responses. The Respan integration uses respan-tracing auto-instrumentation to capture all LLM calls.

Example project: GitHub Link

Setup

1

Install packages

$pip install instructor openai respan-tracing
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
3

Initialize and run

1from respan_tracing import RespanTelemetry, workflow, task, Instruments
2from openai import OpenAI
3import instructor
4from pydantic import BaseModel
5
6# Initialize Respan — auto-instruments OpenAI calls
7telemetry = RespanTelemetry(instruments={Instruments.OPENAI})
8
9# Patch OpenAI with Instructor
10client = instructor.from_openai(OpenAI())
11
12class User(BaseModel):
13 name: str
14 age: int
15
16@task(name="extract_user")
17def extract_user(text: str) -> User:
18 return client.chat.completions.create(
19 model="gpt-4o-mini",
20 response_model=User,
21 messages=[{"role": "user", "content": f"Extract user info: {text}"}],
22 )
23
24@workflow(name="extraction_pipeline")
25def extraction_pipeline():
26 user = extract_user("John is 30 years old")
27 print(f"Name: {user.name}, Age: {user.age}")
28 return user
29
30extraction_pipeline()
4

View your trace

Open the Traces page to see your extraction traces.

Configuration

Instructor uses the respan-tracing SDK. Instruments.OPENAI auto-captures all OpenAI calls made by Instructor.

See the Python Tracing SDK reference for configuration options.