Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add SpanHandler customization to langfuse integration #1311

Closed
wants to merge 3 commits into from

Conversation

vblagoje
Copy link
Member

Why:

Enhances the Langfuse integration by adding a flexible mechanism for custom span processing, allowing users to define how spans should be handled within their specific LLM components.

What:

  • Introduced SpanHandler, a callable type for custom span processing
  • Added span handler support to LangfuseConnector and LangfuseTracer
  • Default span processing behavior is preserved through default_span_handler

How can it be used:

from haystack import Pipeline
from haystack_integrations.components.connectors.langfuse import LangfuseConnector
from haystack_integrations.components.generators.custom import CustomChatGenerator

def custom_span_handler(span, component_type):
    if component_type == "CustomChatGenerator":
        output = span._data.get("haystack.component.output", {})
        span._span.update(
            usage=output.get("usage"),
            model="custom-llm-model",
            custom_metadata=output.get("extra_info")
        )

# Add to your pipeline
pipe = Pipeline()
pipe.add_component(
    "tracer",
    LangfuseConnector(
        name="Custom LLM Pipeline",
        span_handler=custom_span_handler
    )
)
pipe.add_component("llm", CustomChatGenerator())

How did you test it:

  • Unit tests for span handler serialization/deserialization
  • Integration tests with custom span handler
  • Verified traces appear correctly in Langfuse UI

Notes for the reviewer:

The callable approach was chosen over a class-based one for simplicity and better serialization support. The handler has access to both span data and component type, making it flexible enough for custom LLM integrations.

@github-actions github-actions bot added integration:langfuse type:documentation Improvements or additions to documentation labels Jan 21, 2025
@vblagoje vblagoje closed this Jan 21, 2025
@vblagoje vblagoje deleted the span_handler branch January 21, 2025 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration:langfuse type:documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add SpanHandler abstraction to enable custom LLM tracing in Langfuse
1 participant