Skip to main content
Instructor AI is a library that provides structured output capabilities for LLMs, making it easier to extract structured data from language models. For more details on Instructor AI, refer to the official Instructor documentation. LangWatch can capture traces generated by Instructor AI by leveraging OpenInference’s OpenAI instrumentation, since Instructor AI is built on top of OpenAI’s client. This guide will show you how to set it up.

Prerequisites

  1. Install LangWatch SDK:
  2. Install Instructor AI and OpenInference instrumentor:
  3. Set up your OpenAI API key: You’ll need to configure your OpenAI API key in your environment.

Instrumentation with OpenInference

LangWatch supports seamless observability for Instructor AI using the OpenInference Instructor AI instrumentor. This dedicated instrumentor automatically captures traces from your Instructor AI calls and sends them to LangWatch.

Basic Setup (Automatic Tracing)

Here’s the simplest way to instrument your application:
That’s it! All Instructor AI calls will now be traced and sent to your LangWatch dashboard automatically.

Optional: Using Decorators for Additional Context

If you want to add additional context or metadata to your traces, you can optionally use the @langwatch.trace() decorator:

How it Works

  1. langwatch.setup(): Initializes the LangWatch SDK, which includes setting up an OpenTelemetry trace exporter. This exporter is ready to receive spans from any OpenTelemetry-instrumented library in your application.
  2. InstructorInstrumentor(): The OpenInference instrumentor automatically patches Instructor AI operations to create OpenTelemetry spans for their operations, including:
    • Structured output generation
    • Model calls with response models
    • Validation and parsing
    • Error handling
  3. Instructor AI Integration: The dedicated Instructor AI instrumentor captures all Instructor AI operations (structured output generation, validation, etc.) as spans.
  4. Optional Decorators: You can optionally use @langwatch.trace() to add additional context and metadata to your traces, but it’s not required for basic functionality.
With this setup, all Instructor AI operations, including structured output generation, validation, and error handling, will be automatically traced and sent to LangWatch, providing comprehensive visibility into your structured data extraction applications.

Notes

  • You do not need to set any OpenTelemetry environment variables or configure exporters manually—langwatch.setup() handles everything.
  • You can combine Instructor AI instrumentation with other instrumentors (e.g., LangChain, DSPy) by adding them to the instrumentors list.
  • The @langwatch.trace() decorator is optional - the OpenInference instrumentor will capture all Instructor AI activity automatically.
  • For advanced configuration (custom attributes, endpoint, etc.), see the Python integration guide.

Troubleshooting

  • Make sure your LANGWATCH_API_KEY is set in the environment.
  • If you see no traces in LangWatch, check that the instrumentor is included in langwatch.setup() and that your Instructor AI code is being executed.
  • Ensure you have the correct OpenAI API key set.
  • Verify that your Pydantic models are properly defined and compatible with Instructor AI.

Interoperability with LangWatch SDK

You can use this integration together with the LangWatch Python SDK to add additional attributes to the trace:
This approach allows you to combine the automatic tracing capabilities of Instructor AI with the rich metadata and custom attributes provided by LangWatch.