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.
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.
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.
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 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.