# Failed Run Status when using assistance&threads

**URL:** <https://community.openai.com/t/failed-run-status-when-using-assistance-threads/528095>\
**Category:** API\
**Tags:** chatgpt\
**Created:** [November 26, 2023, 10:38pm UTC](https://community.openai.com/t/failed-run-status-when-using-assistance-threads/528095 "2023-11-26T22:38:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![clashofclanstownhall](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/clashofclanstownhall/32/260596_2.png) [@clashofclanstownhall](https://community.openai.com/u/clashofclanstownhall)\
**Post date:** [November 26, 2023, 10:38pm UTC](https://community.openai.com/t/failed-run-status-when-using-assistance-threads/528095/1 "2023-11-26T22:38:00Z")

</div>

Hello im pretty desperate…I’m trying to implement a chatbot based on chatgpt. Therefore I have looked up a script which should be working, accoriding to the sources - To get started with a first API call…But my request all get a failed run status… I tried several different python script e.q. this one here:

from openai import OpenAI  
import time

client = OpenAI(api\_key=“xxx”)

assistant = client.beta.assistants.create(  
name=“Code Assistant”,  
instructions=“You are a code helper for beginners. Write, execute, and explain code to best answer questions.”,  
tools=[{“type”: “code\_interpreter”}],  
model=“gpt-3.5-turbo”  
)

thread = client.beta.threads.create()

message = client.beta.threads.messages.create(  
thread\_id=thread.id,  
role=“user”,  
content=“How does recursion work? Give an example in Python.”  
)

run = client.beta.threads.runs.create(  
thread\_id=thread.id,  
assistant\_id=assistant.id,  
instructions=“Explain as simply as possible, the user is a beginner.”  
)

while True:  
run\_status = client.beta.threads.runs.retrieve(thread\_id=thread.id,  
run\_id=run.id)  
print(f"Run status: {run\_status.status}")  
if run\_status.status == ‘completed’:  
break  
time.sleep(1) # Wait for a second before checking again

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

assistant\_response = messages.data[0].content[0].text.value

print(assistant\_response)

Can you spot the problem?  
Thank you in advance

---

<div class="post-metadata">

**Author:** ![GuilhermeZago](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/guilhermezago/32/238399_2.png) [@GuilhermeZago](https://community.openai.com/u/GuilhermeZago)\
**Post date:** [November 26, 2023, 10:53pm UTC](https://community.openai.com/t/failed-run-status-when-using-assistance-threads/528095/2 "2023-11-26T22:53:09Z")

</div>

> [@clashofclanstownhall](#):
>
> from openai import OpenAI  
> import time
> 
> client = OpenAI(api\_key=“xxx”)
> 
> assistant = client.beta.assistants.create(  
> name=“Code Assistant”,  
> instructions=“You are a code helper for beginners. Write, execute, and explain code to best answer questions.”,  
> tools=[{“type”: “code\_interpreter”}],  
> model=“gpt-3.5-turbo”  
> )
> 
> thread = client.beta.threads.create()
> 
> message = client.beta.threads.messages.create(  
> thread\_id=thread.id,  
> role=“user”,  
> content=“How does recursion work? Give an example in Python.”  
> )
> 
> run = client.beta.threads.runs.create(  
> thread\_id=thread.id,  
> assistant\_id=assistant.id,  
> instructions=“Explain as simply as possible, the user is a beginner.”  
> )
> 
> while True:  
> run\_status = client.beta.threads.runs.retrieve(thread\_id=thread.id,  
> run\_id=run.id)  
> print(f"Run status: {run\_status.status}")  
> if run\_status.status == ‘completed’:  
> break  
> time.sleep(1) # Wait for a second before checking again
> 
> messages = client.beta.threads.messages.list(  
> thread\_id=thread.id  
> )
> 
> assistant\_response = messages.data[0].content[0].text.value
> 
> print(assistant\_response)

Can you explain further what is going on? Maybe a screenshot?

I just copy pasted your code and it worked flawlessly:

 ![image](https://us1.discourse-cdn.com/openai1/original/4X/e/d/1/ed132bd9f16454bd7e9ee616108f4c5192f2f33c.jpeg)

Edit: I only had to update the _ **"** _ and _ **'** _, but usually its due to the copy paste from the web

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [November 26, 2023, 11:59pm UTC](https://community.openai.com/t/failed-run-status-when-using-assistance-threads/528095/3 "2023-11-26T23:59:30Z")

</div>

> [@clashofclanstownhall](#):
>
> client = OpenAI(api\_key=“xxx”)

You need to remove the lack of a key, just leave the api\_key parameter out, and instead set an environment variable in your operating system OPENAI\_API\_KEY with an API key from your account.

Python should be version 3.8-3.11. Then you’ll need to again run `pip install --upgrade openai` to get the latest version installed.

None of the scripts or files in your program directory - especially your own program - should be named openai. They would get called instead of the correct library.

Then - that code is an “assistant” a very poor way to enter the world of interacting with OpenAI AI models.

Here’s a script I made to be as simple as possible, while still supporting:

- latest openai python methods
- demonstration of system and user messages, and maintaining a chat history
- introduces itself, giving immediate confirmation all is working without you needing to type
- giving a limited number of past chat turns back to the AI for context understanding
- streaming output to the python console (without any formatting)
- looping conversation until you type “exit”

```auto
from openai import OpenAI
client = OpenAI()
system = [{"role": "system",
           "content": """You are a chatbot, giving expert answers."""}]
user = [{"role": "user", "content": "brief introduction?"}]
chat = []
while not user[0]['content'] == "exit":
    response = client.chat.completions.create(
        messages = system + chat[-10:] + user,
        model="gpt-3.5-turbo", top_p=0.9, stream=True)
    reply = ""
    for delta in response:
        if not delta.choices[0].finish_reason:
            word = delta.choices[0].delta.content or ""
            reply += word
            print(word, end ="")
    chat += user + [{"role": "assistant", "content": reply}]
    user = [{"role": "user", "content": input("\nPrompt: ")}]

```

---

<div class="post-metadata">

**Author:** ![hollinwakefield](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/hollinwakefield/32/261493_2.png) [@hollinwakefield](https://community.openai.com/u/hollinwakefield)\
**Post date:** [November 27, 2023, 7:29pm UTC](https://community.openai.com/t/failed-run-status-when-using-assistance-threads/528095/4 "2023-11-27T19:29:20Z")

</div>

It’s important to keep in mind that OP is asking about assistants and threads, so “stream=true” will not work right now, as that’s currently a limitation of the assistants API.

---

<div class="post-metadata">

**Author:** ![landed](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/landed/32/448350_2.png) [@landed](https://community.openai.com/u/landed)\
**Post date:** [February 9, 2024, 11:03am UTC](https://community.openai.com/t/failed-run-status-when-using-assistance-threads/528095/5 "2024-02-09T11:03:30Z")

</div>

I have a very similar issue and my code was running ok 2 days ago. I thought the system may have been overloaded or something.

I have looked at the comments posted here but nothing seemed to be clear what to do. Yes I am a beginner with Python but this was working.

```auto
def wait_for_run_completion(thread_id, run_id, timeout=50):
    start_time = time.time()
    while time.time() - start_time < timeout:
        print(time.time() - start_time)
        run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
        print(run_status.status,"\n\n\n")
        if run_status.status == 'completed':
            return run_status
        elif run_status.status == 'failed':
            quit()
        time.sleep(10)
    raise TimeoutError("Run did not complete within the specified timeout.")

```

---

<div class="post-metadata">

**Author:** ![zorangrete](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/zorangrete/32/296553_2.png) [@zorangrete](https://community.openai.com/u/zorangrete)\
**Post date:** [March 5, 2025, 7:24pm UTC](https://community.openai.com/t/failed-run-status-when-using-assistance-threads/528095/6 "2025-03-05T19:24:17Z")

</div>

Did you check your credit balance?
