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

Fixed TypeError in LangfuseTrace #1184

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

alex-stoica
Copy link

Related Issues

Proposed Changes:

The primary issue was caused by the missing parent_span parameter in LangfuseTracer.trace, as explained in the original issue discussion.

Additionally, after updating the function signature to accept parent_span, meta was not found anymore as the code didn’t rely on it directly. The solution involves creating a root span when on creating a root span when parent_span does not exist or a child span when it does.

How did you test it?

Locally with the following setup:

pipe.add_component("llm", AzureOpenAIGenerator(...))
pipe.add_component("pb", PromptBuilder(...))
pipe.connect("pb.prompt", "llm.prompt")

And with a custom generator

Notes for the reviewer

I’ve left some commented code for a potential future implementation related to #1154

@alex-stoica alex-stoica requested a review from a team as a code owner November 12, 2024 19:08
@alex-stoica alex-stoica requested review from davidsbatista and removed request for a team November 12, 2024 19:08
@CLAassistant
Copy link

CLAassistant commented Nov 12, 2024

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added integration:langfuse type:documentation Improvements or additions to documentation labels Nov 12, 2024
@alex-stoica alex-stoica changed the title Readded original comments and completion_start_time Fixed TypeError in LangfuseTrace Nov 12, 2024
@alex-stoica alex-stoica marked this pull request as draft November 12, 2024 20:50
@alex-stoica alex-stoica force-pushed the fix/bug-1174-langfuse-parent-span branch from a3651ba to 2b6782d Compare November 13, 2024 06:33
@alex-stoica alex-stoica marked this pull request as ready for review November 13, 2024 06:43
@alex-stoica alex-stoica force-pushed the fix/bug-1174-langfuse-parent-span branch from 93e427b to 6b6c5df Compare November 13, 2024 06:48
@vblagoje
Copy link
Member

@silvanocerza do you want to take over this PR review?

Copy link
Member

@vblagoje vblagoje left a comment

Choose a reason for hiding this comment

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

Some minor issues found, please have another look @alex-stoica 🙏

@alex-stoica
Copy link
Author

Nice catches, @vblagoje. Solved them

@davidsbatista davidsbatista removed their request for review November 15, 2024 09:35
@vblagoje
Copy link
Member

@alex-stoica thanks once again for your efforts. We seem to have caused a small regressions where spans don't have latency attached to them. I'm not talking about time-to-first-token that @LastRemote recently worked on but this particular change in span management and context stack handling in the trace method that have likely lead to spans not having the correct latency information.

Here's how this latency was available on Langfuse before:
Screenshot 2024-11-15 at 11 38 39

as well as in generation span:
Screenshot 2024-11-15 at 11 39 17

While this information is not available when running your PR branch:
Screenshot 2024-11-15 at 11 40 27

Would you please have a look where/how did we lose this important info. For the above screenshots I used chat.py in example directory of the langfuse integration. Perhaps you can as well during your dev/debug cycles.
Looking forward to your fixes here. 🙏

@alex-stoica
Copy link
Author

alex-stoica commented Nov 16, 2024

@vblagoje Indeed, I had overlooked the end() method call, and it turned out to be a bit trickier than I initially expected. At first, I tried updating the span with start_time and end_time, but after taking a closer look, I realized that end() effectively addressed the issue
image

I've also tested locally with chat.py with AzureOpenAIChatGenerator and it worked as expected.

@LastRemote
Copy link
Contributor

LastRemote commented Nov 17, 2024

@alex-stoica Thanks for making this change! I added the parent_span parameter in Haystack 2.7 with the intention to fix a bug where Langfuse traces overlap with each if there are two or more pipeline.run() running concurrently (which is very common in deployed pipelinements), but I haven't had a chance to update the Langfuse integration part since then.

Your changes look good, although I'd recommend we always create a new root span if parent_span is None to prevent the concurrency issue (there would be cases where the 2nd pipeline.run has parent_span is None with a non-empty current_span, resulting the follow-up trace be registered as a span instead of trace). Also I suppose you don't have to deal with meta this way.

Here is something I have in my draft:

tracing_context: ContextVar[Dict[Any, Any]] = ContextVar("tracing_context", default={})
"""
A context variable containing information related to tracing. This can be used to store trace, user, and session IDs,
custom tags and pipeline version information.
"""

class LangfuseTracer(Tracer):
    @contextlib.contextmanager
    def trace(
        self, operation_name: str, tags: Optional[Dict[str, Any]] = None, parent_span: Optional[Span] = None
    ) -> Iterator[Span]:
        """
        Start and manage a new trace span.

        :param operation_name: The name of the operation.
        :param tags: A dictionary of tags to attach to the span.
        :param parent_span: The parent span to use for the newly created span.
        :return: A context manager yielding the span.
        """
        # pylint: disable=protected-access
        # This is to make it consistent with the original implementation
        tags = tags or {}
        span_name = tags.get("haystack.component.name", operation_name)

        if not parent_span:
            if operation_name != "haystack.pipeline.run":
                logger.warning(
                    "Creating a new trace without a parent span is not recommended for operation '%s'.", operation_name
                )
            # Create a new trace if no parent span is provided
            span = LangfuseSpan(
                self._tracer.trace(
                    name=self._name,
                    public=self._public,
                    id=tracing_context.get().get("trace_id"),
                    user_id=tracing_context.get().get("user_id"),
                    session_id=tracing_context.get().get("session_id"),
                    tags=tracing_context.get().get("tags"),
                    version=tracing_context.get().get("version"),
                )
            )
        else:
            if tags.get("haystack.component.type") in self.ALL_SUPPORTED_GENERATORS:
                span = LangfuseSpan(parent_span.raw_span().generation(name=span_name))
            else:
                span = LangfuseSpan(parent_span.raw_span().span(name=span_name))

        self._context.append(span)
        span.set_tags(tags)

        yield span

        ...  # The rest of the code is left unchanged

I hope this helps.

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.

TypeError: LangfuseTracer.trace() got an unexpected keyword argument 'parent_span'
4 participants