langflow-ai / langflow

Langflow is a low-code app builder for RAG and multi-agent AI applications. It’s Python-based and agnostic to any model, API, or database.
http://www.langflow.org
MIT License
32.34k stars 3.97k forks source link

How do i add a session id or user or custom metadata to Langfuse traces? #4274

Open hamzaxd11 opened 2 hours ago

hamzaxd11 commented 2 hours ago

I'm working with Langflow and need to pass custom metadata (like session ID, user ID, or other relevant information) into Langfuse traces. I find it difficult to understand the class hierarchies and code structure, and I’m not sure where to inject this metadata for it to be tracked properly in the trace lifecycle.

Additionally, my desired functionality is to be able to send a session ID as part of the API request to Langflow, and have that session ID be used as the session identifier within the Langfuse traces. Essentially, I want to ensure that each session is uniquely tracked using the session ID provided in the API request.

Could someone please guide me or provide a simple solution to achieve this? A step-by-step explanation or pointers to the relevant parts of the code would be really helpful!

Thanks in advance!

dosubot[bot] commented 2 hours ago

Hey @hamzaxd11! I'm here to help you with your Langfuse traces. Let's get this sorted out!

To add a session ID or custom metadata to Langfuse traces when working with Langflow, you can use the add_trace method in the LangWatchTracer class. This method allows you to include a session ID in the inputs dictionary, which will then be used to update the trace metadata with a unique thread_id. Here's how you can ensure each session is uniquely tracked:

  1. Modify the add_trace Method: Ensure that the add_trace method in the LangWatchTracer class is set up to handle the session ID. You can include the session ID in the inputs dictionary when calling this method. The method will update the trace metadata with the session ID as the thread_id.

    def add_trace(
       self,
       trace_id: str,
       trace_name: str,
       trace_type: str,
       inputs: dict[str, Any],
       metadata: dict[str, Any] | None = None,
       vertex: Vertex | None = None,
    ) -> None:
       if not self._ready:
           return
       # If user is not using session_id, then it becomes the same as flow_id, but
       # we don't want to have an infinite thread with all the flow messages
       if "session_id" in inputs and inputs["session_id"] != self.flow_id:
           self.trace.update(metadata=(self.trace.metadata or {}) | {"thread_id": inputs["session_id"]})
  2. Pass the Session ID: When you make an API request to Langflow, include the session ID in the inputs dictionary. This will ensure that the session ID is used as the session identifier within the Langfuse traces.

By following these steps, you can ensure that each session is uniquely tracked using the session ID provided in the API request [1].

To continue talking to Dosu, mention @dosu.

hamzaxd11 commented 2 hours ago

@dosu Can you tell me about the inputs dictionary in this context? This is the body schema of the run api:

{ "input_value": "string", "input_type": "chat", "output_type": "chat", "output_component": "", "tweaks": { "Component Name": { "parameter_name": "value" }, "component_id": { "parameter_name": "value" }, "parameter_name": "value" }, "session_id": "string" }

Do you mean that i should send the session_id over here where the session_id is or in the query params or what?

dosubot[bot] commented 2 hours ago

You should include the session_id in the inputs dictionary of the run API body schema, as shown in your example. The session_id is part of the request body and is used to manage session data for flow execution. It should not be included in the query parameters [1].

To continue talking to Dosu, mention @dosu.

hamzaxd11 commented 2 hours ago

@dosu I integrated your code and then tested the api with a session id, i got this error:

AttributeError:
'StatefulTraceClient' object has no
attribute 'metadata'

This is the code you provided and i integrated:

Check if a session ID is provided in the inputs and is not the same as the flow ID

    if "session_id" in inputs and inputs["session_id"] != self.flow_id:
        # Update the trace metadata with the session ID as the thread_id
        self.trace.update(metadata=(self.trace.metadata or {}) | {"thread_id": inputs["session_id"]})
dosubot[bot] commented 2 hours ago

The error you're encountering suggests that the StatefulTraceClient object does not have a metadata attribute. This might be due to the way the trace object is being used or initialized.

To resolve this, you should ensure that the trace object within the LangWatchTracer class has a metadata attribute before attempting to update it. You can modify the code to handle cases where metadata might not be initialized:

# Check if a session ID is provided in the inputs and is not the same as the flow ID
if "session_id" in inputs and inputs["session_id"] != self.flow_id:
    # Ensure the trace has a metadata attribute
    if not hasattr(self.trace, 'metadata') or self.trace.metadata is None:
        self.trace.metadata = {}
    # Update the trace metadata with the session ID as the thread_id
    self.trace.update(metadata=self.trace.metadata | {"thread_id": inputs["session_id"]})

This code snippet first checks if the trace object has a metadata attribute and initializes it if necessary. Then, it updates the metadata with the session_id. This should prevent the AttributeError you're seeing.

To continue talking to Dosu, mention @dosu.