Instructor
Trace Instructor structured outputs with cetra from Python, with the model call, its tokens and cost.
Install
Section titled “Install”pip install "cetra-sdk[instructor,openai]" instructorSet up
Section titled “Set up”import os
import cetraimport instructorimport openaifrom pydantic import BaseModel
class User(BaseModel): """The model extracted from the prompt."""
name: str age: int
cetra.init(instrument=["instructor", "openai"])
client = instructor.from_openai(openai.OpenAI(api_key=os.environ["OPENAI_API_KEY"]))user = client.chat.completions.create( model="gpt-4o-mini", response_model=User, messages=[{"role": "user", "content": "Ada is 36."}],)print(user)
cetra.flush()What you get
Section titled “What you get”- The extraction is a tree: an
instructor.createchainspan, with the model call as anllmspan under it. - The
chainspan carries the response model’s name and the extracted output. It carries no token counts. - The
llmspan carries the messages, the tool definition built from the pydantic model, the token counts and the cost, computed from the built-in prices.
Limits
Section titled “Limits”- Enable
openaitoo, as in the sample: Instructor’s own span carries no token counts, so without the OpenAI instrumentor the run has no tokens and no cost. - The
instructorextra needs Python 3.13 or earlier: the OpenInference Instructor instrumentation does not support Python 3.14 yet. - The fixture was captured with instructor 1.17.0, openinference-instrumentation-instructor 0.1.35 and openinference-instrumentation-openai 0.1.69.
- If a field is missing, look at the span’s Attributes tab and compare it with Span attributes.
- Traces: read the trace as a tree and a timeline.
- Cost and tokens: how cost is computed from the token counts.
- Online rules: score these traces automatically.