#Can't create thread while run is active. What's the workaround?
1 messages · Page 1 of 1 (latest)
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).