Skip to content

bug(LangfuseConnector): invocation_context parameter not applied to Langfuse traces #2484

@Hansehart

Description

@Hansehart

Describe the bug
The LangfuseConnector.run() method accepts an invocation_context parameter that is documented to make "key-value pairs visible in the Langfuse traces" (e.g. user_id, session_id). However, this parameter is completely ignored by the implementation - it only logs the value but never applies it to the trace metadata.

The documentation promises that passing {"user_id": "user-123"} as invocation_context will make this visible in Langfuse, but in reality, the user_id remains
None in all traces.

Expected behavior: The invocation_context parameter should set the corresponding trace metadata fields (user_id, session_id, tags, version) in Langfuse.

Root cause: The run() method should set the tracing_context_var ContextVar with the invocation_context data, but it doesn't. The tracer's DefaultSpanHandler.create_span() reads from tracing_context_var.get({}) to populate trace metadata, but nothing populates this ContextVar from the
invocation_context parameter.

To Reproduce

import os

os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"

from haystack import Pipeline
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.connectors.langfuse import LangfuseConnector
from haystack_integrations.components.generators.mistral import MistralChatGenerator

pipe = Pipeline()
pipe.add_component("tracer", LangfuseConnector("Test Pipeline"))
pipe.add_component("llm", MistralChatGenerator(model="open-mistral-nemo-2407"))

result = pipe.run(
    {
        "llm": {"messages": [ChatMessage.from_user("Hello")]},
        "tracer": {"invocation_context": {"user_id": "user-123"}},
    }
)

# Check Langfuse trace at result["tracer"]["trace_url"]
# Expected: user_id should be "user-123"
# Actual: user_id is None
print(result["tracer"]["trace_url"])

See connector.py that in line 174 the run method takes invocation_context and only logs it.

Describe your environment (please complete the following information):

  • OS: Ubuntu 24.04
  • Haystack version: 2.19.0
  • Integration version: 3.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions