Skip to content

Bug(langfuse): trace_id from tracing context not applied to Langfuse traces #2780

Description

@LastRemote

Description

When creating a root span (no parent span) in the Langfuse tracer, the trace_id from Haystack's tracing context was not being passed to Langfuse's start_as_current_observation method. This caused custom trace IDs set in the tracing context to be ignored, making it difficult to correlate traces across different systems or maintain consistent trace identifiers.

Root Cause

In the DefaultSpanHandler.create_span() method, when creating a root span, the code was calling start_as_current_observation() without passing the trace_context parameter that contains the trace_id from the Haystack tracing context.

Fix

  • Import TraceContext type from langfuse.types
  • Extract trace_id from the tracing context (tracing_ctx)
  • Create a trace_context dictionary with the trace_id if it exists
  • Pass trace_context parameter to start_as_current_observation()

Impact

This fix ensures that when users set a custom trace_id in the Haystack tracing context, it will be properly propagated to Langfuse, enabling better trace correlation and consistent trace identification across distributed systems.

Testing

Added test case test_create_span_root_trace to verify that the trace_id is correctly extracted from the tracing context and passed to Langfuse when creating a root span.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions