Skip to content

fix(models): close OpenAI responses stream after completed/incomplete - #6161

Open
rome-xi wants to merge 1 commit into
strands-agents:mainfrom
rome-xi:fix-6160-close-openai-responses-stream
Open

rome-xi wants to merge 1 commit into
strands-agents:mainfrom
rome-xi:fix-6160-close-openai-responses-stream

Conversation

@rome-xi

@rome-xi rome-xi commented Nov 15, 2026

Copy link
Copy Markdown

OpenAIResponsesModel.stream() left the OpenAI Responses stream open when it stopped on response.completed or response.incomplete, and also when the stream raised. asyncio then logged aclose(): asynchronous generator is already running at loop shutdown.

The stream is now closed in a finally block once iteration stops. AsyncStream.close() releases the socket. A plain async iterator, which is what the older tests return, is closed with aclose(). The loop body is otherwise unchanged.

Fixes #6160

Testing

A fake async iterator records close() and aclose(). On HEAD c8e1351 the new tests failed because nothing closed the stream after response.completed, response.incomplete, or a stream error.

After the fix, hatch test tests/strands/models/test_openai_responses.py passed (230 tests). hatch fmt, ruff, and mypy were clean on the touched files. This did not call the live OpenAI API.

The Responses stream loop returned on a terminal event without closing
the AsyncStream, so shutdown logged an aclose error. Close the stream
when that loop finishes, including when it raises.

Fixes strands-agents#6160
@rome-xi
rome-xi requested a review from a team as a code owner November 15, 2026 09:37
@github-actions github-actions Bot added bug Something isn't working area-model Related to models or model providers python Pull requests that update python code labels Nov 15, 2026

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

area-model Related to models or model providers bug Something isn't working python Pull requests that update python code

Projects

None yet

1 participant