Instructor Integration with Future AGI for Structured LLM

Integrate Instructor with Future AGI observability. Trace structured LLM output extraction and validation automatically using traceAI-instructor.

Trace Instructor structured-output requests in Future AGI.

1. Installation

Use Python 3.10–3.13. Install the instrumentation and application dependencies.

pip install traceAI-instructor instructor openai pydantic

2. Set Environment Variables

Set environment variables to authenticate with Future AGI and OpenAI.

import os

os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
os.environ["FI_API_KEY"] = "your-futureagi-api-key"
os.environ["FI_SECRET_KEY"] = "your-futureagi-secret-key"

3. Initialize Trace Provider

Set up the trace provider to create a new project in FutureAGI, establish telemetry data pipelines.

from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType

trace_provider = register(
    project_type=ProjectType.OBSERVE,
    project_name="Instructor",
)

4. Instrument your Project

Use the Instructor Instrumentor to instrument your project.

from traceai_instructor import InstructorInstrumentor

InstructorInstrumentor().instrument(tracer_provider=trace_provider)

5. Run your Instructor application.

Run an Instructor request that extracts a structured result.

import instructor

from openai import OpenAI
from pydantic import BaseModel

# Define the output structure
class UserInfo(BaseModel):
    name: str
    age: int

# Patch the OpenAI client
client = instructor.patch(client=OpenAI())

user_info = client.chat.completions.create(
    model="gpt-3.5-turbo",
    response_model=UserInfo,
    messages=[
        {
            "role": "system",
            "content": "Extract the name and age from the text and return them in a structured format.",
        },
        {"role": "user", "content": "John Doe is nine years old."},
    ],
)

print(user_info, type(user_info))

Open the Instructor project in Future AGI to inspect the recorded request.

Was this page helpful?

Questions & Discussion