Skip to content

AsyncHttpxClientWrapper creates task in destructor -> prevents usage in other threads #2440

Open
@stippi

Description

@stippi

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

We have noticed strange exceptions being logged in the asyncio runtime about "close()" being called on an already closed client. It took us a while to understand what was going on, but it seems to all make sense now: We are creating a Python thread to do some processing which involves creating OpenAI client instances to make completion requests. When the destructor creates a task, the thread context will be the garbage collector, which is the main thread, not the thread which created the client.

To Reproduce

  1. Create a thread in python
  2. Use for example asyncio.run() to call an async function which eventually uses an OpenAI client to make a completion request.
  3. Observe strange exceptions about clients being closed that are already closed.

Code snippets

OS

any

Python version

Python 3.12

Library version

openai v1.93.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions