#Can't create thread while run is active. What's the workaround?

1 messages · Page 1 of 1 (latest)

viscid forum
#

Hi,
I'm getting this issue:
"Can't add messages to thread_OdKj8BSMFzZqSNwRthNCE71m while a run run_R3yMgahfV2SCesGFter8V88I is active."

What is the proper way to fix it as I thought you need a thread and run to pass message into to begin with

grizzled geyserBOT
#

Hi @viscid forum. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error message you're encountering is due to the fact that when a run is in_progress and not in a terminal state, the thread is locked. This means that new messages cannot be added to the thread and new runs cannot be created on the thread source (https://platform.openai.com/docs/assistants/how-it-works).

To work around this, you need to ensure that the run is completed before adding new messages to the thread or creating new runs. Here's a Python code snippet from the knowledge sources that demonstrates this:

while runInfo.status != "completed":
    runInfo =  await client.beta.threads.runs.retrieve(
        thread_id=thread.id,
        run_id=run.id
    )

In this code, the program waits until the run status is completed before proceeding.

Also, you can create the message before creating a run. Here's a JavaScript code snippet that demonstrates this:

const thread = (await openai.beta.threads.create());
const message = await openai.beta.threads.messages.create(
    thread.id,
    { role: "user", content: userQuery }
);
const run = await openai.beta.threads.runs.create(thread.id, {assistant_id : assistantId});

In this code, the message is created before the run is created source (https://community.openai.com/t/513943).

Remember to handle the case where the run status is not completed after the loop, as it might be in an error state.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: