langfuse-python icon indicating copy to clipboard operation
langfuse-python copied to clipboard

fix: add option to force keep_trace_attributes in langfuse

Open philipshurpik opened this issue 4 months ago • 0 comments

Fix of the bug, when langfuse trace attributes was not passed to Langfuse UI, if call was inside of LLM chain

https://github.com/langfuse/langfuse/issues/10380


[!IMPORTANT] Fix bug in CallbackHandler.py to ensure trace attributes are passed to Langfuse UI when LLM is run inside a chain.

  • Bug Fix:
    • In CallbackHandler.py, fix bug in __on_llm_action where trace attributes were not passed to Langfuse UI if call was inside LLM chain.
    • Introduce keep_trace_attributes to determine if trace attributes should be kept based on metadata or if parent_run_id is None.

This description was created by Ellipsis for 3d36dfc28e2e950fc4a7b7382da0c1109e68538e. You can customize this summary. It will automatically update as commits are pushed.

Disclaimer: Experimental PR review

Greptile Overview

Greptile Summary

Adds keep_trace_attributes option to force preservation of Langfuse trace attributes (session ID, user ID, tags) when LLM calls occur inside chains

  • Previously, trace attributes were only preserved for isolated LLM calls (parent_run_id is None)
  • Now allows explicit override via metadata['keep_trace_attributes'] to preserve attributes in chain contexts
  • Critical bug: Line 796 will raise AttributeError when metadata is None since .get() is called on optional parameter

Confidence Score: 2/5

  • This PR contains a critical bug that will cause runtime errors when metadata is None
  • The implementation adds the requested feature but introduces an AttributeError on line 796 when metadata is None (calling .get() on None). This is a guaranteed runtime error that must be fixed before merging.
  • langfuse/langchain/CallbackHandler.py line 796 requires immediate fix for the None handling bug

Important Files Changed

File Analysis

Filename Score Overview
langfuse/langchain/CallbackHandler.py 2/5 Added keep_trace_attributes flag to allow forcing trace attributes in LLM chains, but contains AttributeError bug when metadata is None

Sequence Diagram

sequenceDiagram
    participant User
    participant LangchainCallbackHandler
    participant __on_llm_action
    participant __join_tags_and_metadata
    participant _strip_langfuse_keys_from_dict
    participant LangfuseObservation

    User->>LangchainCallbackHandler: on_llm_start(metadata={keep_trace_attributes: true})
    LangchainCallbackHandler->>__on_llm_action: Process LLM start event
    
    alt metadata.keep_trace_attributes == true
        __on_llm_action->>__on_llm_action: keep_trace_attributes = True
    else parent_run_id is None (isolated LLM call)
        __on_llm_action->>__on_llm_action: keep_trace_attributes = True
    else inside chain
        __on_llm_action->>__on_llm_action: keep_trace_attributes = False
    end
    
    __on_llm_action->>__join_tags_and_metadata: Join metadata with keep flag
    __join_tags_and_metadata->>_strip_langfuse_keys_from_dict: Strip keys conditionally
    
    alt keep_trace_attributes == True
        _strip_langfuse_keys_from_dict->>_strip_langfuse_keys_from_dict: Keep langfuse_session_id, langfuse_user_id, langfuse_tags
    else keep_trace_attributes == False
        _strip_langfuse_keys_from_dict->>_strip_langfuse_keys_from_dict: Strip langfuse_session_id, langfuse_user_id, langfuse_tags
    end
    
    _strip_langfuse_keys_from_dict-->>__on_llm_action: Return processed metadata
    __on_llm_action->>LangfuseObservation: start_observation(metadata=processed_metadata)
    LangfuseObservation-->>User: Trace created with/without attributes

philipshurpik avatar Nov 11 '25 13:11 philipshurpik