The use case I have is, we let assistant ask 10 question and at the end of the 10th question we call a function to return status of all 10 questions.
Now I want to capture that function call but its getting skipped because completed is called before and i dont reach till requires_action.
I cant rely on 10th question because next time it can be 20 or 30
how can i get my function called(requires_action) first and then completed.
while retry_count < max_retries:
run = openai_client.beta.threads.runs.retrieve(
thread_id=thread_id,
run_id=run_id,
)
# Handle requires_action status
print(f"Run status: {run.status}")
print(f"Run : {run}")
if run.status == "requires_action":
# Extract the required action
tools_output = []
required_action = run.required_action
if required_action and required_action.type == "submit_tool_outputs":
tool_calls = required_action.submit_tool_outputs.tool_calls
for tool_call in tool_calls:
tool_name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)
response_data = await handle_tool_call(tool_name, arguments)
tools_output.append({
"tool_call_id": tool_call.id,
"output": json.dumps(response_data),
})
await finalize_run(openai_client, thread_id, run_id, tool_name, tools_output)
return
if run.status == "completed":
return run
await asyncio.sleep(delay)
delay = min(delay * 2, 30) # Exponential backoff with a maximum delay of 30s
retry_count += 1
raise TimeoutError("Run did not complete within the allowed retries.")
first this gets called
{
“data”:{
…
}
“event”:
“thread.message.delta”
}
{
“data”:{
…
}
“event”:
“thread.message.completed”
}
{
“data”:{
…
}
“event”:
“thread.run.step.completed”
}
{
“data”:{
…
}
“event”:
“thread.run.step.created”
}
then
{
“data”:{
…
}
“event”:
“thread.run.step.delta”
}
{
“data”:{
…
}
“event”:
“thread.run.step.delta”
}
{
“data”:{
…
}
“event”:
“thread.run.step.delta”
}
{
“data”:{
…
}
“event”:
“thread.run.requires_action”
}
either I need to update my while loop or assistant?
Really appreciate for your help