Skip to content

Instructor

Instructor gives you validated, structured outputs from LLMs by patching an underlying client (such as OpenAI) and adding a response_model. Because it wraps a normal client, the simplest way to send data to Logfire is to instrument that client directly — which is exactly what Instructor’s own docs recommend.

Installation

Terminal
pip install logfire instructor openai

Usage

Call logfire.instrument_openai() to trace the LLM requests, and optionally logfire.instrument_pydantic() to record validation of the response_model:

import instructor
from openai import OpenAI
from pydantic import BaseModel

import logfire

logfire.configure()
logfire.instrument_openai()  # trace the OpenAI client that Instructor wraps
logfire.instrument_pydantic()  # optional: record response_model validation

client = instructor.from_openai(OpenAI())


class UserInfo(BaseModel):
    name: str
    age: int


user = client.chat.completions.create(
    model='gpt-4o-mini',
    response_model=UserInfo,
    messages=[{'role': 'user', 'content': 'John Doe is 30 years old.'}],
)
print(user)
#> name='John Doe' age=30

You’ll see the LLM conversation in the Logfire UI along with the structured output and, if you enabled it, the Pydantic validation of the UserInfo model.

Managed prompts

Keep your prompts in Prompt Management and fetch them at runtime:

Terminal
pip install 'logfire[variables]'
import instructor
from openai import OpenAI
from pydantic import BaseModel

import logfire

logfire.configure()
logfire.instrument_openai()


class ExtractInputs(BaseModel):
    text: str


prompt_var = logfire.template_var(
    name='prompt__extract_user',
    type=str,
    default='Extract the user info from: {{text}}',
    inputs_type=ExtractInputs,
)

with prompt_var.get(ExtractInputs(text='John Doe is 30 years old.'), label='production') as resolved:
    content = resolved.value


class UserInfo(BaseModel):
    name: str
    age: int


client = instructor.from_openai(OpenAI())
user = client.chat.completions.create(
    model='gpt-4o-mini',
    response_model=UserInfo,
    messages=[{'role': 'user', 'content': content}],
)
print(user)

See Use Prompts in Your Application for the full workflow.