Assisstants API - How to know when all tasks/runs have completed

Hi - I can’t see where this has been asked before so maybe I’m the only one having this problem.

I’m using the assisstants API with code interpreter.

I’m seeing lots of instances where the programme ends prematurely. For example it will answer my question up until a point where it might say something like, “Let me take another look using X method instead”, and then the programme ends and returns to the user prompt.

When using Data Analysis tool in ChatGPT it will just continue until finished.

I can’t see any obvious flag in the run or step object that notifies when everyhting has completed and it can return back to the user prompt.

Here is my Python code:

import openai
import time

# Initialize the client with your API key
client = openai.OpenAI(api_key="sk-xxxxx")

# Upload a file with an "assistants" purpose
file = client.files.create(
    file=open("PCBDX2.csv", "rb"),

# Create an assistant
assistant = client.beta.assistants.create(
    name="Data Analyst",
    instructions="You are a data analyst. Write and run code to answer questions about my data.",
    tools=[{"type": "code_interpreter"}],

# Create a thread
thread = client.beta.threads.create()

while True:
    q = input('How can I help: ')

    if q.lower() == 'exit':

    # Create a message
    message = client.beta.threads.messages.create(,

    # Create a run
    run = client.beta.threads.runs.create(,,
        instructions="Please address the user as Dan"

    # Store the run ID
    run_id =

    # Polling for the run status
    while True:
        # Retrieve the run status
        run_status = client.beta.threads.runs.retrieve(

        # Check and print the step details
        run_steps = client.beta.threads.runs.steps.list(
        for step in
            if step.type == 'tool_calls':
                print(f"Tool {step.type} invoked.")

            # If step involves code execution, print the code
            if step.type == 'code_interpreter':
                print(f"Python Code Executed: {step.step_details['code_interpreter']['input']}")

        if run_status.status == 'completed':
            # Retrieve all messages from the thread
            messages = client.beta.threads.messages.list(

            # Print all messages from the thread
            for msg in
                role = msg.role
                content = msg.content[0].text.value
                print(f"{role.capitalize()}: {content}")
            break  # Exit the polling loop since the run is complete
        elif run_status.status in ['queued', 'in_progress']:
            print(f'{run_status.status.capitalize()}... Please wait.')
            time.sleep(1.5)  # Wait before checking again
            print(f"Run status: {run_status.status}")
            break  # Exit the polling loop if the status is neither 'in_progress' nor 'completed'


I’m seeing the exact same issue. Something like 5-10% of requests just prematurely provide a "completed"status