Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
Loading
from

Conversation

chapati
Copy link

@chapati chapati commented May 14, 2025

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 in langfuse_decorator.py.

  • Behavior:
    • Fixes context/trace ID loss in async generators with @observe decorator in langfuse_decorator.py.
    • Ensures observations are properly tracked and stack is consistently maintained during async generator iteration.
  • Functions:
    • Modifies _wrap_async_generator_result() to manage observation stack correctly by setting and restoring stack state around anext(generator) calls.
    • Removes commented-out code in _finalize_call() related to stack management.

This description was created by Ellipsis 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.

  • Modified _wrap_async_generator_result in langfuse/decorators/langfuse_decorator.py to preserve observation stack between generator yields
  • Added proper context restoration during anext calls to maintain parent-child relationships
  • Fixed spans appearing as separate traces in UI by preventing premature observation stack removal

💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!

@CLAassistant
Copy link

CLAassistant commented May 14, 2025

CLA assistant check
All committers have signed the CLA.

Copy link

@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.

LGTM

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

Copy link

@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.

LGTM

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

langfuse/decorators/langfuse_decorator.py Outdated Show resolved Hide resolved
langfuse/decorators/langfuse_decorator.py Outdated Show resolved Hide resolved
chapati and others added 2 commits May 14, 2025 02:30
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>
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.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.