Issues with Assistant API + threads

anything i’m missing? each time i send message seems like it’s creating a new thread id or not sticking into 1 thread id, for that reason it’s not remembering the previous messages or the whole conversation.

async function askGPT(question) {
    try {
        conversationHistory.push({"role": "user", "content": question});
        // Retrieve the Assistant
        const myAssistant = await openai.beta.assistants.retrieve(assistantId);
        console.log(myAssistant); // For debugging

        // Create a new thread for each conversation
        const threadResponse = await openai.beta.threads.create();

        // Add a message to the thread with the user's question
        await openai.beta.threads.messages.create( ,{
            role: "user",
            content: question

        // Run the assistant to get a response
        const run = await openai.beta.threads.runs.create(
            { assistant_id:  assistantId }
        let runStatus = await openai.beta.threads.runs.retrieve(

        // Polling for run completion
        while (runStatus.status !== 'completed') {
            await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second
            runStatus = await openai.beta.threads.runs.retrieve(,;

        // Retrieve the messages after the assistant run is complete
        const messagesResponse = await openai.beta.threads.messages.list(;
        const aiMessages = => msg.role === 'assistant');

        // Assuming the last message is the assistant's response
        return aiMessages[aiMessages.length - 1].content[0].text.value;
    } catch (error) {
        console.error('Error in askGPT:', error.response ? : error);
        return 'An error occurred while processing your request.'; // Placeholder response
1 Like

If i understand the API correctly, there is no automatic thread management. So this line will always create a new thread every single time it’s called.

 // Create a new thread for each conversation
const threadResponse = await openai.beta.threads.create();

so each time you call askGPT, a new thread is created.

There does not seem to be any method/API for getting a list of created Threads, which means that you need to store the thread id on your side to a database.

So instead of always calling create(), you need to check if a thread id exists in your database, then fetch it with openai.beta.threads.retrieve(thread_id). And if it doesn’t exist, then create a new thread.


As this topic has a selected solution, closed topic.