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"),
    purpose='assistants'
)

# 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"}],
    model="gpt-4-1106-preview",
    file_ids=[file.id]
)

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

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

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

    # Create a message
    message = client.beta.threads.messages.create(
        thread_id=thread.id,
        role="user",
        content=q
    )

    # Create a run
    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant.id,
        instructions="Please address the user as Dan"
    )

    # Store the run ID
    run_id = run.id

    # Polling for the run status
    while True:
        # Retrieve the run status
        run_status = client.beta.threads.runs.retrieve(
            thread_id=thread.id,
            run_id=run_id
        )

        # Check and print the step details
        run_steps = client.beta.threads.runs.steps.list(
            thread_id=thread.id,
            run_id=run_id
        )
        for step in run_steps.data:
            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(
                thread_id=thread.id
            ) 

            # Print all messages from the thread
            for msg in messages.data:
                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
        else:
            print(f"Run status: {run_status.status}")
            break  # Exit the polling loop if the status is neither 'in_progress' nor 'completed'

3 Likes

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

1 Like

I also face similar issue while using assistant api. The status is changed from in progress to completed prematurely