How to do asynchronous calls with the latest api version?

Hi All,

How do we now handle asynchronous calls to the API now that acreate has been removed?

previously I could do this.

response = await openai.ChatCompletion.acreate

After the update, to call the chat completion API you’d use

response = client.chat.completions.create

I tried searching for acreate or asynchronous on the docs sites and there are no results, even for legacy.

Has asynchronous calls been removed?

Thanks,

Maybe below code is the replacement i have not tried yet though but found on github

from openai import AsyncOpenAI
client = AsyncOpenAI()

response = await client.chat.completions.create(
            model="gpt-4",
            messages=messages,
            tools=functions,
            temperature=0.0,
            tool_choice=None
        )
5 Likes

Thanks @altaf

This worked. For those that come across this thread, you will also need to update how you handle the responses, as objects are now returned.

Something like this:

From:

response.choices[0].message["content"]

To:

response.choices[0].message.content

4 Likes

Hello @madvaultllc Can you help, How did you use this code, If I am trying to use await client.chat.completions.create, and client = AsyncOpenAI() , I am getting error for cannot import name ‘AsyncOpenAI’ from ‘openai’. Can you Please help, how did it work for you! I will really appreciate your help!

Check your version of the openai library you’re importing. If it is too old then it won’t have the new client libraries available. It’s best to uninstall and re-install the latest.