Instructor is a Python library for working with structured outputs for LLMs built on top of Pydantic. It provides a simple way to manage schema validations, retries and streaming responses.

You can check out the Colab Notebook if you’d like to jump straight to the code:

Open In Colab

Getting started

First, ensure you have both opik and instructor installed:

$pip install opik instructor

In addition, you can configure Opik using the opik configure command which will prompt you for the correct local server address or if you are using the Cloud platform your API key:

$opik configure

Using Opik with Instructor library

To use Opik with Instructor, we are going to rely on the existing Opik integrations with popular LLM providers.

For all the integrations, we will first add tracking to the LLM client and then pass it to the Instructor library:

1from opik.integrations.openai import track_openai
2import instructor
3from pydantic import BaseModel
4from openai import OpenAI
5
6# We will first create the OpenAI client and add the `track_openai` method to log data to Opik
7openai_client = track_openai(OpenAI())
8
9# Patch the OpenAI client for Instructor
10client = instructor.from_openai(openai_client)
11
12# Define your desired output structure
13class UserInfo(BaseModel):
14 name: str
15 age: int
16
17user_info = client.chat.completions.create(
18 model="gpt-4o-mini",
19 response_model=UserInfo,
20 messages=[{"role": "user", "content": "John Doe is 30 years old."}],
21)
22
23print(user_info)

Thanks to the track_openai method, all the calls made to OpenAI will be logged to the Opik platform. This approach also works well if you are also using the opik.track decorator as it will automatically log the LLM call made with Instructor to the relevant trace.

Supported LLM providers

Opik supports the OpenAI, Anthropic and Gemini LLM providers, feel free to create a Github issue if you would like us to support more providers.

1from opik.integrations.openai import track_openai
2import instructor
3from openai import OpenAI
4
5# Add Opik tracking
6openai_client = track_openai(OpenAI())
7
8# Patch the OpenAI client for Instructor
9client = instructor.from_openai(openai_client)

You can read more about how to use the Instructor library in their documentation.

Built with