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
20 changes: 19 additions & 1 deletion langfuse/_client/observe.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
overload,
)

from typing_extensions import ParamSpec
from opentelemetry.util._decorator import _AgnosticContextManager
from typing_extensions import ParamSpec

from langfuse._client.environment_variables import (
LANGFUSE_OBSERVE_DECORATOR_IO_CAPTURE_ENABLED,
Expand Down Expand Up @@ -267,6 +267,15 @@ async def async_wrapper(*args: Tuple[Any], **kwargs: Dict[str, Any]) -> Any:
result = await func(*args, **kwargs)

if capture_output is True:
if inspect.isgenerator(result):
is_return_type_generator = True

return self._wrap_sync_generator_result(
langfuse_span_or_generation,
result,
transform_to_string,
)

if inspect.isasyncgen(result):
is_return_type_generator = True

Expand Down Expand Up @@ -369,6 +378,15 @@ def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
transform_to_string,
)

if inspect.isasyncgen(result):
is_return_type_generator = True

return self._wrap_async_generator_result(
langfuse_span_or_generation,
result,
transform_to_string,
)

langfuse_span_or_generation.update(output=result)

return result
Expand Down
2 changes: 1 addition & 1 deletion tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ async def main_async(**kwargs):
assert main_observation.output == mock_output

assert nested_observation.name == "async_generator_function"
assert nested_observation.output == "<async_generator>"
assert nested_observation.output == "Hello--, async --World!"


@pytest.mark.asyncio
Expand Down