What do I do after using submitToolOutputs to send data back to the run

I’m working on integrating OpenAI’s GPT-4 into a node.js application using the official OpenAI npm package. My application includes an assistant that can retrieve a list of countries using a vector database. I’m currently able to create a thread, add messages, and start a streaming run. However, I’m facing issues with handling responses after submitting tool outputs.

Here are the relevant steps and code snippets:

  1. Create a thread:
    const thread = await openai.beta.threads.create();

  2. Add a Message:

await openai.beta.threads.messages.create(req.session.threadId, {
    role: "user",
    content: req.body.message,
});
  1. Start a Streaming Run:
const stream = await openai.beta.threads.runs.create(req.session.threadId, {
    assistant_id: assistantId,
    stream: true,
});
  1. Handle Tool Output Submission:
for await (const event of stream) {
    if (event.event === "thread.run.requires_action") {
        const toolCalls = event.data.required_action.submit_tool_outputs.tool_calls;
        for (const toolCall of toolCalls) {
             if (toolCall.function.name === "searchCountries") { 
                  await openai.beta.threads.runs.submitToolOutputs(
                    event.data.thread_id,
                    runId,
                    "Australia, Japan, China", // result is actually calculated, hard coded for brevity
                 );
             }
        }
    } 
}

My question is: What should I do after executing await openai.beta.threads.runs.submitToolOutputs?

Specifically, I want to know if there’s another event that I should listen for to obtain and send back the results of the run to the frontend. Previously, I handled this with the following code, but it seems outdated according to the latest documentation:

.on("messageDone", async (event) => {
    if (event.role === "assistant" && event.content) {
        res.json({ aiResponse: event.content[0].text }); // sends message back to the front end
    } else {
        res.status(500).send("No valid response from the AI.");
    }
})

I can’t find the equivalent of messageDone or understand how to properly finalize and send the response once the run is complete using the new API structure. Any guidance on how to handle this with the current OpenAI API would be greatly appreciated.

So basically we have two things here

  1. if status is complete
  2. if we need to do require action

Second point is after implementing all code and add the submitToolOutputs function you dont need to worry about the response because after the response will done from submitToolOutputs and status is complete then it will goto the status complete if and then the response will show.


here you can check make sure too add set interval so that the function will loop until we will find the status equal to complete

There is a better way, per documentation buried here:
https://platform.openai.com/docs/assistants/tools/function-calling/quickstart?context=without-streaming
submitToolOutputsAndPoll() can be used to wait until the run reaches a terminal state - which means you can await it and then do a simple if(run.status === x) after completion.

2 Likes

Yes! That was it, thank you! They have to stop burrying stuff!!

Unrelated to your original question, but did you need to stream thread responses from your server to a client? If so, how? I’ve been able to receive the stream from the API to my server, but I haven’t been able to stream the deltas to the client as they’re received on the server – the entire response shows up at once on the client.