From d1231d0fa250dd044ba1e0ae4a0f8cf0315948f3 Mon Sep 17 00:00:00 2001 From: William Schmitt Date: Mon, 16 Nov 2026 13:21:51 -0800 Subject: [PATCH] fix(client): defer async streaming request until the context manager 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 --- src/browserbase/_response.py | 14 +++++++------- tests/test_response.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/browserbase/_response.py b/src/browserbase/_response.py index eeef6426..9016da73 100644 --- a/src/browserbase/_response.py +++ b/src/browserbase/_response.py @@ -629,12 +629,12 @@ class AsyncResponseContextManager(Generic[_AsyncAPIResponseT]): when the context manager exits """ - def __init__(self, api_request: Awaitable[_AsyncAPIResponseT]) -> None: - self._api_request = api_request + def __init__(self, request_func: Callable[[], Awaitable[_AsyncAPIResponseT]]) -> None: + self._request_func = request_func self.__response: _AsyncAPIResponseT | None = None async def __aenter__(self) -> _AsyncAPIResponseT: - self.__response = await self._api_request + self.__response = await self._request_func() return self.__response async def __aexit__( @@ -680,9 +680,9 @@ def wrapped(*args: P.args, **kwargs: P.kwargs) -> AsyncResponseContextManager[As kwargs["extra_headers"] = extra_headers - make_request = func(*args, **kwargs) + make_request = functools.partial(func, *args, **kwargs) - return AsyncResponseContextManager(cast(Awaitable[AsyncAPIResponse[R]], make_request)) + return AsyncResponseContextManager(cast(Callable[[], Awaitable[AsyncAPIResponse[R]]], make_request)) return wrapped @@ -730,9 +730,9 @@ def wrapped(*args: P.args, **kwargs: P.kwargs) -> AsyncResponseContextManager[_A kwargs["extra_headers"] = extra_headers - make_request = func(*args, **kwargs) + make_request = functools.partial(func, *args, **kwargs) - return AsyncResponseContextManager(cast(Awaitable[_AsyncAPIResponseT], make_request)) + return AsyncResponseContextManager(cast(Callable[[], Awaitable[_AsyncAPIResponseT]], make_request)) return wrapped diff --git a/tests/test_response.py b/tests/test_response.py index 41e1a362..e5036fdc 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -1,4 +1,6 @@ +import gc import json +import warnings from typing import Any, List, Union, cast from typing_extensions import Annotated @@ -275,3 +277,12 @@ async def test_async_response_parse_expect_model_union_non_json_content(async_cl obj = await response.parse(to=cast(Any, Union[CustomModel, OtherModel])) assert isinstance(obj, str) assert obj == "foo" + + +async def test_async_streamed_response_not_entered(async_client: AsyncBrowserbase) -> None: + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + async_client.sessions.with_streaming_response.create() + gc.collect() + + assert [str(w.message) for w in caught] == []