Run.status not moving to requires_action while using custom function in an Assistant

Hi Forum,

I am running the following code as part of learning how to create Assistants with custom functions. The executes till the run and then gives an error (after the “wait_on_run” function) and exits as run.status is not moving to “requires_sction”. Any guidance or help is greatly appreciated.

import os
import openai
import time
import json

from openai import OpenAI
client = OpenAI()

def wait_on_run(run, thread):
# Simple function that waits on the run of a particular thread. Returns the run once the thread has been run.
while run.status == “queued” or run.status == “in_progress”:
print(run.status)
run = client.beta.threads.runs.retrieve (thread_id=thread.id, run_id=run.id)
time.sleep(2)
return run

def word_definition_quiz(word,definition_options):
print(“Hello! Let’s test your knowledge about words.”)
print(f"What is the correct definition of this word: {word}“)
print(”\n")
for num,option in enumerate(definition_options):
print(f"Definition #{num} is: {option}“)
print(”\n")
num_choice = input(“What is your choice? (Return the single number option)”)
return definition_options[int(num_choice)]

function_json=[{
“type”: “function”,
“function”: {
“name”: “word_definition_quiz”,
“description”: “Get the quize response from the student”,
“parameters”: {
“type”: “object”,
“properties”: {
“word”: {“type”: “string”, “description”: “The word for which definition is asked”},
“definition_options”: {“type”: “array”,
“items”: {“type”:“string”},
“description”: “A Python list of strings of definitions. Only one definition would be correct for the given word.”}
},
“required”: [“word”, “definition_options”]
}
}
}]

#instructions=“You help create a quiz for checking defintions of words, providing a word and then multiple definition options, where only one option is correct.”

assistant = client.beta.assistants.create(instructions=“You help create a quiz for checking defintions of words, providing a word and then multiple definition options, where only one option is correct”,
model=“gpt-4-turbo-preview”,
tools = [{
“type”: “function”,
“function”: {
“name”: “word_definition_quiz”,
“description”: “Get the quize response from the student”,
“parameters”: {
“type”: “object”,
“properties”: {
“word”: {“type”: “string”, “description”: “The word for which definition is asked”},
“definition_options”: {“type”: “array”,
“items”: {“type”:“string”},
“description”: “A Python list of strings of definitions. Only one definition would be correct for the given word.”}
},
“required”: [“word”, “definition_options”]
}
}
}]
)

thread = client.beta.threads.create()

user_input = “Create a new quiz question word and definition list. Then let me know if the student answer recieved is correct”

message = client.beta.threads.messages.create(thread_id=thread.id, role=“user”, content=“Create a new quiz question word and definition list. Then using the function defined in tools, let me know if the student answer recieved is correct”)

run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id)

wait_on_run(run, thread)

tool_call = run.required_action.submit_tool_outputs.tool_calls[0]

arguments = json.loads(tool_call.function.arguments)

response = word_definition_quiz(arguments[‘word’], arguments[‘definition_options’])

run = client.beta.threads.runs.submit_tool_outputs (thread_id = thread.id, run_id = run.id,
tool_outputs = [{‘tool_call_id’:tool_call.id,
‘output’: json.dumps(response)
}])
run = wait_on_run(run,thread)

messages = client.beta.threads.messages.list(thread.id)

for thread_message in messages:
print(thread_message.content[0].text.value)
print(‘\n’)