Skip to content

🐛 Bug Report: streaming openai responses api does not emit spans #3395

Description

@ellie-endgame

Which component is this bug for?

OpenAI Instrumentation

📜 Description

Using the responses api, we do not see spans emitted if stream=True on the respones.create command for openapi.

it looks like, potentially, if streaming is being used it returns prior to starting the span.
https://github.com/traceloop/openllmetry/blob/main/packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/v1/responses_wrappers.py#L442

👟 Reproduction steps

script to reproduce:

# /// script
# dependencies = [
#   "opentelemetry-instrumentation-openai>=0.46.2",
#   "opentelemetry-api>=1.31.1",
#   "opentelemetry-sdk>=1.31.1",
#   "opentelemetry-exporter-otlp>=1.32.1",
#   "openai>=1.69.0",
# ]
# ///
import os
from urllib import response

from openai import OpenAI
from opentelemetry import trace
from opentelemetry.instrumentation.openai import OpenAIInstrumentor  # Traceloop's
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

api_key = os.getenv("OPENAI_API_KEY")

tracer_provider = TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(tracer_provider)

OpenAIInstrumentor().instrument()

tracer = trace.get_tracer(__name__)

with tracer.start_as_current_span("example-span"):
    client = OpenAI(api_key=api_key)
    response = client.responses.create(model="gpt-4o-mini", input=[{"role": "user", "content": "ping"}], stream=True)
    for chunk in response:
        print(chunk)

# example: OPENAI_API_KEY=... uv run scripts/missing-spans-when-streaming.py

👍 Expected behavior

It should show spans printed the console for the openapi responses api request

👎 Actual Behavior with Screenshots

It only shows ChatCompletionEvents, no spans.

🤖 Python Version

No response

📃 Provide any additional context for the Bug.

No response

👀 Have you spent some time to check if this bug has been raised before?

  • I checked and didn't find similar issue

Are you willing to submit PR?

None

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions