Why is the order on Assistants API messages backwards?

I’m using the Assistants API in Node.js but I’m confused as to the order of the messages I get back when i retrieve messages using openai.beta.threads.messages.list(). When I ask one question and retrieve messages, the first item in the array is the Assistant response. This doesn’t make sense to me because then when I loop through the messages to render them in my app, I see:

Assistant Response 1: "Hello! I'm here to assist you. How can I help you today?"
User Message 1: "hey what's up"

When it should show the assistant response afterwards like this:

User Message 1: "hey what's up"
Assistant Response 1: "Hello! I'm here to assist you. How can I help you today?"

I know I can simply reverse the array to get rid of this issue, but I don’t understand why nobody has brought it up. Wouldn’t this be an issue, since the index of the first message created in the thread will always be changing?

I would really appreciate if someone could help me understand this. Thanks!

Hi!. I’m facing the same issue. I’ve made a simple assistant to help with coding problems and when I ask it a simple question such as “Explain DFS with an example”, The responses it returns are in reverse order. The first message contains the graph explanation, the second contains the graph plot from matplotlib, and the third contains the explanation to introduction to the concept of DFS. It should have been reversed. For now I’m simply reversing the list to solve my problem, although idk why this is happening.


the API method has a parameter:

order - default “desc”
Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.

1 Like

Yep that fixed the problem. Thanks for the response!.

Thank you so much for this, that worked! On a bit of a side note, I have a question regarding the process of adding messages to threads.

Since you have to make an API request to add a message and then list the messages in the thread, there will inevitably be a delay in updating the messages UI after you send the message. How are people getting around this?

1 Like

By waiting.

The whole process of starting a run, the AI autonomously calling knowledge functions or running python, etc, takes time with no feedback unless you also scrape run steps. You have to keep polling to see if the status has changed. Then go get your complete assistant message or more tool the AI wants you to run yourself.

It is not a satisfying experience for end users.

I did a few things, first when the user sends a message to the chatbot, I automatically post that direct user input to the chat window without listing the messages from the thread so that it is instantly visible.

Then, during the “waiting” time for polling the run status, I had an animated icon display like you would in a meta messenger or text message window showing that something is happening. I also have the chat window set to show different icons and messages if functions are called such as reading a file, analyzing an image, etc. Each function has its own animated icon and messaging so even though there may be a delay before the run status is “Complete” and I can display the assistant response, they can visibly see something is happening. Very similar to the way ChatGPT does it.

1 Like