-
Notifications
You must be signed in to change notification settings - Fork 228
feat(decorator): require langfuse_public_key only in top most decorated func #1281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, 1 comment
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The PR enhances the
@observedecorator to use context variables forlangfuse_public_keymanagement, reducing explicit key passing in nested functions, with comprehensive testing for various scenarios.@observedecorator now uses context variables to managelangfuse_public_key, requiring it only in the top-most decorated function.contextvarsinget_client.pyandobserve.py._current_public_keyContextVar and_set_current_public_key()context manager inget_client.py.get_client()to check execution context for public key if not explicitly provided.observe()inobserve.pyto wrap function calls with_set_current_public_key().test_decorators.pyfor context propagation in nested, async, and multi-project scenarios.This description was created by
for e95e93e. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR introduces context variable-based public key propagation for the
@observedecorator to improve usability in multi-project Langfuse setups. Previously, users had to explicitly passlangfuse_public_keyto every nested decorated function in a call chain, which was cumbersome and error-prone. The change leverages Python'scontextvarsmodule to automatically propagate the public key from the top-most decorated function to all nested decorated functions through execution context.The implementation adds three key components:
Context Variable Infrastructure (
get_client.py): Introduces_current_public_keyContextVar and_set_current_public_key()context manager for thread-safe key storage and retrieval. Theget_client()function now checks the execution context when no explicit public key is provided.Decorator Enhancement (
observe.py): Wraps both sync and async execution paths with the context manager to ensure the public key is available in execution context for nested functions. The patternwith _set_current_public_key(public_key):is consistently applied before callingget_client().Comprehensive Testing (
test_decorators.py): Adds four new test functions covering basic inheritance, deep nesting (4 levels), explicit key override scenarios, and security validation when no key is provided. Tests verify that the context propagation works correctly while maintaining isolation between concurrent executions.This change integrates seamlessly with the existing LangfuseResourceManager singleton pattern and maintains backward compatibility. Users can now specify
langfuse_public_keyonly at the top-level decorated function, significantly reducing boilerplate code while preventing cross-project data leakage through proper context isolation.Confidence score: 4/5
contextvarsmodule with proper cleanup and thread safetyget_client.pyto ensure proper isolation between concurrent executions