Handling Errors When Adding Messages to an Active Run in OpenAI

I’m encountering an issue when trying to add messages to a thread while a run is active. The error message returned is:

Can’t add messages to thread while a run is active

Even after checking the completed status to obtain an assistant response, this error persists.

What would be a better approach to prevent new messages from being added to threads during active runs? Any suggestions or best practices to handle this scenario more effectively?

By design, you cannot add messages to an ongoing thread in the middle of a run. Maybe wait for the Run to finish before appending the message and then starting a new run.

If you capture the thread’s ID, you can use that persisted thread in another run with the added message

Can you elaborate the last part?

Becausing Im getting a lot of threads stuck in cancelling state and there is nothing that I can do about it (cant cancel in cancelling state)

I am using the openai assistant now for more than 6 months, and this evening I have for the first time the exact same problem:
Can’t add messages to thread while a run is active

When trying to manually cancel all 3 runs for that thread, I get:

  • two cannot be cancelled because error': {'message': "Cannot cancel run with status 'completed'."
  • one cannot be cancelled because 'error': {'message': "Cannot cancel run with status 'cancelling'."

I have no indication why my flow would suddenly result in an active run at the moment I try to add a message (was never a problem before) and for now I don’t even find how to fix this ‘state’