Loop over JSON API content field

Hei Bubblers.

I stuck a little bit. I receive a response from OpenAI in JSON format. But the response in terms of content (message) has null value. It means that the response is typing (in progress). After a while null value is changed to a message.
Question:
I need to check this value if it null - Ask again, if it is not → get message. It is kind of loop. I am trying to do that from different sided but nothing working out.

Could you please help me with that?

You are trying to use API assistant, right? what is the workflow you made until now?

Yes, I built an assistant.
I use “Get all messages” endpoint. In a response I get all the messages. I get last item and then I need to check wether content value is empty or not. Do you know how can I Loop this condition?

When using the Assistants API, you create a “run” to execute the task. Messages are created by the run so when the content is empty it means the run hasn’t finished executing.

The way to check whether it has finished is by querying the status of the Run. When it’s “completed”, you can call a refresh on your messages and redisplay them.

See OpenAi docs on Runs: https://platform.openai.com/docs/api-reference/runs/createRun

Run status does not guarantee to generate a message /response.
My question is how to loop that condition in JSON. I need to check this until it is done and return result: success /unsuccess.
Do you know how would be better to do that?

  1. Create a state (e.g. some “Yes/No”) that determines when you want to start your loop.

  2. Create a “Do every x” event workflow with a condition to only fire when the above is set to “Yes”

  3. Make your API call and retrieve the status.

  4. Do whatever you want to do next based on the result of the API call. Set the state back to “No” to save WU :slight_smile:

Run status just reflect the generating message status, if the the status is completed this means that the message is generated.

You have to check the Run status not if the messages is empty, because the status will tell you if the run finished without generating a message, if you are checking if the message is empty then you may stay checking forever while the Run finished (without generating message) without you knowing it.

I use a backend workflow, it contains an action to check the Run status, if the status is completed it moves to get the message, if the status is in_progress or queued it schedules the same backend after 2 seconds, if the status is other thing then it moves to the appropriate action