Prototype showing gen_ai.conversation_root span attribute to mark the root GenAI span of a conversation (WIP)#187
Draft
wrisa wants to merge 3 commits into
Draft
Prototype showing gen_ai.conversation_root span attribute to mark the root GenAI span of a conversation (WIP)#187wrisa wants to merge 3 commits into
wrisa wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes # (issue356)
``
Adds
gen_ai.conversation_rootas a boolean span attribute automatically set on `WorkflowInvocation` and `AgentInvocation` spans that are the outermost GenAI span in a trace — i.e., the entry point of a conversation.Detection uses a context-scoped attribute key (following the API shape of open-telemetry/opentelemetry-specification#4931) stored under a private OTel context key rather than raw OTel span parentage. This correctly handles the common production case where a GenAI root span lives under a non-GenAI parent (HTTP, gRPC, queue consumer, etc.) — a plain OTel parent span never writes the key, so it never suppresses conversation_root.
Design
Context-scoped attribute key
A private OTel context key is used to propagate a
gen_ai.conversation_rootmarker through the OTel context:set_context_scoped_attributes uses existing-key-wins semantics (outermost writer wins), matching the CSA spec. This means once the root sets the key, any nested
WorkflowInvocationorAgentInvocationsees it already present and does not mark itself as root.Auto-detection in
WorkflowInvocationandAgentInvocationBefore _start():
Propagate the key so nested invocations do not mark themselves root
The key is attached alongside the span in a single attach() call and automatically detached when the invocation ends (same lifecycle as the span context token), so the next independent conversation starts fresh.
Emission
In GenAIInvocation._finish():
Only emitted when explicitly set — None means "not applicable" and produces no attribute.
Scope
Only
WorkflowInvocationandAgentInvocationparticipate in root detection.InferenceInvocation(chat),ToolInvocation(execute_tool),EmbeddingInvocation, andRetrievalInvocationnever set conversation_root — even when called at the top level with no GenAI parent — because they represent model/tool operations, not conversation-level entry points.Example traces
Agent under HTTP span (common production case):
Workflow wrapping agents:
What this does NOT do
Relation to prior art
Please delete options that are not relevant.
How has this been tested?
Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. List any relevant details for your test
configuration.
Checklist
See CONTRIBUTING.md
for the style guide, changelog guidance, and more.