Skip to content

fix(observe): wrap returned async generators#1259

Merged
hassiebp merged 1 commit intomainfrom
hassieb/lfe-5270-fix-async-gen
Jul 16, 2025
Merged

fix(observe): wrap returned async generators#1259
hassiebp merged 1 commit intomainfrom
hassieb/lfe-5270-fix-async-gen

Conversation

@hassiebp
Copy link
Copy Markdown
Contributor

@hassiebp hassiebp commented Jul 16, 2025

Important

Wraps async generators in async_wrapper and sync_wrapper in observe.py to capture and transform results, updating tests accordingly.

  • Behavior:
    • Wraps async generators in async_wrapper and sync_wrapper in observe.py to capture and transform results.
    • Updates test_async_generator_as_return_value in test_decorators.py to check transformed async generator output.
  • Functions:
    • Modifies async_wrapper and sync_wrapper to handle async generators using _wrap_async_generator_result and _wrap_sync_generator_result.
  • Tests:
    • Updates test_async_generator_as_return_value to assert transformed output of async generators.

This description was created by Ellipsis for 0b88c30. You can customize this summary. It will automatically update as commits are pushed.

Disclaimer: Experimental PR review

Greptile Summary

This PR fixes an important limitation in the Langfuse Python SDK's @observe decorator regarding how it handles asynchronous generators. Previously, when an async generator was returned from a decorated function, the observation would only record <async_generator> as the output instead of the actual yielded values. The changes implement proper wrapping and output capture for both synchronous and asynchronous generators.

Key changes:

  • Added symmetric support for both sync and async generators in the observe decorator
  • Implemented proper wrapping logic using inspect.isgenerator and inspect.isasyncgen checks
  • Modified tests to verify actual generator output is captured instead of just the generator type
  • Ensures consistent behavior between sync and async generator handling

This improvement is particularly valuable for applications that use streaming responses or process data incrementally, as it provides better observability into the actual data being generated.

Confidence score: 4/5

  1. This PR is safe to merge with normal code review attention
  2. The changes are well-contained, properly tested, and follow a clear pattern of symmetric implementation
  3. Special attention should be paid to:
    • tests/test_decorators.py to ensure all generator scenarios are covered
    • langfuse/_client/observe.py for the generator wrapping implementation

2 files reviewed, 1 comment
Edit PR Review Bot Settings | Greptile

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing changes made in this pull request

@hassiebp hassiebp merged commit cce4119 into main Jul 16, 2025
11 checks passed
@hassiebp hassiebp deleted the hassieb/lfe-5270-fix-async-gen branch July 16, 2025 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Latency incorrect with observe decorator on FastAPI route with StreamingResponse

1 participant