No assistant message in the thread

Sometimes even though I wait for the run to be completed there’s still no assistant reply in the thread. The only message in the thread is mine.

How do I fix it?

Here’s my code:


	let run = await openai.beta.threads.runs.create(
		thread.id,
		{ assistant_id: assistant.id }
	);

	while (run.status === "queued" || run.status === "in_progress") {
		run = await openai.beta.threads.runs.retrieve(thread.id, run.id);
	}

	const threadMessages = await openai.beta.threads.messages.list(thread.id);

	const assistantMessage = threadMessages.data.filter(record => record.role === "assistant")[0];
	if(!assistantMessage) {
		throw new Error("No assistant message");
	}

A couple of things I’d ask / look into:

  1. That while loop seems a little aggressive – try sleeping for a second after retrieving the most recent status. I could see OpenAI throttling or blocking you for pining their API too frequently.

  2. What is the status of run when you’re done? You might also be running into a “failed” or “canceled” run, which would explain the missing messages.

1 Like

Thanks for the answer! The status is “completed”.

Good point about the loop – I copied the code from their docs but you’re right.

It could be hitting some sort of a limit indeed, the same request ran perfectly fine just a few hours ago. I’ll try running it tomorrow.