From 7cd9dc878f63f09e95f88c584dafa4b9046f284a Mon Sep 17 00:00:00 2001 From: Danny McCormick Date: Fri, 30 May 2025 09:24:07 -0400 Subject: [PATCH 1/2] Add info on garbage collection of async clients --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b83cb47c74..3c38e3e135 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,26 @@ async def main() -> None: asyncio.run(main()) ``` -Functionality between the synchronous and asynchronous clients is otherwise identical. +Functionality between the synchronous and asynchronous clients is otherwise mostly identical. + +### Async Garbage Collection + +When opening multiple `AsyncOpenAI` clients, it is recommended to use `with` blocks to handle +resource management instead of relying on traditional garbage collection. Failing to do this +may result in leaked connections and degraded performance over time since the async event loop +can be cleaned up before the `AsyncOpenAI` client has a chance to clean up its resources. + +``` +async def callModel(prompt) -> None: + async with getAsyncVLLMClient(api_key=os.environ.get("OPENAI_API_KEY")) as client: + response = await client.responses.create( + model="gpt-4o", input=prompt + ) + +prompts = [...] +for prompt in prompts: + asyncio.run(callModel(prompt)) +``` ## Streaming responses From 709862d746a1f8c7e24cc2e116fc5a03026bb5db Mon Sep 17 00:00:00 2001 From: Marcus Wood Date: Tue, 15 Sep 2026 17:56:11 +0000 Subject: [PATCH 2/2] docs: clarify async client cleanup --- README.md | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 4142542b6a..40ad4c1ae6 100644 --- a/README.md +++ b/README.md @@ -299,26 +299,7 @@ async def main() -> None: asyncio.run(main()) ``` -Functionality between the synchronous and asynchronous clients is otherwise mostly identical. - -### Async Garbage Collection - -When opening multiple `AsyncOpenAI` clients, it is recommended to use `with` blocks to handle -resource management instead of relying on traditional garbage collection. Failing to do this -may result in leaked connections and degraded performance over time since the async event loop -can be cleaned up before the `AsyncOpenAI` client has a chance to clean up its resources. - -``` -async def callModel(prompt) -> None: - async with getAsyncVLLMClient(api_key=os.environ.get("OPENAI_API_KEY")) as client: - response = await client.responses.create( - model="gpt-4o", input=prompt - ) - -prompts = [...] -for prompt in prompts: - asyncio.run(callModel(prompt)) -``` +Functionality between the synchronous and asynchronous clients is otherwise identical. ### With aiohttp @@ -1091,6 +1072,25 @@ with OpenAI() as client: # HTTP client is now closed ``` +For `AsyncOpenAI`, use `async with` or `await client.close()` before shutting down +the event loop. Garbage collection cannot reliably await asynchronous cleanup. + +```py +import asyncio +from openai import AsyncOpenAI + + +async def main() -> None: + async with AsyncOpenAI() as client: + response = await client.responses.create( + model="gpt-5.5", input="Say this is a test" + ) + print(response.output_text) + + +asyncio.run(main()) +``` + ## Microsoft Azure OpenAI To use this library with [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview), use the `AzureOpenAI`