Skip to content

Commit a19056a

Browse files
committed
fix(get_client): preserve properties on client such as environment
1 parent f153b59 commit a19056a

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

langfuse/_client/get_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def get_client(*, public_key: Optional[str] = None) -> Langfuse:
9898
secret_key=instance.secret_key,
9999
host=instance.host,
100100
tracing_enabled=instance.tracing_enabled,
101+
environment=instance.environment,
101102
)
102103

103104
else:
@@ -131,4 +132,5 @@ def get_client(*, public_key: Optional[str] = None) -> Langfuse:
131132
secret_key=target_instance.secret_key,
132133
host=target_instance.host,
133134
tracing_enabled=target_instance.tracing_enabled,
135+
environment=target_instance.environment,
134136
)

langfuse/_client/resource_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def _initialize_instance(
162162
self.tracing_enabled = tracing_enabled
163163
self.host = host
164164
self.mask = mask
165+
self.environment = environment
165166

166167
# OTEL Tracer
167168
if tracing_enabled:

tests/test_resource_manager.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Test environment setting on Langfuse get_client() function."""
2+
3+
import pytest
4+
from langfuse import Langfuse
5+
from langfuse._client.get_client import get_client
6+
7+
8+
def test_get_client_preserves_environment():
9+
"""Test that get_client() preserves the environment when returning existing clients."""
10+
test_env = "production-test-env"
11+
original_client = Langfuse(environment=test_env)
12+
13+
# Verify the original client has the correct environment
14+
assert original_client._environment == test_env, (
15+
f"original client environment should be '{test_env}', got '{original_client._environment}'"
16+
)
17+
18+
# call get_client() should return a client with the same environment
19+
retrieved_client = get_client()
20+
21+
assert retrieved_client._environment == test_env, (
22+
f"get_client() should return client with environment '{test_env}', got '{retrieved_client._environment}'"
23+
)
24+
25+
original_client.shutdown()
26+
27+
28+
def test_get_client_with_multiple_environments():
29+
"""Test get_client() behavior with multiple clients having different environments."""
30+
env_a = "environment-a"
31+
env_b = "environment-b"
32+
33+
client_a = Langfuse(public_key="pk-a", secret_key="sk-a", environment=env_a)
34+
client_b = Langfuse(public_key="pk-b", secret_key="sk-b", environment=env_b)
35+
36+
# original clients should have correct environments
37+
assert client_a._environment == env_a
38+
assert client_b._environment == env_b
39+
40+
# Get clients using get_client() with specific public keys
41+
retrieved_a = get_client(public_key="pk-a")
42+
retrieved_b = get_client(public_key="pk-b")
43+
44+
# should have the same environments as the originals
45+
assert retrieved_a._environment == env_a, (
46+
f"Expected client A environment to be '{env_a}', got '{retrieved_a._environment}'"
47+
)
48+
assert retrieved_b._environment == env_b, (
49+
f"Expected client B environment to be '{env_b}', got '{retrieved_b._environment}'"
50+
)
51+
52+
client_a.shutdown()
53+
client_b.shutdown()
54+
55+
56+
def test_get_client_single_client_environment():
57+
"""Test that get_client() preserves environment in single-client scenario."""
58+
# Clean state - ensure no existing clients
59+
from langfuse._client.resource_manager import LangfuseResourceManager
60+
61+
with LangfuseResourceManager._lock:
62+
LangfuseResourceManager._instances.clear()
63+
64+
test_env = "single-client-env"
65+
66+
client = Langfuse(environment=test_env)
67+
assert client._environment == test_env
68+
69+
# get_client() should return a client with the same environment
70+
retrieved = get_client()
71+
72+
# This assertion demonstrates the bug
73+
assert retrieved._environment == test_env, (
74+
f"Expected single client scenario to preserve environment '{test_env}', got '{retrieved._environment}'"
75+
)
76+
77+
client.shutdown()

0 commit comments

Comments
 (0)