Skip to main content
Instructor is a Python library for extracting structured, validated data from LLMs using Pydantic models. Braintrust traces each extraction call, including the retries and validation failures Instructor handles under the hood.

Setup

Install the Braintrust SDK and Instructor alongside your provider SDK:
Set your API keys before you run your app:
.env
Tracing Instructor requires instructor 1.11.0 or later and the Braintrust Python SDK v0.23.0 or later.

Auto-instrumentation

To trace Instructor without wrapping each client, call init_logger() and auto_instrument() once at startup. auto_instrument() patches Instructor’s create methods along with the underlying provider SDK, so each extraction appears as a task span with the provider’s LLM call nested beneath it.
To patch Instructor without enabling Braintrust’s other integrations, wrap the client with wrap_instructor() instead. See Manual instrumentation.

Manual instrumentation

To trace Instructor yourself, wrap the Instructor client with wrap_instructor(). Wrap the underlying provider client with wrap_openai() first so the provider’s LLM call is captured as a child of the Instructor span.

What Braintrust traces

Braintrust captures:
  • Structured-output spans (instructor.create, instructor.create_with_completion, instructor.create_partial, and instructor.create_iterable), with the request messages, response model, and Instructor mode as input, and the extracted Pydantic model as output.
  • Extraction metadata, including the model, provider, response model, Instructor mode, and configured max retries.
  • Retry details, including the retry count and the Pydantic validation errors that triggered each retry.
Token usage stays on the provider’s LLM child span, so enable provider instrumentation (wrap_openai() or auto_instrument()) to see token counts alongside the extraction.

Resources