OpenAi Api Assistant Answers with the Question value

Description: Ive encountered an issue when i send a message to the assistant it returns me the same value as a answer. but its not consistent issue, i can ask the same question a moment after and it will result with a actual answer.
this error usually characterized by long time of waiting for thread run to finish

specs:
gpt-3.5-turbo-1106

code:
SENDING THE QUESTION:
client.beta.threads.messages.create(
thread_id=thread.id, role=“user”, content=user_message
)
return thread,client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=ASSISTANT_ID,
)

WAITING FOR RESPONSE FROM API:
while run.status == “queued” or run.status == “in_progress”:
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id,
)
time.sleep(0.5)
return run

GETTING RESPONSE:
return client.beta.threads.messages.list(thread_id=thread.id, order=“asc”).data[-1].content[0].text.value

thanks for eveyone who encountered this issue and found any solution

It can take a while before the Assistant writes a response to the thread of messages, so you need to keep polling the run status until one of the final statuses is set. e.g. [expired, completed, failed, cancelled].

Depending on which one of these final statuses your run ends up, you can then decide the appropriate action for your code to take. Ideally, look for the completed status as an indication of success and treat the other three final statuses as an error of some sort. Your code snippet seems to be only checking the queued and in_progress statuses, which might not be ideal if the run ends up in an error status as you are not handling the errors.

Also be careful of the requires_action status which results when there is a Function call that is required, as the Assistant will wait for up to 10min for your code to send the outputs of the call before expiring the run.

It takes a fair amount of code to properly handles all the possible states in a robust way.More information can be found on the Run lifecycle in the OpenAI docs.

1 Like