open-telemetry / open-telemetry/opentelemetry-python-genai

Instrument OpenAI structured outputs

Open
#394 3 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
39
Forks
63
Avg merge
1d 15h
Merged PRs (30d)
175

Description

What problem do you want to solve?

Instrument https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat

Currently this records nothing even when exporters are configured:

import os

import openai
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor
from pydantic import BaseModel

os.environ['OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT'] = 'true'

OpenAIInstrumentor().instrument()


class CalendarEvent(BaseModel):
    name: str
    date: str
    participants: list[str]


completion = openai.beta.chat.completions.parse(
    model='gpt-4o-2024-08-06',
    messages=[
        {'role': 'system', 'content': 'Extract the event information.'},
        {'role': 'user', 'content': 'Alice and Bob are going to a science fair on Friday.'},
    ],
    response_format=CalendarEvent,
)

print(completion.choices[0].message.parsed)
Describe the solution you'd like

openai.beta.chat.completions.parse should be instrumented just like openai.chat.completions.create is.

Describe alternatives you've considered

No response

Additional Context

No response

Would you like to implement a fix?

No

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at OpenAIInstrumentor and trace how openai.chat.completions.create is instrumented. Compare that path with openai.beta.chat.completions.parse, then verify that configured exporters record the structured-output call when message-content capture is enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.