-
Notifications
You must be signed in to change notification settings - Fork 135
trace is lost in spans coming from async generators #1183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
nice catch! Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
Issue
In the current version of SDK @observe loses context/trace id for spans that are coming from async generators and nested async generators (when observed async generator is called in another observed async generator). Such spans appear not on the current trace but added as a new trace and are displayed separately in the UI. The previous implementation have been prematurely removing observations from the stack/not properly restoring the stack state during async generator iteration.
Impact
My changes improve the reliability of observation tracking when using async generators with the
@observe
decorator. This is particularly important for applications that use async generators extensively, such as streaming responses from language models or other async data processing pipelines.The fix ensures that observations are properly tracked and that the observation stack is maintained in a consistent state throughout the lifecycle of async generators.
Note
Tested for 2 weeks in-house
Important
Fixes context/trace ID loss in async generators with
@observe
by managing observation stack correctly inlangfuse_decorator.py
.@observe
decorator inlangfuse_decorator.py
._wrap_async_generator_result()
to manage observation stack correctly by setting and restoring stack state aroundanext(generator)
calls._finalize_call()
related to stack management.This description was created by
for 8fa20d6. You can customize this summary. It will automatically update as commits are pushed.
Greptile Summary
Disclaimer: Experimental PR review
Fixes context/trace ID loss in async generators when using the @observe decorator by properly maintaining observation stack state during async generator iteration.
_wrap_async_generator_result
inlangfuse/decorators/langfuse_decorator.py
to preserve observation stack between generator yieldsanext
calls to maintain parent-child relationships💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!