semantic-kernel
semantic-kernel copied to clipboard
.Net: Repeated ChatHistory.AddStreamingMessageAsync(GetStreamingChatMessageContentsAsync) throws ClientResultException
Describe the bug
When using the streaming API with automatic tool calls like this:
// Create Kernel
var kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(deployment, client)
.Build();
kernel.ImportPluginFromObject(new Functions());
// Prepare Chat
var chatService = kernel.GetRequiredService<IChatCompletionService>();
OpenAIPromptExecutionSettings settings = new()
{
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};
ChatHistory chatHistory = new("You are to test the system");
for (int i = 0; i < 2; i++)
{
try
{
chatHistory.AddUserMessage("Please test the system");
var results = chatHistory.AddStreamingMessageAsync(chatService
.GetStreamingChatMessageContentsAsync(chatHistory, settings, kernel)
.Cast<OpenAIStreamingChatMessageContent>()
);
await foreach (var result in results)
{
Console.Write(result.ToString());
}
Console.WriteLine($"Call #{i} OK");
}
catch (Exception e)
{
// On 2nd call...
Console.WriteLine("ERROR: " + e.Message);
}
}
a second call after the assistant has actually invoked a function will throw a ClientResultException with:
HTTP 400 (invalid_request_error: )
Parameter: messages.[5].role
An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_MnrWYfXsn1cz6RgiUi44acoy
To Reproduce I have attached a small Console app that reproduces the issue. ToolCallBug.zip
Platform
- OS: Windows, Azure OpenAI (GPT4o)
- IDE: Visual Studio
- Language: C#
- Source: Microsoft.SemanticKernel 1.25.0
@Hassmann - thanks for reporting this, we will take a look.