Skip to content

Conversation

pgrayy
Copy link
Member

@pgrayy pgrayy commented Sep 12, 2025

Description

The OpenAI async client has an issue where making multiple requests with separate asyncio.run() calls causes the second request to fail with "Event loop is closed" error. The reason is that the client uses httpx internally, which maintains a connection pool that persists beyond individual event loops. This means connections created in the first event loop remain in the pool after that loop closes, and when the second asyncio.run() creates a new event loop, httpx tries to reuse those existing pooled connections that are still tied to the first event loop instead of the new event loop, thus causing a failure.

This is a problem for Strands as Agent.__call__ runs asyncio.run() on every invocation. We have seen this problem before with the Mistral model provider (PR).

The fix is to establish a new OpenAI client connection on every call to stream. This fits with the suggestion in openai/openai-python#1254 (comment) and the httpx docs.

Related Issues

#604

Documentation PR

N/A

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@pgrayy pgrayy marked this pull request as ready for review September 12, 2025 20:13
@pgrayy
Copy link
Member Author

pgrayy commented Sep 15, 2025

A LiteLLM integration test is failing. This is unrelated to these changes.

@pgrayy pgrayy merged commit 6ccc8e7 into strands-agents:main Sep 15, 2025
21 of 22 checks passed
This was referenced Sep 17, 2025
Unshure pushed a commit to Unshure/sdk-python that referenced this pull request Sep 24, 2025
@pgrayy pgrayy deleted the openai-connection branch October 2, 2025 18:36
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.

3 participants