Skip to content

Conversation

@philipshurpik
Copy link
Contributor

@philipshurpik philipshurpik commented Nov 11, 2025

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

langfuse/langfuse#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 3d36dfc. 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
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

philipshurpik and others added 2 commits November 11, 2025 14:04
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
@hassiebp hassiebp self-requested a review November 12, 2025 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant