Skip to content

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Aug 5, 2025

Important

The PR enhances the @observe decorator to use context variables for langfuse_public_key management, reducing explicit key passing in nested functions, with comprehensive testing for various scenarios.

  • Behavior:
    • @observe decorator now uses context variables to manage langfuse_public_key, requiring it only in the top-most decorated function.
    • Context propagation implemented using contextvars in get_client.py and observe.py.
  • Implementation:
    • Introduces _current_public_key ContextVar and _set_current_public_key() context manager in get_client.py.
    • Modifies get_client() to check execution context for public key if not explicitly provided.
    • Updates observe() in observe.py to wrap function calls with _set_current_public_key().
  • Testing:
    • Adds tests in test_decorators.py for context propagation in nested, async, and multi-project scenarios.
    • Tests cover key inheritance, explicit key override, and context isolation in concurrent executions.

This description was created by Ellipsis 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 @observe decorator to improve usability in multi-project Langfuse setups. Previously, users had to explicitly pass langfuse_public_key to every nested decorated function in a call chain, which was cumbersome and error-prone. The change leverages Python's contextvars module 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:

  1. Context Variable Infrastructure (get_client.py): Introduces _current_public_key ContextVar and _set_current_public_key() context manager for thread-safe key storage and retrieval. The get_client() function now checks the execution context when no explicit public key is provided.

  2. 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 pattern with _set_current_public_key(public_key): is consistently applied before calling get_client().

  3. 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_key only at the top-level decorated function, significantly reducing boilerplate code while preventing cross-project data leakage through proper context isolation.

Confidence score: 4/5

  • This PR introduces well-architected changes with proper context variable usage and comprehensive test coverage
  • Score reflects the solid implementation using Python's built-in contextvars module with proper cleanup and thread safety
  • Pay close attention to the context variable implementation in get_client.py to ensure proper isolation between concurrent executions

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.

3 files reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

@hassiebp hassiebp merged commit fc44bec into main Aug 5, 2025
10 checks passed
@hassiebp hassiebp deleted the multi-project-lf-public-key branch August 5, 2025 17:47
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.

2 participants