Repository navigation
[CB-0] [sdk-superfan] Defer async streaming request until the context manager is entered - #218
Draft
spicysouvlaki wants to merge 1 commit into
Draft
spicysouvlaki wants to merge 1 commit into
spicysouvlaki wants to merge 1 commit into
Conversation
…is entered `async_to_streamed_response_wrapper` and `async_to_custom_streamed_response_wrapper` called the endpoint eagerly (`func(*args, **kwargs)`) and handed the resulting coroutine to `AsyncResponseContextManager`. A coroutine body doesn't run until awaited, so no request was sent, but a manager that is never entered drops an unawaited coroutine and Python emits `RuntimeWarning: coroutine 'AsyncSessionsResource.create' was never awaited` on GC. The repo's pytest config sets `filterwarnings = ["error"]`, and so do many downstream suites. The sync side already stores `functools.partial(func, ...)` and calls it in `__enter__`. Do the same for async: the manager takes a zero-arg callable returning an awaitable and calls it in `__aenter__`. Side effect: the async manager can now be re-entered like the sync one instead of failing with "cannot reuse already awaited coroutine". Regression test in tests/test_response.py builds a streaming manager without entering it, forces GC and asserts no warnings. It fails on origin/main with the exact warning and passes with this change. Also checked live against api.dev.browserbase.com: `async with sessions.with_streaming_response.list(...)` returns 200 under `-W error`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
What happened
#175: creating an async
.with_streaming_responsemanager and never entering it emitsRuntimeWarning: coroutine 'AsyncSessionsResource.create' was never awaitedWhy
src/browserbase/_response.py:683and:733call the endpoint eagerly and pass the coroutine toAsyncResponseContextManager(:632-637). The sync wrappers (:662,:708) pass afunctools.partialand call it on enter.Fix
AsyncResponseContextManagertakes a zero-arg callable and calls it in__aenter__. Both async wrappers passfunctools.partial, the same as the sync ones.Tests
test_async_streamed_response_not_entered. Before: fails with the warning above. After: passes../scripts/lintpasses../scripts/test(steady mock) passes on 3.9 and 3.14 with pydantic v1 and v2.async with sessions.with_streaming_response.list(...)against the dev API returns 200 under-W error.Risk and rollout
Edits the generated
_response.pyandtests/test_response.py. Stainless seals both, so a template change near these lines may conflict. Behavior change: the request now starts on__aenter__, which is already how the sync manager works.AsyncResponseContextManageris private, and nothing else constructs it.Fixes #175
🤖 Generated with Claude Code