Skip to content

Instrument OpenAI structured outputs #3449

@alexmojaki

Description

@alexmojaki

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions