Skip to content

Commit 801436c

Browse files
.Net: Don't allow function name/id to be overwritten with an empty string (#9601)
### Motivation and Context Closes #9567 ### Description The fix handles the case where a streaming update contains an empty string for the function name. I haven't been able to reproduce the scenario where this happens and I have asked the issue creator for more information. The function name has a max size of 64 chars and when I use that the function isn't split over multiple streaming responses. Referencing the approach shown here: https://community.openai.com/t/functions-calling-with-streaming/305742 ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent 19fef34 commit 801436c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

dotnet/src/SemanticKernel.Abstractions/Contents/FunctionCallContentBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ private static void TrackStreamingFunctionCallUpdate(StreamingFunctionCallUpdate
181181

182182
// If we have an call id, ensure the index is being tracked. Even if it's not a function update,
183183
// we want to keep track of it so we can send back an error.
184-
if (update.CallId is string id)
184+
if (update.CallId is string id && !string.IsNullOrEmpty(id))
185185
{
186186
(functionCallIdsByIndex ??= [])[update.FunctionCallIndex] = id;
187187
}
188188

189189
// Ensure we're tracking the function's name.
190-
if (update.Name is string name)
190+
if (update.Name is string name && !string.IsNullOrEmpty(name))
191191
{
192192
(functionNamesByIndex ??= [])[update.FunctionCallIndex] = name;
193193
}

dotnet/src/SemanticKernel.UnitTests/Contents/FunctionCallBuilder/FunctionCallContentBuilderTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,41 @@ public void ItShouldBuildFunctionCallContentForOneFunction(JsonSerializerOptions
4646
Assert.Null(functionCall.Exception);
4747
}
4848

49+
[Theory]
50+
[ClassData(typeof(TestJsonSerializerOptionsForKernelArguments))]
51+
public void ItShouldNotOverwriteFunctionNameOrId(JsonSerializerOptions? jsos)
52+
{
53+
// Arrange
54+
var sut = jsos is not null ? new FunctionCallContentBuilder(jsos) : new FunctionCallContentBuilder();
55+
56+
// Act
57+
var update1 = CreateStreamingContentWithFunctionCallUpdate(choiceIndex: 1, functionCallIndex: 2, callId: "f_101", name: null, arguments: null);
58+
sut.Append(update1);
59+
60+
var update2 = CreateStreamingContentWithFunctionCallUpdate(choiceIndex: 1, functionCallIndex: 2, callId: null, name: "WeatherUtils-GetTemperature", arguments: null);
61+
sut.Append(update2);
62+
63+
var update3 = CreateStreamingContentWithFunctionCallUpdate(choiceIndex: 1, functionCallIndex: 2, callId: "", name: "", arguments: "{\"city\":");
64+
sut.Append(update3);
65+
66+
var update4 = CreateStreamingContentWithFunctionCallUpdate(choiceIndex: 1, functionCallIndex: 2, callId: null, name: null, arguments: "\"Seattle\"}");
67+
sut.Append(update4);
68+
69+
var functionCalls = sut.Build();
70+
71+
// Assert
72+
var functionCall = Assert.Single(functionCalls);
73+
74+
Assert.Equal("f_101", functionCall.Id);
75+
Assert.Equal("WeatherUtils", functionCall.PluginName);
76+
Assert.Equal("GetTemperature", functionCall.FunctionName);
77+
78+
Assert.NotNull(functionCall.Arguments);
79+
Assert.Equal("Seattle", functionCall.Arguments["city"]);
80+
81+
Assert.Null(functionCall.Exception);
82+
}
83+
4984
[Theory]
5085
[ClassData(typeof(TestJsonSerializerOptionsForKernelArguments))]
5186
public void ItShouldBuildFunctionCallContentForManyFunctions(JsonSerializerOptions? jsos)

0 commit comments

Comments
 (0)