Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Aug 8, 2025

Important

Enhance trace updates in on_chain_start by parsing and applying specific metadata attributes in CallbackHandler.py.

  • Behavior:
    • In on_chain_start, update trace with parsed attributes from metadata using _parse_langfuse_trace_attributes_from_metadata().
    • _parse_langfuse_trace_attributes_from_metadata() extracts session_id, user_id, and tags from metadata.
  • Utility Functions:
    • Update _strip_langfuse_keys_from_dict() to remove langfuse_session_id, langfuse_user_id, and langfuse_tags from metadata.

This description was created by Ellipsis 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:

  • Added _parse_langfuse_trace_attributes_from_metadata() method that extracts and validates session_id, user_id, and tags from metadata
  • Modified on_chain_start() to detect root chain spans (where parent_run_id is None) and apply trace-level attributes via update_trace()
  • Extended the metadata key stripping logic to remove langfuse-specific keys after processing

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:

chain.run(input_text, metadata={"langfuse_session_id": "session123", "langfuse_user_id": "user456"})

The implementation includes proper type validation to ensure attributes are correctly typed before being passed to the trace update.

Confidence score: 4/5

  • This PR is safe to merge with minimal risk as it only adds new functionality without breaking existing behavior
  • Score reflects well-structured implementation that follows existing patterns, with proper validation and error handling
  • Pay close attention to the metadata parsing logic in _parse_langfuse_trace_attributes_from_metadata method

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, no comments

Edit Code Review Bot Settings | Greptile

@hassiebp hassiebp merged commit c9ad8e3 into main Aug 8, 2025
10 checks passed
@hassiebp hassiebp deleted the optimize-langchain-trace-attributes branch August 8, 2025 09:04
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