perf(langchain): send trace updates on root span #1284
Merged
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
Enhance trace updates in
on_chain_startby parsing and applying specific metadata attributes inCallbackHandler.py.on_chain_start, update trace with parsed attributes from metadata using_parse_langfuse_trace_attributes_from_metadata()._parse_langfuse_trace_attributes_from_metadata()extractssession_id,user_id, andtagsfrom metadata._strip_langfuse_keys_from_dict()to removelangfuse_session_id,langfuse_user_id, andlangfuse_tagsfrom metadata.This description was created by
for 15f26fc. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR adds performance optimizations to the Langchain callback handler by enabling trace-level attributes to be set through Langchain's metadata parameter. The key changes include:
New Functionality:
_parse_langfuse_trace_attributes_from_metadata()method that extracts and validatessession_id,user_id, andtagsfrom metadataon_chain_start()to detect root chain spans (whereparent_run_idis None) and apply trace-level attributes viaupdate_trace()Integration with Existing Codebase:
This change fits naturally into the existing Langchain callback handler architecture. The handler already processes metadata and manages trace/span relationships. By leveraging the root span detection logic (
parent_run_id is None), the implementation correctly identifies when to apply trace-level attributes versus span-level attributes.The metadata stripping mechanism already existed for other langfuse-specific keys, so extending it to include the new trace attribute keys (
langfuse_session_id,langfuse_user_id,langfuse_tags) maintains consistency with the existing pattern of preventing langfuse-internal metadata from polluting the actual span metadata.User Experience Improvement:
This allows users to pass trace attributes directly through Langchain's standard metadata parameter when invoking chains, rather than requiring separate Langfuse API calls. For example, users can now do:
The implementation includes proper type validation to ensure attributes are correctly typed before being passed to the trace update.
Confidence score: 4/5
_parse_langfuse_trace_attributes_from_metadatamethod