semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Bug: Exception occurs at the end of foreach loop when streaming Azure OpenAI result and invoking tools.

Open sandrohanea opened this issue 2 years ago • 2 comments

Describe the bug A clear and concise description of what the bug is.

To Reproduce

  1. Activate Async Filter with Azure OpenAI
  2. Create a Kernel with some plugins tools.
  3. Invoke a Kernel function from prompt using streaming method.
  4. Iterate in the result.

Repro Code:

builder.AddAzureOpenAIChatCompletion(
    "gpt-4o-mini",
    "https://your-azureopenai-instance.openai.azure.com/",
    new DefaultAzureCredential());

builder.Plugins.AddFromType<NewsPlugin>();
var kernel1 = builder.Build();
OpenAIPromptExecutionSettings executionSettings = new()
{
    ToolCallBehavior = ToolCallBehavior.EnableFunctions(
        kernel1.Plugins
            .SelectMany(p => p)
            .Select(f => f.Metadata.ToOpenAIFunction()),
        autoInvoke: true),
    ChatSystemPrompt = "You are a smart AI assistant that have access to latest news and are able to help user with summaries."
};

var streamingFunc = kernel1.CreateFunctionFromPrompt("What happened today?", executionSettings);
var streamingEnumerable = streamingFunc.InvokeStreamingAsync(kernel1, new KernelArguments());
await foreach (var content in streamingEnumerable)
{
    var contentToWrite = content.ToString();
    Console.WriteLine(contentToWrite);
}

Exception

System.NullReferenceException: 'Object reference not set to an instance of an object.'

This exception was originally thrown at this call stack:
    OpenAI.Chat.StreamingChatCompletionUpdate.ContentUpdate.get()
    Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetStreamingChatMessageContentsAsync(string, Microsoft.SemanticKernel.ChatCompletion.ChatHistory, Microsoft.SemanticKernel.PromptExecutionSettings, Microsoft.SemanticKernel.Kernel, System.Threading.CancellationToken)
    Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetStreamingChatMessageContentsAsync(string, Microsoft.SemanticKernel.ChatCompletion.ChatHistory, Microsoft.SemanticKernel.PromptExecutionSettings, Microsoft.SemanticKernel.Kernel, System.Threading.CancellationToken)
    Microsoft.SemanticKernel.KernelFunctionFromPrompt.InvokeStreamingCoreAsync<TResult>(Microsoft.SemanticKernel.Kernel, Microsoft.SemanticKernel.KernelArguments, System.Threading.CancellationToken)
    Microsoft.SemanticKernel.KernelFunctionFromPrompt.InvokeStreamingCoreAsync<TResult>(Microsoft.SemanticKernel.Kernel, Microsoft.SemanticKernel.KernelArguments, System.Threading.CancellationToken)
    Microsoft.SemanticKernel.KernelFunction.InvokeStreamingAsync<TResult>(Microsoft.SemanticKernel.Kernel, Microsoft.SemanticKernel.KernelArguments, System.Threading.CancellationToken)
    Microsoft.SemanticKernel.KernelFunction.InvokeStreamingAsync<TResult>(Microsoft.SemanticKernel.Kernel, Microsoft.SemanticKernel.KernelArguments, System.Threading.CancellationToken)
    Program.<Main>$(string[]) in Program.cs

Expected behavior To not crash.

Platform

  • OS: [e.g. Windows, Mac] Any
  • IDE: [e.g. Visual Studio, VS Code] Any
  • Language: [e.g. C#, Python] C#
  • Source: [e.g. NuGet package version 0.1.0, pip package version 0.1.0, main branch of repository] NuGet 1.18.2

sandrohanea avatar Sep 09 '24 16:09 sandrohanea

@sandrohanea If possible, can you provide us what is the payload of the streaming response like we had in this other related issue so we can reproduce on our side?

  • #8407

RogerBarret0 avatar Sep 10 '24 09:09 RogerBarret0

Hello Roger, Here is the payload that is causing the crash:

data: {"choices":[],"created":0,"id":"","model":"","object":"","prompt_filter_results":[{"prompt_index":0,"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"jailbreak":{"filtered":false,"detected":false},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}}}]}

data: {"choices":[{"delta":{"content":"","role":"assistant"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":"Today"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":","},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" Tropical"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" Storm"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" Deb"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":"by"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" intensified"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" along"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" the"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" Atlantic"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" coast"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":","},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" prompting"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" millions"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" in"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" the"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" Carol"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":"inas"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" to"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" prepare"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" for"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" its"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" second"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" land"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":"fall"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":","},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" which"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" is"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" expected"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" to"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" bring"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" more"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" heavy"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" rainfall"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" to"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" already"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" saturated"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":" areas"},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{"content":"."},"finish_reason":null,"index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"delta":{},"finish_reason":"stop","index":0,"logprobs":null}],"created":1725961768,"id":"chatcmpl-A5rmykBk5Zh0PnPFzLOsf7OuYnuai","model":"gpt-4o-mini","object":"chat.completion.chunk","system_fingerprint":"fp_80a1bad4c7"}

data: {"choices":[{"content_filter_offsets":{"check_offset":636,"start_offset":636,"end_offset":848},"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}},"finish_reason":null,"index":0}],"created":0,"id":"","model":"","object":""}

data: {"choices":[{"content_filter_offsets":{"check_offset":636,"start_offset":636,"end_offset":848},"content_filter_results":{},"finish_reason":null,"index":0}],"created":0,"id":"","model":"","object":""}

data: [DONE]


sandrohanea avatar Sep 10 '24 09:09 sandrohanea