From 1fb2d50edc71c364c9ce008c0da3287232030196 Mon Sep 17 00:00:00 2001 From: Hassieb Pakzad <68423100+hassiebp@users.noreply.github.com> Date: Wed, 28 May 2025 17:44:10 +0200 Subject: [PATCH 1/2] fix(core): gracefully handle missing keys --- langfuse/_client/client.py | 33 +++++++++++++++------------------ langfuse/_client/observe.py | 7 +++---- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/langfuse/_client/client.py b/langfuse/_client/client.py index d262f5047..a511189e0 100644 --- a/langfuse/_client/client.py +++ b/langfuse/_client/client.py @@ -160,8 +160,22 @@ def __init__( sample_rate: Optional[float] = None, mask: Optional[MaskFunction] = None, ): - debug = debug if debug else (os.getenv(LANGFUSE_DEBUG, "False") == "True") + self._host = host or os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com") + self._environment = environment or os.environ.get(LANGFUSE_TRACING_ENVIRONMENT) + self._mask = mask + self._project_id = None + sample_rate = sample_rate or float(os.environ.get(LANGFUSE_SAMPLE_RATE, 1.0)) + self._tracing_enabled = ( + tracing_enabled + and os.environ.get(LANGFUSE_TRACING_ENABLED, "True") != "False" + ) + if not self._tracing_enabled: + langfuse_logger.info( + "Configuration: Langfuse tracing is explicitly disabled. No data will be sent to the Langfuse API." + ) + + debug = debug if debug else (os.getenv(LANGFUSE_DEBUG, "False") == "True") if debug: logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" @@ -188,23 +202,6 @@ def __init__( self._otel_tracer = otel_trace_api.NoOpTracer() return - self._host = host or os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com") - self._environment = environment or os.environ.get(LANGFUSE_TRACING_ENVIRONMENT) - sample_rate = sample_rate or float(os.environ.get(LANGFUSE_SAMPLE_RATE, 1.0)) - - self._tracing_enabled = ( - tracing_enabled - and os.environ.get(LANGFUSE_TRACING_ENABLED, "True") != "False" - ) - - if not self._tracing_enabled: - langfuse_logger.info( - "Configuration: Langfuse tracing is explicitly disabled. No data will be sent to the Langfuse API." - ) - - self._mask = mask - self._project_id = None - # Initialize api and tracer if requirements are met self._resources = LangfuseResourceManager( public_key=public_key, diff --git a/langfuse/_client/observe.py b/langfuse/_client/observe.py index 8416ffd46..cdfb04d7b 100644 --- a/langfuse/_client/observe.py +++ b/langfuse/_client/observe.py @@ -145,10 +145,9 @@ def sub_process(): - For async functions, the decorator returns an async function wrapper. - For sync functions, the decorator returns a synchronous wrapper. """ - function_io_capture_enabled = ( - os.environ.get(LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED, "True") - .lower() not in ("false", "0") - ) + function_io_capture_enabled = os.environ.get( + LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED, "True" + ).lower() not in ("false", "0") def decorator(func: F) -> F: return ( From ed29f65a0070d635738eb9d2895805db8b8d3917 Mon Sep 17 00:00:00 2001 From: Hassieb Pakzad <68423100+hassiebp@users.noreply.github.com> Date: Wed, 28 May 2025 17:53:42 +0200 Subject: [PATCH 2/2] Update langfuse/_client/client.py Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --- langfuse/_client/client.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/langfuse/_client/client.py b/langfuse/_client/client.py index a511189e0..c46278180 100644 --- a/langfuse/_client/client.py +++ b/langfuse/_client/client.py @@ -165,6 +165,8 @@ def __init__( self._mask = mask self._project_id = None sample_rate = sample_rate or 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}") self._tracing_enabled = ( tracing_enabled