Skip to content

fix: prefer W3C baggage conversation ID for span consistency#158

Open
handsomesix wants to merge 1 commit into
SAP:mainfrom
handsomesix:fix/conversation-id-consistency
Open

fix: prefer W3C baggage conversation ID for span consistency#158
handsomesix wants to merge 1 commit into
SAP:mainfrom
handsomesix:fix/conversation-id-consistency

Conversation

@handsomesix

Copy link
Copy Markdown

Summary

Fix inconsistent gen_ai.conversation.id values between LangGraph invoke_agent spans and other spans when using traceloop instrumentation.

The change introduces a shared conversation ID resolution mechanism that prioritizes:

  1. Explicit conversation_id parameter
  2. gen_ai.conversation.id from W3C baggage
  3. None

This ensures all spans within the same trace use a consistent conversation ID.

Root Cause

invoke_agent spans relied on traceloop-derived values, which are sourced from thread_id.

At the same time, SAP Cloud SDK propagates the conversation identifier through the W3C baggage header. When the traceloop thread_id and baggage-provided conversation ID differ, invoke_agent spans receive a different gen_ai.conversation.id than the rest of the trace.

As a result, spans belonging to the same conversation can be associated with different conversation identifiers.

Changes

  • Added _resolve_conversation_id() helper to centralize conversation ID resolution.
  • Implemented the following priority order:
    • Explicit conversation_id parameter
    • W3C baggage gen_ai.conversation.id
    • None
  • Applied the shared resolution logic to chat_span.
  • Applied the shared resolution logic to invoke_agent_span.
  • Ensured conversation ID handling is consistent across span types.

Tests

Added unit tests covering _resolve_conversation_id() behavior:

  • Explicit conversation_id takes precedence.
  • Falls back to W3C baggage value when no explicit parameter is provided.
  • Returns None when baggage is not present.
  • Returns None when baggage value is empty.

Risk

Low.

  • Backward compatible.
  • Existing explicit conversation_id behavior is unchanged.
  • Only introduces baggage-based fallback when no explicit conversation ID is provided.
  • Scope is limited to conversation ID resolution logic.

@handsomesix handsomesix requested a review from a team as a code owner June 12, 2026 02:51
@cla-assistant

cla-assistant Bot commented Jun 12, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

When using LangGraph with traceloop, the invoke_agent span gets a
different gen_ai.conversation.id than the one propagated via W3C
baggage header, because traceloop derives it from thread_id.

Add _resolve_conversation_id() that applies priority:
1. Explicit conversation_id parameter (highest)
2. W3C baggage gen_ai.conversation.id
3. None

Apply to both chat_span and invoke_agent_span so all spans in a
trace share the same conversation ID.

Fixes SAP#157
@handsomesix handsomesix force-pushed the fix/conversation-id-consistency branch from 7acc3b1 to af0733e Compare June 12, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant