openai / openai-dotnet

The official .NET library for the OpenAI API
https://www.nuget.org/packages/OpenAI
MIT License
707 stars 60 forks source link

Assistant API how to properly handle hanging run? #87

Open DraTeots opened 1 week ago

DraTeots commented 1 week ago

I develop an app which utilizes assistant API with custom function call.

The problem I am having is that if user has two clients opened with the same thread. One of the clients create a run, which hangs in function call. Then the next client starts constantly failing at

var message = assistantClient.CreateMessage(threadInfo, MessageRole.User, new[] { MessageContent.FromText(newText) },

with

ClientResultException: HTTP 400 (invalid_request_error: )

Can't add messages to thread_4yYjTyrns5kqKcM4XZNw75wz while a run run_kdnQuv6LVnk1dzoHpTynhnFR is active.
OpenAI.ClientPipelineExtensions.ProcessMessage (System.ClientModel.Primitives.ClientPipeline pipeline, System.ClientModel.Primitives.PipelineMessage message, System.ClientModel.Primitives.RequestOptions options) (at <9ae513c2b2b94747af8c7391523e6f61>:0)
OpenAI.Assistants.InternalAssistantMessageClient.CreateMessage (System.String threadId, System.ClientModel.BinaryContent content, System.ClientModel.Primitives.RequestOptions options) (at <9ae513c2b2b94747af8c7391523e6f61>:0)
OpenAI.Assistants.AssistantClient.CreateMessage (System.String threadId, System.ClientModel.BinaryContent content, System.ClientModel.Primitives.RequestOptions options) (at <9ae513c2b2b94747af8c7391523e6f61>:0)
OpenAI.Assistants.AssistantClient.CreateMessage (System.String threadId, OpenAI.Assistants.MessageRole role, System.Collections.Generic.IEnumerable`1[T] content, OpenAI.Assistants.MessageCreationOptions options, System.Threading.CancellationToken cancellationToken) (at <9ae513c2b2b94747af8c7391523e6f61>:0)
OpenAI.Assistants.AssistantClient.CreateMessage (OpenAI.Assistants.AssistantThread thread, OpenAI.Assistants.MessageRole role, System.Collections.Generic.IEnumerable`1[T] content, OpenAI.Assistants.MessageCreationOptions options) (at <9ae513c2b2b94747af8c7391523e6f61>:0)
...

And the other instance might hang forever on waiting for function call return. IDK why.

Opening the previous thread is important for the app. Thus I probably have to handle running runs somehow? There are many ways how to actually do this, but how to correctly handle it without disturbing the other running instance work?