fix(langchain): keep trace attributes on non-chain llm runs #1293
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Important
Fixes in
CallbackHandler.pyensure trace attributes are preserved for isolated non-chain LLM runs in Langchain integration._register_langfuse_promptinCallbackHandler.pyto return early ifrun_idis not provided.__on_llm_actionto keep trace attributes if LLM is run isolated and outside a chain._deregister_langfuse_promptto check forrun_idbeingNonebefore accessingprompt_to_parent_run_map.__join_tags_and_metadatato acceptkeep_langfuse_trace_attributesand pass it to_strip_langfuse_keys_from_dict._strip_langfuse_keys_from_dictto conditionally remove trace attributes based onkeep_langfuse_trace_attributes.This description was created by
for 8b5d63a. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR fixes a bug in the Langfuse Langchain callback handler where trace attributes (
session_id,user_id,tags) were being incorrectly stripped from LLM runs executed outside of chains. The issue occurred because the existing logic was designed to remove all Langfuse-specific metadata keys from chain runs but was also removing valuable trace context from standalone LLM calls.The changes introduce conditional logic to distinguish between two scenarios:
Key technical changes include:
_register_langfuse_promptand_deregister_langfuse_prompt)__on_llm_actionto detect root-level LLM runs (whereparent_run_idis None) and preserve trace attributes_strip_langfuse_keys_from_dictto separate internal metadata keys from user-facing trace attribute keyskeep_langfuse_trace_attributesparameter to__join_tags_and_metadatafor conditional behaviorThis change integrates well with the existing Langfuse callback architecture by extending rather than replacing the current metadata handling logic. The conditional approach ensures backward compatibility for chain-based workflows while fixing the data loss issue for standalone LLM invocations.
Confidence score: 4/5
_strip_langfuse_keys_from_dictto ensure the key categorization is correct