Skip to content

Add info on garbage collection of async clients #2388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down