Skip to content

Commit 018c014

Browse files
.Net: Fix #10389 (#10406)
### Motivation and Context Fixes issue #10389. ### Description Use `JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping` to generate more LLM friendly serialized FunctionResult. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] 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 - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
1 parent f5eb23d commit 018c014

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

dotnet/src/InternalUtilities/connectors/AI/FunctionCalling/FunctionCallsProcessor.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics.CodeAnalysis;
66
using System.Linq;
77
using System.Runtime.CompilerServices;
8+
using System.Text.Encodings.Web;
89
using System.Text.Json;
910
using System.Threading;
1011
using System.Threading.Tasks;
@@ -490,6 +491,17 @@ public static string ProcessFunctionResult(object functionResult)
490491
return chatMessageContent.ToString();
491492
}
492493

493-
return JsonSerializer.Serialize(functionResult);
494+
return JsonSerializer.Serialize(functionResult, s_functionResultSerializerOptions);
494495
}
496+
497+
/// <summary>
498+
/// The <see cref="JsonSerializerOptions" /> which will be used in <see cref="ProcessFunctionResult(object)"/>.
499+
/// </summary>
500+
/// <remarks>
501+
/// <see cref="JsonSerializer.Serialize{TValue}(TValue, JsonSerializerOptions?)"/> is very likely to escape characters and generates LLM unfriendly results by default.
502+
/// </remarks>
503+
private static readonly JsonSerializerOptions s_functionResultSerializerOptions = new()
504+
{
505+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
506+
};
495507
}

dotnet/src/SemanticKernel.UnitTests/Utilities/AIConnectors/FunctionCallsProcessorTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,19 @@ public void ItShouldSerializeFunctionResultsOfComplexType()
824824
Assert.Equal("{\"a\":2,\"b\":\"test\"}", result);
825825
}
826826

827+
[Fact]
828+
public void ItShouldSerializeFunctionResultsWithStringProperties()
829+
{
830+
// Arrange
831+
var functionResult = new { Text = "テスト" };
832+
833+
// Act
834+
var result = FunctionCallsProcessor.ProcessFunctionResult(functionResult);
835+
836+
// Assert
837+
Assert.Equal("{\"Text\":\"テスト\"}", result);
838+
}
839+
827840
private sealed class AutoFunctionInvocationFilter(
828841
Func<AutoFunctionInvocationContext, Func<AutoFunctionInvocationContext, Task>, Task>? onAutoFunctionInvocation) : IAutoFunctionInvocationFilter
829842
{

0 commit comments

Comments
 (0)