Confirm this is an issue with the Python library and not an underlying OpenAI API
[X] This is an issue with the Python library
Describe the bug
When using the OpenAI Python library (version 1.52.2), I encountered an issue where AnyIO worker threads are not terminating properly after asynchronous requests. This leads to tests hanging and not exiting when using pytest. The problem occurs because the library utilizes asyncify(get_platform) to call the synchronous get_platform function in an asynchronous context, causing AnyIO to create non-daemon worker threads that remain alive after the request completes.
To Reproduce
Use the OpenAI Python library in an asynchronous environment.
Make an asynchronous API request that triggers the _request method.
Run tests using pytest.
Observe that after the tests complete, the process hangs and does not exit due to lingering AnyIO worker threads.
Code snippets
The issue arises in the _request method where asyncify is used:
In openai/_openai_client.py
async def _request(
self,
cast_to: Type[ResponseT],
options: FinalRequestOptions,
*,
stream: bool,
stream_cls: type[_AsyncStreamT] | None,
retries_taken: int,
) -> ResponseT | _AsyncStreamT:
if self._platform is None:
# `get_platform` can make blocking IO calls so we
# execute it earlier while we are in an async context
self._platform = await asyncify(get_platform)()
# ... rest of the method ...
Expected behavior
The AnyIO worker threads should terminate after completing their tasks, allowing the program or tests to exit cleanly without hanging.
### OS
macOS
### Python version
Python 3.12.4
### Library version
1.52.2
Confirm this is an issue with the Python library and not an underlying OpenAI API
Describe the bug
When using the OpenAI Python library (version 1.52.2), I encountered an issue where AnyIO worker threads are not terminating properly after asynchronous requests. This leads to tests hanging and not exiting when using
pytest
. The problem occurs because the library utilizesasyncify(get_platform)
to call the synchronousget_platform
function in an asynchronous context, causing AnyIO to create non-daemon worker threads that remain alive after the request completes.To Reproduce
_request
method.pytest
.Code snippets
The issue arises in the _request method where asyncify is used:
In openai/_openai_client.py
Expected behavior
The AnyIO worker threads should terminate after completing their tasks, allowing the program or tests to exit cleanly without hanging.