Minimum time between .create() and .retrieve() calling assistant over API?

Hello,

I experiment with using Assistant API in python and noticed, that there is a minimum time required between calling

run = client.beta.threads.runs.create()

and

run = client.beta.threads.runs.retrieve()

I tested the time limit and it seems like if the time bewteen calling both functions is <5s, the run status of the retrieve function will stick to ‘in progress’.

I now wanted to now if someone has experienced a simillar issue or if anyone has an idea if there is a possibility to set a condition to execute the retrieve function.

In the following there is a sample of my code:

for i in range(len(list)):

    message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Test Request" 
    )

    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id,
    )
    j = 0
    while run.status != 'queued':
        if j > 5:
            print("Message Status: ", run.status, "\nRequest timed out move on to next news article")
            break
        print("Message Status: ", run.status)
        time.sleep(1)
        j += 1
    
    #critical! Otherwise retrive would stick to 'in_progress'!
    time.sleep(5)

    run = client.beta.threads.runs.retrieve(
        thread_id=thread.id,
        run_id=run.id,
    )
    j = 0
    while run.status != 'completed':
        if j > 10:
            print("Message Status: ", run.status, "\nRequest timed out move on to next news article")
            break
        print("Message Status: ", run.status)
        time.sleep(1)
        j += 1

I apreciate any useful information and help.

Hi! I am using a construction like:

reply_ready = False
while not reply_ready:
    run = llm.retreive_run(thread_id,run_id)
    if run.status == 'completed':
        reply_ready = True

to check if it’s time to extract the messages of the assistant.
Also wondering what is the best practice.

in the cookbook they posted Assistants API Overview (Python SDK) | OpenAI Cookbook

they just do a while loop and sleep .5 secs

def wait_on_run(run, thread):
    while run.status == "queued" or run.status == "in_progress":
        run = client.beta.threads.runs.retrieve(
            thread_id=thread.id,
            run_id=run.id,
        )
        time.sleep(0.5)
    return run

But i find that wait 2s per loop since usual

1 Like

New thing i did here is to wait for 5s before retrieving status for the first time, as in the first loop we know it won’t be completed so I waited 5s to check for the first time, also to mention, in small prompts cases, the first check happens to be completed case! :wink:

  async function checkStatus() {
    await new Promise((resolve) => setTimeout(resolve, 5000));

    while (true) {
      const runStatus = await openai.beta.threads.runs.retrieve(
        thread.id,
        run.id
      );

      if (runStatus.status === "completed") {
        break;
      }

      await new Promise((resolve) => setTimeout(resolve, 5000));
    }
  }

  await checkStatus();