Async usage of the Assistant APIs

I’m trying to use OpenAI in asyncronous mode via Python’s Asyncio.
However when use “await” with the Open AI API calls,

Run = await openai.beta.threads.runs.create_and_poll(
thread_id=MyThreadId,
assistant_id=AssId
)

…I’m returned the error

“TypeError: object Run can’t be used in ‘await’ expression”.

My understanding was that by importing

from openai import AsyncOpenAI

…and using

client = AsyncOpenAI

…it was sufficient to simply use “await” before the API call

Can anybody give me some adivce? Thanks in advance.

1 Like

Did you make an oopsie and call the client openai instead in that line? That would be just the global instance of the library.

If you like asyncio, I just scripted a performance test to see the fastest complete response you can get out of a new assistant and thread without streaming; using a polling interval that would burn through your API call limit (why streaming is better).

Starting chatbot session…
Thread thread_uIAQnVCtxhixiaGfogNqwU17 created in 0.49 seconds
Assistant asst_SGAhmUIskfaqeAs3FPUaE2EQ created in 0.53 seconds
Starting create_and_poll after 0.53 seconds
create_and_poll completed in 3.04 seconds
newest message: No, I’m not human. I’m an AI, a computer program designed to assist you by providing information and answering questions. How can I help you today?
Paused. Press Enter to delete the thread and assistant created…

Demo async code written to not actually be useful
from openai import AsyncOpenAI
import asyncio
import time

client = AsyncOpenAI(timeout=60.0, max_retries=0)

async def create_assistant(instructions: str, name: str, model: str, tim: float):
    """Create assistant asynchronously and print time when done."""
    assistant = await client.beta.assistants.create(
        instructions=instructions,
        name=name, 
        model=model
    )
    print(f"Assistant {assistant.id} created in {time.time() - tim:.2f} seconds")
    return assistant

async def create_thread(content: str, tim: float):
    """Create thread asynchronously and print time when done."""
    thread = await client.beta.threads.create(
        messages=[{"role": "user", "content": content}]
    )
    print(f"Thread {thread.id} created in {time.time() - tim:.2f} seconds")
    return thread

async def get_latest_message(thread_id: str) -> str:
    """Retrieve the latest message from the thread, only text responses."""
    try:
        messages = await client.beta.threads.messages.list(thread_id=thread_id, limit=1)
        new_message = messages.data[0].content[0].text.value
        return new_message
    except Exception as e:
        return f"Error retrieving message: {e}"

async def cleanup_resources(thread_id: str, assistant_id: str) -> None:
    """Delete thread and assistant in parallel."""
    await asyncio.gather(
        client.beta.threads.delete(thread_id),
        client.beta.assistants.delete(assistant_id)
    )

async def run_chatbot_session(instructions: str, name: str, model: str, content: str):
    """Create assistant, thread, poll for result, retrieve message, and cleanup."""
    tim = time.time()  # Start time tracking

    assistant, thread = await asyncio.gather(  # Creating of both can be parallel
        create_assistant(instructions, name, model, tim),
        create_thread(content, tim)
    )

    # Run the thread and poll for result once assistant and thread are created
    print(f"Starting create_and_poll after {time.time() - tim:.2f} seconds")
    run = await client.beta.threads.runs.create_and_poll(
        thread_id=thread.id,
        assistant_id=assistant.id,
        poll_interval_ms=2000
    )
    print(f"create_and_poll completed in {time.time() - tim:.2f} seconds")

    # After the run is completed, retrieve the latest message
    new_message = await get_latest_message(thread.id)
    print(f"newest message: {new_message}")

    return thread.id, assistant.id

async def main():
    """Main function that handles user inputs and manages async flow."""
    # User-supplied values
    instructions = "You are an AI friend."
    model = "gpt-4o"
    name = "example_bot"
    content = "Are you human?"

    print("Starting chatbot session...")

    # Run the chatbot session with provided parameters
    thread_id, assistant_id = await run_chatbot_session(instructions, name, model, content)

    input("Paused. Press Enter to delete the thread and assistant created...")

    # Clean up resources (threads, assistant)
    await cleanup_resources(thread_id, assistant_id)

if __name__ == "__main__":
    asyncio.run(main())
3 Likes

Hi Jay, thank you, you solved the issue. The “await openai.beta.” was not an oopsie: all the API calls in my program are written in this way, but it was working fine in synchronous mode.

Of course the declaration “client = AsyncOpenAI” is totally useless if I call the APIs with “openai.beta.”

For this reason OpenAI was returning that error: I was trying to call asnchornous APIs using the synchronous client…

Now I changed my calls to “await client.beta.” and everything works perfectly…

How silly of me, I’m clearly a neewbie…
Thank you a lot for your help! I owe you one.

2 Likes

Hi @mauro.biscotti and welcome to the community!

Is it ok if you mark @_j 's post as a solution? It may help others who come across the same problem? Thanks!

1 Like

Sure! Apologies for not having done it already. Best regards.

2 Likes