Skip to content

🐛 Bug Report: OpenAI events don't contain span IDs #3234

@josephwangrb

Description

@josephwangrb

Which component is this bug for?

OpenAI Instrumentation

📜 Description

What's the issue?

The events that contain prompts input&output don't have SpanID inside, causing a lack of correlation between the span and the related event.

What's the root cause?

Looks like we are using start_span instead of start_as_current_span in a couple of places, such as https://github.com/traceloop/openllmetry/blob/main/packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/shared/chat_wrappers.py#L85. This causes the span to be not in the current context while the EventLogger takes span ID from the current span, which is None.

What's the resolution?

I tried to replace start_span with start_as_current_span, looks like it fixes this issue. Please advice if this is the right fix, if so I'm happy to contribute.

👟 Reproduction steps

start the instrumentor using use_legacy_attributes=False such that It emit input&output as events, then we found those events have no span id.

 OpenAIInstrumentor(
       use_legacy_attributes=False,
  ).instrument(
      event_logger_provider=EventLoggerProvider()
 )

👍 Expected behavior

Trace ID and Span ID should not be None as below, this is generated after I modified the code with start_as_current_span

otel-collector  | Attributes:
otel-collector  |      -> gen_ai.system: Str(openai)
otel-collector  |      -> event.name: Str(gen_ai.user.message)
otel-collector  | Trace ID: 305110b711d06527b6770dd191435bed
otel-collector  | Span ID: 41f231c4b4be309d
otel-collector  | Flags: 1
otel-collector  | LogRecord #2
otel-collector  | ObservedTimestamp: 2025-08-06 17:40:25.911162 +0000 UTC
otel-collector  | Timestamp: 2025-08-06 17:40:25.911127 +0000 UTC
otel-collector  | SeverityText:
otel-collector  | SeverityNumber: Info(9)
otel-collector  | Body: Map({"finish_reason":"stop","index":0,"message":{"content":"Hello! How can I assist you today?"}})
otel-collector  | Attributes:
otel-collector  |      -> gen_ai.system: Str(openai)
otel-collector  |      -> event.name: Str(gen_ai.choice)

👎 Actual Behavior with Screenshots

TraceId and Span ID are None.

otel-collector  | Body: Map({"content":"test"})
otel-collector  | Attributes:
otel-collector  |      -> gen_ai.system: Str(openai)
otel-collector  |      -> event.name: Str(gen_ai.user.message)
otel-collector  | **Trace ID:**
otel-collector  | **Span ID:**
otel-collector  | Flags: 0
otel-collector  | LogRecord #2
otel-collector  | ObservedTimestamp: 2025-08-06 17:22:32.717473 +0000 UTC
otel-collector  | Timestamp: 2025-08-06 17:22:32.717433 +0000 UTC
otel-collector  | SeverityText:
otel-collector  | SeverityNumber: Info(9)
otel-collector  | Body: Map({"finish_reason":"stop","index":0,"message":{"content":"Hello! How can I assist you today?"}})
otel-collector  | Attributes:
otel-collector  |      -> gen_ai.system: Str(openai)
otel-collector  |      -> event.name: Str(gen_ai.choice)

🤖 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?

Yes I am willing to submit a PR!

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