Skip to content

fix(core): prefer OTEL trace ID over external span when trace IDs differ#1542

Open
cfauchere wants to merge 5 commits intomainfrom
fix/prefer-otel-trace-id-over-external
Open

fix(core): prefer OTEL trace ID over external span when trace IDs differ#1542
cfauchere wants to merge 5 commits intomainfrom
fix/prefer-otel-trace-id-over-external

Conversation

@cfauchere
Copy link
Copy Markdown
Contributor

Summary

When both an OTEL current span (agent runtime) and an external span (OpenInference LangChain instrumentor) exist with different trace IDs, get_parent_context() now returns the OTEL span.

Problem

The agent runtime sets an OTEL span with the agent's trace ID. The OpenInference LangChain instrumentor creates its own spans with a separate trace ID. When the Python SDK makes HTTP calls to downstream services (ECS, LLM Gateway), _get_bottom_most_span() would pick the OpenInference span because the depth-based tiebreaker defaulted to the external span.

Result: the x-uipath-traceparent-id header carried a different trace ID than the agent trace, making it impossible to correlate agent traces with CG/ECS logs.

Fix

In _get_bottom_most_span(), when the two spans have different trace IDs, return the OTEL current span immediately (before the depth comparison). The existing depth logic still applies when trace IDs match.

Validation

Tested locally: ran an agent with a CG semantic search tool, confirmed the header's trace ID matches the agent's TraceId.

Test plan

  • Verify trace ID in x-uipath-traceparent-id header matches agent TraceId
  • Verify existing span parent-child logic still works when trace IDs match
  • Verify no regression when only one span exists (no external provider)

🤖 Generated with Claude Code

When both an OTEL current span (set by the agent runtime) and an
external span (from OpenInference LangChain instrumentor) exist with
different trace IDs, get_parent_context() now returns the OTEL span.

Previously, the depth-based tiebreaker would pick the external span,
causing the agent's trace ID to be lost. Downstream services (ECS)
received a different trace ID than the agent trace, making correlation
impossible.

Validated locally: the x-uipath-traceparent-id header now carries the
same trace ID as the agent trace.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions bot added test:uipath-langchain Triggers tests in the uipath-langchain-python repository test:uipath-runtime labels Apr 2, 2026
Clement Fauchere and others added 4 commits April 2, 2026 08:02
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…iffer

Verifies that when the OTEL current span (agent runtime) and the
external span (OpenInference) have different trace IDs, the @Traced
decorator inherits the OTEL trace ID.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…test

Updated test_ctx_parameter_required_when_external_deeper_than_current
to assert the new behavior: when OTEL and external spans have different
trace IDs, OTEL span is preferred. Removed the duplicate test from
test_external_integration.py.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@JosephMar JosephMar requested a review from saksharthakkar April 2, 2026 18:19
@saksharthakkar
Copy link
Copy Markdown
Contributor

@akshaylive I vaguely remember for simulated tools you had a scenario where you needed Openinference trace id and not manual instrumentation Id... please take a look, just wanted to check if your scenario is not affected by this change.

Copy link
Copy Markdown
Collaborator

@akshaylive akshaylive left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you demonstrate running multiple evals in parallel using simulation in the PR description please?

span_capture: SpanCapture,
):
"""Test that trace.get_current_span(ctx) is required when external span is deeper.
"""Test that OTEL span is preferred when trace IDs differ from external span.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test name is different from the intention. Could you add a test instead of replacing this please?

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

Labels

test:uipath-langchain Triggers tests in the uipath-langchain-python repository test:uipath-runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants