BadRequestError: 400 Invalid 'thread_id': 'undefined'. Expected an ID that begins with 'thread'

I am getting error in the api for retrieving the run:
await openai.beta.threads.runs.retrieve(currentThreadId, run.id);
Here is my code:

        console.log(`Backend Debug: Polling for run status on thread ID: ${currentThreadId}, run ID: ${run.id}`);
        if (!currentThreadId || typeof currentThreadId !== 'string' || !currentThreadId.startsWith('thread_')) {
          throw new Error(`Invalid thread ID: ${currentThreadId}`);
        }

        let runStatus = await openai.beta.threads.runs.retrieve(currentThreadId, run.id);
        console.log(runStatus.status)
        while (runStatus.status !== 'completed') {
          if (['failed', 'cancelled', 'expired'].includes(runStatus.status)) {
            throw new Error(`Run ended with status: ${runStatus.status}`);
          }
          await new Promise(resolve => setTimeout(resolve, 1000));
          runStatus = await openai.beta.threads.runs.retrieve(thread_id = currentThreadId, run.id);
        }

EDIT: My code runs fine now, referring to the runs.mjs in openai module:

    retrieve(runID, params, options) {
        const { thread_id } = params;
        return this._client.get(path `/threads/${thread_id}/runs/${runID}`, {
            ...options,
            headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
        });
    }

This expects a named thread_id param, so changed my code to:

openai.beta.threads.runs.retrieve(run.id, { thread_id : currentThreadId})

I’ve been running into the same issue since yesterday. I haven’t changed anything in my code, and there’s no new version of the OpenAI SDK out. Weird.

I have a deployed version of same code, that still runs, let me know if you find out something. I also tried changing API key, taking reference from other posts, but didn’t work for me.
The thread create, list messages, all of these run fine.

It looks like there’s a weird internal glitch with the OpenAI SDK. We’re literally following the documentation step-by-step:

https://platform.openai.com/docs/api-reference/runs/getRun?lang=curl

The strange part is that only the retrieve run function is acting up—everything else is working fine.

Here’s the code below that will get the job done:

        // let runStatus = await openai.beta.threads.runs.retrieve(currentThreadId, run.id);`
        let runStatus = await fetch(`https://api.openai.com/v1/threads/${currentThreadId}/runs/${run.id}`, {
          headers: {
            'Authorization': `Bearer ${OPENAI_API_KEY}`,
            'OpenAI-Beta': 'assistants=v2'
          }
        }).then(res => res.json());
1 Like