#Unable to retrieve run by polling.

1 messages · Page 1 of 1 (latest)

ionic wren
#
const createRun = async (threadId: string, assistantId: string) => {
  try {
    const run = await openai.beta.threads.runs.create(threadId, {
      assistant_id: assistantId,
    });
    console.log(run);
    if (run.id) {
      return startPolling(run.id, threadId);
    }
  } catch (error) {
    console.error(error);
    return "Error while creating run.";
  }
};

const startPolling = async (runId: string, threadId: string) => {
  if (!threadId) return;
  console.log("Start polling");

  const poll = async () => {
    try {
      const response = await openai.beta.threads.runs.retrieve(threadId, runId);
      console.log(response);

      if (
        ["cancelled", "failed", "completed", "expired"].includes(
          response.status
        )
      ) {
        const completion = await openai.beta.threads.messages.list(threadId);
        clearInterval(intervalId);
        console.log(completion.data);
        return completion.data;
      }
    } catch (error) {
      console.error("Error polling run status:", error);
      clearInterval(intervalId);
      return "Error polling run status";
    }
  };

  const intervalId = setInterval(poll, 1000);

  return poll;
};

I am creating a run, and then calling the startPolling function once I have the run.id. In the startPolling func, I want to retrieve the run and check for status after every1s till status is completed or failed.

But when I am implementing the above approach, the polling func isn't working perfectly, I feel like it's only running once or not running at all and I am also getting Promise errors.

Can you help me with this? I want to create a func that retrieves the run every 1s till the sttaus of run is completed or failed.

lean quiverBOT
#

Hi @ionic wren. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the extracts, it seems like there might be a few issues with your current approach. Here are some suggestions:

  1. Check for 'queued' status: In the Forum Post: Thread.id being treated as object > Official Answer Part 2 (https://community.openai.com/t/513815), it's mentioned that you should also check for the 'queued' status. If the project is still queued after one second, your script might fail. So, you might want to include 'queued' in your list of statuses to check.

  2. Use a while loop instead of setInterval: In the same forum post, it's suggested to use a while loop to check the status instead of using setInterval. This is because the while loop will keep checking the status until it's no longer 'in_progress' or 'queued', whereas setInterval will run the function every 1 second regardless of the status.

  3. Handle Promises correctly: The error messages you're seeing might be due to unhandled Promises. Make sure you're correctly handling Promises with async/await or .then/.catch.

Here's a modified version of your startPolling function based on these suggestions:

const startPolling = async (runId: string, threadId: string) => {
  if (!threadId) return;
  console.log("Start polling");

  while (true) {
    try {
      const response = await openai.beta.threads.runs.retrieve(threadId, runId);
      console.log(response);
#

if (
["cancelled", "failed", "completed", "expired", "queued"].includes(
response.status
)
) {
const completion = await openai.beta.threads.messages.list(threadId);
console.log(completion.data);
return completion.data;
}
// Wait for 1 second before checking the status again
await new Promise(resolve => setTimeout(resolve, 1000));
} catch (error) {
console.error("Error polling run status:", error);
return "Error polling run status";
}
}
};


Please note that this is just a suggestion based on the information provided in the extracts. You might need to adjust the code to fit your specific use case.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------