Error 400: already has an active run

Hi,

I am getting an Error 400 when using the Assistants API:

Error code: 400 - {‘error’: {‘message’: ‘Thread thread_daOMx7ESOdqQBP12345678910 already has an active run run_Md123456789F8oA206vDAgi5.’, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: None}}

The error occurs after the creation of the thread, just executing the run.

thread = in_client.beta.threads.create(
  messages=[
    {
      "role": "user",
      "content": paragraph_content_text
    }
  ],
)

run = in_client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=in_n_akey
)

The code does not go beyond this line. It crashes after several seconds.

I think that it’s impossible that I’m using an axistente thread because I just created it before executing the run.

Does anyone know what this may be due to? On status.openai.com I don’t see any malfunctioning of the servers or the API itself.

Thanks in advance for the help.

K.

This issue is caused by creating two threads with the same thread ID. You can only create one thread with the same thread ID at a time.

https://platform.openai.com/docs/assistants/overview

No, I don’t think that’s the problem.

I print in the logs the thread.id and confirm that they are not repeated. Also, I can’t define the id of the thread I create, it is assigned by the API when I create it.

1 Like

I’m sorry for jumping to conclusions earlier.

I didn’t encounter any errors on my end.
Have you created the assistant object and correctly specified the assistant ID?

I have suspicions that it may be the OpenAI server. Sometimes it stops at that point for several minutes, but it continues, it doesn’t crash. It is as if the servers were collapsed. Although the text of the error does not match my suspicion.

2 Likes

I have a similar problem when using the .NET SDK from OpenAI. Here is a repro app that I wrote:

using System.Text.Json;
using dotenv.net;
using OpenAI.Assistants;
#pragma warning disable OPENAI001

var env = DotEnv.Read(options: new DotEnvOptions(probeForEnv: true, probeLevelsToSearch: 7));

var client = new AssistantClient(env["OPENAI_KEY"]);

var assistant = await client.CreateAssistantAsync(env["OPENAI_MODEL"], new AssistantCreationOptions
{
    Name = "Test Assistant",
    Description = "Test Assistant",
    Instructions = "You are a helpful assistant that can answer questions about the secret number.",
    Tools = {
        new CodeInterpreterToolDefinition(),
        new FunctionToolDefinition()
        {
            FunctionName = "getSecretNumber",
            Description = "Gets the secret number",
            Parameters = BinaryData.FromObjectAsJson(
                new
                {
                    Type = "object",
                    Properties = new
                    {
                        Seed = new { Type = "integer", Description = "Optional seed for the secret number." },
                    },
                    Required = Array.Empty<string>()
                }, new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })
        }
    }
});

var thread = await client.CreateThreadAsync();
Console.WriteLine($"Thread ID: {thread.Value.Id}");

// The following line works (no function call)
//await client.CreateMessageAsync(thread.Value.Id, MessageRole.User, ["Are dolphins fish?"]);

// The following line crashes (function call)
await client.CreateMessageAsync(thread.Value.Id, MessageRole.User, ["Tell me the scret number with seed 1"]);

var asyncUpdate = client.CreateRunStreamingAsync(thread.Value.Id, assistant.Value.Id);
Console.WriteLine();
ThreadRun? currentRun;
do
{
    List<ToolOutput> outputsToSumit = [];
    currentRun = null;
    await foreach (var update in asyncUpdate)
    {
        if (update is RunUpdate runUpdate) { currentRun = runUpdate; }
        else if (update is RequiredActionUpdate requiredActionUpdate)
        {
            Console.WriteLine($"Calling function {requiredActionUpdate.FunctionName} {requiredActionUpdate.FunctionArguments}");
            outputsToSumit.Add(new ToolOutput(requiredActionUpdate.ToolCallId, "{ \"SecretNumber\": 42 }"));
        }
        else if (update is MessageContentUpdate contentUpdate)
        {
            Console.Write(contentUpdate.Text);
        }

        if (outputsToSumit.Count != 0)
        {
            asyncUpdate = client.SubmitToolOutputsToRunStreamingAsync(currentRun, outputsToSumit);
        }
    }

    if (outputsToSumit.Count != 0)
    {
        asyncUpdate = client.SubmitToolOutputsToRunStreamingAsync(currentRun, outputsToSumit);
    }
} while (currentRun?.Status.IsTerminal is false);

Console.WriteLine("\nDone.");

The app works with prompts that do not lead to function calls. The app crashes at the call to “CreateRunAsync” with “Thread … already has an active run”. I can repro the problem with “gpt-4o” and “gpt-4o-2024-08-06”. I am using the OpenAI NuGet package in version “2.0.0-beta.11”.

I also encountered the same error when using the 4o-mini assistant API. My environment is Node.js with the OpenAI SDK version 4.52.3.

Here is my case:

const events = openai.beta.threads.runs.stream(thread.id, {
  assistant_id: assistant.id,
  additional_messages: [{ role: 'user', content: content }],
  stream: true
});

I am still having this issue and it has become a major problem. I tried the official sample from the OpenAI GitHub repo openai-dotnet (path /examples/Assistants/Example02b_FunctionCallingStreaming.cs). It throws the same “Thread … already has an active run” error.