diff --git a/langfuse/_client/client.py b/langfuse/_client/client.py index d262f5047..c46278180 100644 --- a/langfuse/_client/client.py +++ b/langfuse/_client/client.py @@ -160,8 +160,24 @@ 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)) + 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 + 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 +204,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 (