Skip to content

[CB-0] [sdk-superfan] Defer async streaming request until the context manager is entered - #218

Draft
spicysouvlaki wants to merge 1 commit into
mainfrom
wschmitt/async-streaming-lazy-request
Draft

spicysouvlaki wants to merge 1 commit into
mainfrom
wschmitt/async-streaming-lazy-request

Conversation

@spicysouvlaki

@spicysouvlaki spicysouvlaki commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

What happened

#175: creating an async .with_streaming_response manager and never entering it emits
RuntimeWarning: coroutine 'AsyncSessionsResource.create' was never awaited

Why

src/browserbase/_response.py:683 and :733 call the endpoint eagerly and pass the coroutine to AsyncResponseContextManager (:632-637). The sync wrappers (:662, :708) pass a functools.partial and call it on enter.

Fix

AsyncResponseContextManager takes a zero-arg callable and calls it in __aenter__. Both async wrappers pass functools.partial, the same as the sync ones.

Tests

  • Added test_async_streamed_response_not_entered. Before: fails with the warning above. After: passes.
  • ./scripts/lint passes. ./scripts/test (steady mock) passes on 3.9 and 3.14 with pydantic v1 and v2.
  • Live: async with sessions.with_streaming_response.list(...) against the dev API returns 200 under -W error.

Risk and rollout

Edits the generated _response.py and tests/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. AsyncResponseContextManager is private, and nothing else constructs it.

Fixes #175

🤖 Generated with Claude Code

…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

No deployments
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.

Async streaming response wrappers leak an unawaited coroutine when not entered

1 participant