[BUG] OpenAIResponsesModel.stream() leaves the OpenAI AsyncStream open after response.completed, so asyncio logs "aclose(): asynchronous generator is already running"
Human Overview
OpenAIResponsesModel.stream() exits its async for event in response loop with break on response.completed (and on response.incomplete) without closing the AsyncStream that client.responses.create(...) returned. The underlying httpx2/httpcore2 byte-stream async generators stay suspended. When the event loop shuts down, loop.shutdown_asyncgens() tries to close them and asyncio's exception handler logs an error with a traceback.
The model output is complete and the per-request AsyncOpenAI client is closed by async with, so nothing is functionally broken. But every process that makes more than one OpenAI Responses call prints tracebacks to stderr, which looks like a crash to users and drowns real errors.
Checks
- [x] I have updated to the lastest minor and patch version of Strands
- [x] I have checked the documentation and this is not expected behavior
- [x] I have searched ./issues and there are no duplicates of my issue (closest: #5991, see Related Issues)
SDK Language
Python
Strands Version
1.65.0 (also reproduced on 1.64.0)
Language Runtime Version
Python 3.12.12
Operating System
Linux
Installation Method
pip
Steps to Reproduce
Requires an OpenAI API key and any Responses API model.
pip install strands-agents[openai]==1.65.0(openai 3.51.0, httpx2 2.16.0, httpcore2 2.16.0)- Run:
import asyncio
from strands import Agent
from strands.models.openai_responses import OpenAIResponsesModel
async def main() -> None:
for _ in range(4):
agent = Agent(
model=OpenAIResponsesModel(model_id="<any Responses API model>"),
system_prompt="Reply with exactly: OK",
callback_handler=None,
)
print(await agent.invoke_async("ping"))
asyncio.run(main())
- Watch stderr. It also reproduces with sync
agent(...)calls (a fresh event loop per call). The exact point it appears depends on GC timing.
Expected Behavior
No asyncio errors. The response stream should be closed deterministically once the model has finished with it.
Actual Behavior
The replies are correct, but asyncio logs an error with a traceback to stderr (see Additional Context). Nothing is functionally broken: output is complete and the per-request AsyncOpenAI client is closed by async with. But any process making more than one OpenAI Responses call prints tracebacks that look like a crash and drown real errors.
Cause: in strands/models/openai_responses.py, OpenAIResponsesModel.stream() leaves its async for event in response loop with break on response.completed (and response.incomplete) without closing the openai.AsyncStream returned by client.responses.create(...). async for does not close the iterator on break, and closing the client does not finalize the stream's generators. The httpx2/httpcore2 byte-stream async generators stay suspended, and loop.shutdown_asyncgens() later fails to close them.
Possible Solution
openai.AsyncStream supports the async context manager protocol and has close(), so close it deterministically:
response = await client.responses.create(**request)
async with response:
async for event in response:
...
Alternatively, wrap the loop in try: ... finally: await response.close(). Other paths in the same module that iterate an AsyncStream and can exit early may need the same treatment.
Additional Context
Extended context (optional)
Abridged asyncio output:
an error occurred during closing of asynchronous generator <async_generator object HTTP11ConnectionByteStream.__aiter__ at 0x...>
asyncgen: <async_generator object HTTP11ConnectionByteStream.__aiter__ at 0x...>
Traceback (most recent call last):
File ".../httpcore2/_async/connection_pool.py", line 427, in __aiter__
yield chunk
GeneratorExit
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".../httpcore2/_async/connection_pool.py", line 425, in __aiter__
async with safe_async_iterate(self._stream) as iterator:
File ".../contextlib.py", line 386, in __aexit__
await self.thing.aclose()
RuntimeError: aclose(): asynchronous generator is already running
A similar message also appears for Response.aiter_raw.
Workaround: a narrow logging.Filter on the asyncio logger that drops this record (exact message + RuntimeError "already running" + an httpx/httpcore stream generator).
Related Issues
Related: #5991 (closed; same missing stream close, reported without the asyncio symptom). A fix was attempted in #5999 (closed unmerged because it was unclear what problem it solved). This issue supplies that missing motivation. I have not reviewed #5999's code.
I can reproduce this on c8e1351a5a6218c8f5c2417534aedbd021882a76 (current main): OpenAIResponsesModel.stream() breaks on response.completed / response.incomplete without closing the OpenAI AsyncStream. I am working on a fix with a unit test that asserts the stream is closed.