[opentelemetry-util-genai] Add inference span to the context to avoid duplication of span - #479
Conversation
There was a problem hiding this comment.
Pull request overview
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Adds a well-known context entry for the active GenAI inference span so different instrumentations can detect/reuse it and avoid emitting duplicate inference spans.
Changes:
- Introduces
opentelemetry.util.genai.contexthelpers to set/get an inference span in OpenTelemetry context. - Updates
InferenceInvocationto attach its span under the inference-span context key during invocation start. - Adds unit tests and documentation guidance for downstream instrumentations to check for an existing inference span.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/context.py | Adds context helpers and establishes the inference-span context key. |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/_invocation.py | Adds a context creation hook and uses it before attaching context. |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/_inference_invocation.py | Ensures inference invocations publish their span into the inference-span context key. |
| util/opentelemetry-util-genai/src/opentelemetry/util/genai/init.py | Exposes the new context helper API at the package top level. |
| util/opentelemetry-util-genai/tests/test_context.py | Adds tests validating context behavior and interoperability. |
| AGENTS.md | Documents how to avoid duplicate inference spans using get_current_inference_span(). |
| .github/instructions/instrumentation.instructions.md | Adds guidance to check get_current_inference_span() before creating a duplicate span. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Pull request dashboard statusWaiting on the author · refreshed 2026-08-25 20:56 UTC Move out of draft to request review. Status above doesn't look right?
|
Description
The creation of the inference span can happen at different levels in the call stack by different instrumentation libraries.
It makes sense to add it to the context under a constant key, where instrumentations can check if it exists before creating it.
Instrumentations may modify the span (if they have something useful to add) or suppress their instrumentation entirely if not.
Instrumentations both outside and inside this repo should do this. I also sent a PR to the seem conv repo: open-telemetry/semantic-conventions-genai#475
Type of change
Please delete options that are not relevant.
How has this been tested?
Unit tests
Checklista