Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Fix #10389 #10406

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -490,6 +491,17 @@ public static string ProcessFunctionResult(object functionResult)
return chatMessageContent.ToString();
}

return JsonSerializer.Serialize(functionResult);
return JsonSerializer.Serialize(functionResult, s_functionResultSerializerOptions);
}

/// <summary>
/// The <see cref="JsonSerializerOptions" /> which will be used in <see cref="ProcessFunctionResult(object)"/>.
/// </summary>
/// <remarks>
/// <see cref="JsonSerializer.Serialize{TValue}(TValue, JsonSerializerOptions?)"/> is very likely to escape characters and generates LLM unfriendly results by default.
/// </remarks>
private static readonly JsonSerializerOptions s_functionResultSerializerOptions = new()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,19 @@ public void ItShouldSerializeFunctionResultsOfComplexType()
Assert.Equal("{\"a\":2,\"b\":\"test\"}", result);
}

[Fact]
public void ItShouldSerializeFunctionResultsWithStringProperties()
{
// Arrange
var functionResult = new { Text = "テスト" };

// Act
var result = FunctionCallsProcessor.ProcessFunctionResult(functionResult);

// Assert
Assert.Equal("{\"Text\":\"テスト\"}", result);
}

private sealed class AutoFunctionInvocationFilter(
Func<AutoFunctionInvocationContext, Func<AutoFunctionInvocationContext, Task>, Task>? onAutoFunctionInvocation) : IAutoFunctionInvocationFilter
{
Expand Down
Loading