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
15 changes: 15 additions & 0 deletions langfuse/_client/environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,19 @@
Float between 0 and 1 indicating the sample rate of traces to bet sent to Langfuse servers.

**Default value**: ``1.0``

"""
LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED = (
"LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED"
)
"""
.. envvar: LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED

Default capture of function args, kwargs and return value when using the @observe decorator.

Having default IO capture enabled for observe decorated function may have a performance impact on your application
if large or deeply nested objects are attempted to be serialized. Set this value to `False` and use manual
input/output setting on your observation to avoid this.

**Default value**: ``True``
"""
16 changes: 12 additions & 4 deletions langfuse/_client/observe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import inspect
import logging
import os
from functools import wraps
from typing import (
Any,
Expand All @@ -20,6 +21,9 @@

from typing_extensions import ParamSpec

from langfuse._client.environment_variables import (
LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED,
)
from langfuse._client.get_client import get_client
from langfuse._client.span import LangfuseGeneration, LangfuseSpan
from langfuse.types import TraceContext
Expand Down Expand Up @@ -141,24 +145,28 @@ 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")
)

def decorator(func: F) -> F:
return (
self._async_observe(
func,
name=name,
as_type=as_type,
capture_input=capture_input,
capture_output=capture_output,
capture_input=function_io_capture_enabled and capture_input,
capture_output=function_io_capture_enabled and capture_output,
transform_to_string=transform_to_string,
)
if asyncio.iscoroutinefunction(func)
else self._sync_observe(
func,
name=name,
as_type=as_type,
capture_input=capture_input,
capture_output=capture_output,
capture_input=function_io_capture_enabled and capture_input,
capture_output=function_io_capture_enabled and capture_output,
transform_to_string=transform_to_string,
)
)
Expand Down
Loading