Skip to content

Commit dc09f50

Browse files
authored
feat(observe): handle starlette.StreamingResponse (#1394)
Starlette has a StreamingResponse type that wraps an AsyncGenerator, which is often used in fastapi http calls. By wrapping the result inside of it, we can get the correct timings and outputs for these generators in the resulting trace.
1 parent 496aca3 commit dc09f50

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

langfuse/_client/observe.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,16 @@ async def async_wrapper(*args: Tuple[Any], **kwargs: Dict[str, Any]) -> Any:
312312
transform_to_string,
313313
)
314314

315+
# handle starlette.StreamingResponse
316+
if type(result).__name__ == "StreamingResponse" and hasattr(result, "body_iterator"):
317+
is_return_type_generator = True
318+
319+
result.body_iterator = self._wrap_async_generator_result(
320+
langfuse_span_or_generation,
321+
result.body_iterator,
322+
transform_to_string,
323+
)
324+
315325
langfuse_span_or_generation.update(output=result)
316326

317327
return result
@@ -416,6 +426,16 @@ def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
416426
transform_to_string,
417427
)
418428

429+
# handle starlette.StreamingResponse
430+
if type(result).__name__ == "StreamingResponse" and hasattr(result, "body_iterator"):
431+
is_return_type_generator = True
432+
433+
result.body_iterator = self._wrap_async_generator_result(
434+
langfuse_span_or_generation,
435+
result.body_iterator,
436+
transform_to_string,
437+
)
438+
419439
langfuse_span_or_generation.update(output=result)
420440

421441
return result

0 commit comments

Comments
 (0)