Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion langfuse/_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ def __init__(
or get_common_release_envs()
)
self._project_id: Optional[str] = None
sample_rate = sample_rate or float(os.environ.get(LANGFUSE_SAMPLE_RATE, 1.0))
if sample_rate is None:
sample_rate = float(os.environ.get(LANGFUSE_SAMPLE_RATE, 1.0))
if not 0.0 <= sample_rate <= 1.0:
raise ValueError(
f"Sample rate must be between 0.0 and 1.0, got {sample_rate}"
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ def test_default_base_url(self, cleanup_env_vars):

assert client._base_url == "https://cloud.langfuse.com"

def test_zero_sample_rate_parameter_is_preserved(
self, cleanup_env_vars, monkeypatch
):
"""Test that an explicit zero sample rate overrides the environment."""
monkeypatch.setenv("LANGFUSE_SAMPLE_RATE", "1.0")

client = Langfuse(
public_key="test_pk_zero_sample_rate",
secret_key="test_sk",
tracing_enabled=False,
sample_rate=0.0,
)

assert client._resources is not None
assert client._resources.sample_rate == 0.0

def test_base_url_env_var(self, cleanup_env_vars):
"""Test that LANGFUSE_BASE_URL environment variable is used correctly."""
os.environ["LANGFUSE_BASE_URL"] = "http://test-base-url.com"
Expand Down