66
77
88def get_client (* , public_key : Optional [str ] = None ) -> Langfuse :
9- """Retrieve or create a Langfuse client based on optionally provided public key.
10-
11- Client Management Strategy:
12- 1. Single-Project Case:
13- - When only one Langfuse client exists in the process, returns that client
14- - If no clients exist yet, initializes a new client using environment variables
15-
16- 2. Multi-Project Case:
17- - With explicit public_key: Returns the specific client for that project
18- - Without public_key: Returns a disabled tracing client to prevent data leakage
19-
20- Security Features:
21- The function implements a security-first approach to prevent cross-project data
22- leakage. When multiple projects are active in the same process, it requires explicit
23- project identification through the public_key parameter. Without this parameter,
24- it returns a disabled client that prevents any data from being incorrectly attributed
25- to the wrong project.
26-
27- Client Lifecycle:
28- The function does not create new client instances unnecessarily. It follows a
29- singleton pattern per public key, retrieving existing instances where possible
30- and only creating new ones when required. This approach conserves system resources
31- and maintains consistent client state.
9+ """Get or create a Langfuse client instance.
10+
11+ Returns an existing Langfuse client or creates a new one if none exists. In multi-project setups,
12+ providing a public_key is required. Multi-project support is experimental - see Langfuse docs.
13+
14+ Behavior:
15+ - Single project: Returns existing client or creates new one
16+ - Multi-project: Requires public_key to return specific client
17+ - No public_key in multi-project: Returns disabled client to prevent data leakage
18+
19+ The function uses a singleton pattern per public_key to conserve resources and maintain state.
3220
3321 Args:
34- public_key (Optional[str]): The Langfuse public key for project identification.
35- - If provided: Returns the client associated with this specific key
36- - If omitted: Returns either the single active client (if only one exists)
37- or creates a new default client (if none exist) or a disabled client
38- (if multiple exist)
22+ public_key (Optional[str]): Project identifier
23+ - With key: Returns client for that project
24+ - Without key: Returns single client or disabled client if multiple exist
3925
4026 Returns:
41- Langfuse: A properly configured Langfuse client instance with one of three states:
42- 1. Existing client for the specified public_key
43- 2. New or existing default client when only one project is in use
44- 3. Disabled client (tracing_enabled=False) to prevent cross-project data leakage
45- when multiple projects are active but no specific key is provided
46-
47- Security Note:
48- This method enforces strict project isolation by disabling tracing when
49- multiple projects are used without explicit project identification, protecting
50- against accidental data cross-contamination between different projects.
51-
52- Example Usage:
27+ Langfuse: Client instance in one of three states:
28+ 1. Client for specified public_key
29+ 2. Default client for single-project setup
30+ 3. Disabled client when multiple projects exist without key
31+
32+ Security:
33+ Disables tracing when multiple projects exist without explicit key to prevent
34+ cross-project data leakage. Multi-project setups are experimental.
35+
36+ Example:
5337 ```python
54- # In single- project usage:
55- client = get_client() # Returns the default client
38+ # Single project
39+ client = get_client() # Default client
5640
5741 # In multi-project usage:
5842 client_a = get_client(public_key="project_a_key") # Returns project A's client
@@ -80,7 +64,9 @@ def get_client(*, public_key: Optional[str] = None) -> Langfuse:
8064 langfuse_logger .warning (
8165 "No 'langfuse_public_key' passed to decorated function, but multiple langfuse clients are instantiated in current process. Skipping tracing for this function to avoid cross-project leakage."
8266 )
83- return Langfuse (tracing_enabled = False )
67+ return Langfuse (
68+ tracing_enabled = False , public_key = "fake" , secret_key = "fake"
69+ )
8470
8571 else :
8672 # Specific key provided, look up existing instance
@@ -91,6 +77,8 @@ def get_client(*, public_key: Optional[str] = None) -> Langfuse:
9177 langfuse_logger .warning (
9278 f"No Langfuse client with public key { public_key } has been initialized. Skipping tracing for decorated function."
9379 )
94- return Langfuse (tracing_enabled = False )
80+ return Langfuse (
81+ tracing_enabled = False , public_key = "fake" , secret_key = "fake"
82+ )
9583
9684 return Langfuse (public_key = public_key )
0 commit comments