Skip to content

Commit 3dd812a

Browse files
authored
feat(observe): allow disabling function io capture by env var (#1196)
1 parent 84f0bf9 commit 3dd812a

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

langfuse/_client/environment_variables.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,19 @@
9494
Float between 0 and 1 indicating the sample rate of traces to bet sent to Langfuse servers.
9595
9696
**Default value**: ``1.0``
97+
98+
"""
99+
LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED = (
100+
"LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED"
101+
)
102+
"""
103+
.. envvar: LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED
104+
105+
Default capture of function args, kwargs and return value when using the @observe decorator.
106+
107+
Having default IO capture enabled for observe decorated function may have a performance impact on your application
108+
if large or deeply nested objects are attempted to be serialized. Set this value to `False` and use manual
109+
input/output setting on your observation to avoid this.
110+
111+
**Default value**: ``True``
97112
"""

langfuse/_client/observe.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import inspect
33
import logging
4+
import os
45
from functools import wraps
56
from typing import (
67
Any,
@@ -20,6 +21,9 @@
2021

2122
from typing_extensions import ParamSpec
2223

24+
from langfuse._client.environment_variables import (
25+
LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED,
26+
)
2327
from langfuse._client.get_client import get_client
2428
from langfuse._client.span import LangfuseGeneration, LangfuseSpan
2529
from langfuse.types import TraceContext
@@ -141,24 +145,28 @@ def sub_process():
141145
- For async functions, the decorator returns an async function wrapper.
142146
- For sync functions, the decorator returns a synchronous wrapper.
143147
"""
148+
function_io_capture_enabled = (
149+
os.environ.get(LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED, "True")
150+
.lower() not in ("false", "0")
151+
)
144152

145153
def decorator(func: F) -> F:
146154
return (
147155
self._async_observe(
148156
func,
149157
name=name,
150158
as_type=as_type,
151-
capture_input=capture_input,
152-
capture_output=capture_output,
159+
capture_input=function_io_capture_enabled and capture_input,
160+
capture_output=function_io_capture_enabled and capture_output,
153161
transform_to_string=transform_to_string,
154162
)
155163
if asyncio.iscoroutinefunction(func)
156164
else self._sync_observe(
157165
func,
158166
name=name,
159167
as_type=as_type,
160-
capture_input=capture_input,
161-
capture_output=capture_output,
168+
capture_input=function_io_capture_enabled and capture_input,
169+
capture_output=function_io_capture_enabled and capture_output,
162170
transform_to_string=transform_to_string,
163171
)
164172
)

0 commit comments

Comments
 (0)