Skip to content

Commit 23650a7

Browse files
fix(core): gracefully handle missing keys (#1200)
* fix(core): gracefully handle missing keys * Update langfuse/_client/client.py Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 26fd9e3 commit 23650a7

2 files changed

Lines changed: 20 additions & 22 deletions

File tree

langfuse/_client/client.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,24 @@ def __init__(
160160
sample_rate: Optional[float] = None,
161161
mask: Optional[MaskFunction] = None,
162162
):
163-
debug = debug if debug else (os.getenv(LANGFUSE_DEBUG, "False") == "True")
163+
self._host = host or os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com")
164+
self._environment = environment or os.environ.get(LANGFUSE_TRACING_ENVIRONMENT)
165+
self._mask = mask
166+
self._project_id = None
167+
sample_rate = sample_rate or float(os.environ.get(LANGFUSE_SAMPLE_RATE, 1.0))
168+
if not 0.0 <= sample_rate <= 1.0:
169+
raise ValueError(f"Sample rate must be between 0.0 and 1.0, got {sample_rate}")
164170

171+
self._tracing_enabled = (
172+
tracing_enabled
173+
and os.environ.get(LANGFUSE_TRACING_ENABLED, "True") != "False"
174+
)
175+
if not self._tracing_enabled:
176+
langfuse_logger.info(
177+
"Configuration: Langfuse tracing is explicitly disabled. No data will be sent to the Langfuse API."
178+
)
179+
180+
debug = debug if debug else (os.getenv(LANGFUSE_DEBUG, "False") == "True")
165181
if debug:
166182
logging.basicConfig(
167183
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
@@ -188,23 +204,6 @@ def __init__(
188204
self._otel_tracer = otel_trace_api.NoOpTracer()
189205
return
190206

191-
self._host = host or os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com")
192-
self._environment = environment or os.environ.get(LANGFUSE_TRACING_ENVIRONMENT)
193-
sample_rate = sample_rate or float(os.environ.get(LANGFUSE_SAMPLE_RATE, 1.0))
194-
195-
self._tracing_enabled = (
196-
tracing_enabled
197-
and os.environ.get(LANGFUSE_TRACING_ENABLED, "True") != "False"
198-
)
199-
200-
if not self._tracing_enabled:
201-
langfuse_logger.info(
202-
"Configuration: Langfuse tracing is explicitly disabled. No data will be sent to the Langfuse API."
203-
)
204-
205-
self._mask = mask
206-
self._project_id = None
207-
208207
# Initialize api and tracer if requirements are met
209208
self._resources = LangfuseResourceManager(
210209
public_key=public_key,

langfuse/_client/observe.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,9 @@ def sub_process():
145145
- For async functions, the decorator returns an async function wrapper.
146146
- For sync functions, the decorator returns a synchronous wrapper.
147147
"""
148-
function_io_capture_enabled = (
149-
os.environ.get(LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED, "True")
150-
.lower() not in ("false", "0")
151-
)
148+
function_io_capture_enabled = os.environ.get(
149+
LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED, "True"
150+
).lower() not in ("false", "0")
152151

153152
def decorator(func: F) -> F:
154153
return (

0 commit comments

Comments
 (0)