fix: add close and aclose methods to GeneratorWrapper#1607
Open
yorickvP wants to merge 3 commits intolangfuse:mainfrom
Open
fix: add close and aclose methods to GeneratorWrapper#1607yorickvP wants to merge 3 commits intolangfuse:mainfrom
yorickvP wants to merge 3 commits intolangfuse:mainfrom
Conversation
Currently, close and aclose methods are missing on GeneratorWrapper, making it impossible to close generators from client code without accessing the .generator directly, and missing span uploads in the process. This commit adds the missing methods for this usecase.
Contributor
Author
|
Addressed the P2 issue from greptile by preserving the generator context during close as well. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently, close and aclose methods are missing on GeneratorWrapper, making it impossible to close generators from client code without accessing the .generator directly, and missing span uploads in the process.
This commit adds the missing methods for this usecase.
Disclaimer: Experimental PR review
Greptile Summary
This PR fixes a gap in
_ContextPreservedSyncGeneratorWrapperand_ContextPreservedAsyncGeneratorWrapperwhere theclose()/aclose()methods were absent, forcing callers to reach into.generatordirectly and bypassing Langfuse span finalisation. The change also refactors the duplicated span-teardown logic into a shared_end_span()helper protected by an_endedidempotency flag, which is a clean improvement.Key changes:
_ended: boolflag and_end_span()helper to both wrappers, eliminating the duplicated span-finalisation block that previously lived inline in__next__/__anext__.close()to_ContextPreservedSyncGeneratorWrapperandaclose()to_ContextPreservedAsyncGeneratorWrapper, each calling_end_span()before delegating to the underlying generator._end_span()first, then generator teardown) is intentional — the span is finalized with the items collected so far, even if generator cleanup later raises.__next__runs the generator viaself.context.run()/asyncio.create_task(..., context=…)to preservecontextvars, but the newclose()/aclose()delegate to the underlying generator directly, so anyfinallyblocks in the generator that read context variables will observe the caller's context rather than the preserved one.Confidence Score: 5/5
_endedguard prevents double-finalisation. No P0 or P1 issues found.langfuse/_client/observe.py.Important Files Changed
close()/aclose()to sync/async generator wrappers with a shared_end_span()helper and idempotency guard; minor P2 gap where generator teardown doesn't run inside the preserved context, unlike__next__/__anext__.Sequence Diagram
sequenceDiagram participant Client participant SyncWrapper as _ContextPreservedSyncGeneratorWrapper participant Generator Client->>SyncWrapper: close() SyncWrapper->>SyncWrapper: _end_span() [idempotent via _ended flag] SyncWrapper->>SyncWrapper: span.update(output=items).end() SyncWrapper->>Generator: generator.close() Generator-->>SyncWrapper: (GeneratorExit injected) SyncWrapper-->>Client: returns note over SyncWrapper: __next__ exhaustion or error<br/>also calls _end_span() — _ended<br/>flag prevents double-finalisationReviews (1): Last reviewed commit: "fix: add _end_span method to prevent dou..." | Re-trigger Greptile
(5/5) You can turn off certain types of comments like style here!