Repository navigation
Description
Describe the bug
An exception is thrown when streaming a response with toolcall auto invocation, though only when the prompt causes the function to be invoked.
Possibly related to PR 9273?
Stack trace:
System.ArgumentNullException: Value cannot be null. (Parameter 'bytes')
at System.ArgumentNullException.Throw(String paramName)
at System.Text.Encoding.GetString(Byte* bytes, Int32 byteCount)
at System.BinaryData.ToString()
at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetStreamingChatMessageContentsAsync(String targetModel, ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken)+MoveNext()
at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetStreamingChatMessageContentsAsync(String targetModel, ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken)+MoveNext()
at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetStreamingChatMessageContentsAsync(String targetModel, ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
To Reproduce
Steps to reproduce the behavior:
- use SK 1.23.0
- Configure a kernel with a plugin/function.
- Request a chat completion via
Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetStreamingChatMessageContentsAsync(), withOpenAIPromptExecutionSettingshavingToolCallBehaviorset toToolCallBehavior.AutoInvokeKernelFunctions, with a prompt that will trigger the function invocation. - See exception thrown.
Expected behavior
I expect the chat completion completes successfully.
Platform
- OS: Windows
- IDE: Visual Studio 2022
- Language: C#
- Source: Microsoft.SemanticKernel 1.23.0
Additional context
Using Azure OpenAI services, model gpt-4o.
UPDATE
After some testing I found the issue exists with SK 1.22.0 also, and my conclusion on the specific cause:
Using Semantic Kernel to do streaming chat completion, with auto tool invocation, when a function is called, results in an uncaught exception thrown by a specific version of a transitive dependency, System.Memory.Data 6.0.0.
This issue is realized now because the latest versions of other common Microsoft dependencies (Azure.Identity 1.13.0 and Azure.Storage.Blobs 12.22.2) have raised one of their minimum dependency versions such that System.Memory.Data is transitively updated from 1.0.2 to 6.0.0.
Workaround: in my project add a direct reference to System.Memory.Data >= 7.00 (I'm going with 8.0.1).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsSprint: Done
Activity
[-]Bug: 1.23.0 throws an exception when streaming a chat completion with auto-invoked tool call[/-][+].Net: Bug: 1.23.0 throws an exception when streaming a chat completion with auto-invoked tool call[/+]jphorv-bdo commentedon Oct 17, 2024
More info, this has something to do with System.Memory.Data which provides the
System.BinaryDatatype.I updated some other packages at the same time I updated from SK 1.22.0 to 1.23.0:
Both of those depend on Azure.Core, which in turn depends on System.Memory.Data. And recently, Azure.Core changed its dependency requirements:
So updating those packages simultaneously resulted in a much newer version of System.Memory.Data being brought into my project, and with this newer version of System.Memory.Data, the error occurs.
If I downgrade those 2 packages (Azure.Identity and Azure.Storage.Blobs to 1.21.1 and 12.22.1 respectively) and keep SK version 1.23.0, the exception does not occur.
jphorv-bdo commentedon Oct 17, 2024
I've confirmed that the exception is thrown in SK 1.22.0 also when my project updates to Azure.Identity 1.21.1 and Azure.Storage.Blobs 12.22.1.
jphorv-bdo commentedon Oct 17, 2024
Further investigation: if I don't upgrade any packages then I've got a working codebase using SK 1.22.0. It has no direct dependency on System.Memory.Data and so through transitive dependencies it installs System.Memory.Data 1.0.2. Again, this works.
If I take a direct dependency on System.Memory.Data:
Next I'll try updating the Azure packages and SK to 1.23, and with a direct dependency on System.Memory.Data 8.0.1 I expect things will work.
jphorv-bdo commentedon Oct 18, 2024
That workaround works as expected. So restating the bug report (I will add this to the body as well).
Using Semantic Kernel to do streaming chat completion, with auto tool invocation, when a function is called, results in an uncaught exception thrown by a specific version of transitive dependency, System.Memory.Data 6.0.0.
This issue is realized now because the latest versions of other common Microsoft dependencies (Azure.Identity 1.13.0 and Azure.Storage.Blobs 12.22.2) have raised one of their minimum dependency versions such that System.Memory.Data is transitively updated from 1.0.2 to 6.0.0.
Workaround: in my project add a direct reference to System.Memory.Data >= 7.00 (I'm going with 8.0.1).
rogerbarreto commentedon Oct 18, 2024
jphorv-bdo Thank you very much for the valuable context and investigation, in our repository our dependencies with
System.Memory.Datatargets8.0.0.semantic-kernel/dotnet/Directory.Packages.props
Line 43 in a3a69c8
Will try your approach with the
6.0.0and see if we can catch / avoid triggering this exception for our code-base.ystvan commentedon Oct 18, 2024
This dependency combination reproduces the bug (with auto-invoked tool call)
I have added
<PackageReference Include="System.Memory.Data" Version="8.0.1" />and the exception is gone.
What a Friday! This thread saved my weekend and deployment next Monday ❤️
jphorv-bdo commentedon Oct 21, 2024
What change was made to SK to fix this? I couldn't find any commits that appear related.
jphorv-bdo commentedon Oct 22, 2024
Roger Barreto (@rogerbarreto) you mentioned attempting to catch/avoid triggering the exception that occurs with System.Memory.Data v6.0.0, but EvanChaki (@evchaki) closed this and I don't see any relevant commits.
It seems like something should be done, shouldn't it?
1 remaining item
jphorv-bdo commentedon Oct 22, 2024
Roger Barreto (@rogerbarreto) I see, I didn't know it was unsupported. Never even thought about it until this bug appeared. But if it isn't supported then shouldn't one of the SK NuGet packages have a version requirement of System.Memory.Data >= 7.0 to prevent this from happening?
rogerbarreto commentedon Oct 22, 2024
Actually our requirement for
Azure.Identityis >= 1.12, as the problem appears in1.13we can't control this, I will investigate upgrading theAzure.Identityto 1.13 and see what happens to ensure compatibility..Net: Fix error for System.Memory.Data 6, upgrade packages dependenci…
jphorv-bdo commentedon Oct 29, 2024
Thanks for the fix Roger Barreto (@rogerbarreto) !