Keep tool results readable for the model

This commit is contained in:
Thorsten Sommer 2026-09-24 17:20:58 +02:00
parent ad16aa8fb6
commit 38cd0c9f36
Signed by untrusted user who does not match committer: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 54 additions and 1 deletions

View File

@ -1,3 +1,5 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using AIStudio.Provider;
@ -7,6 +9,18 @@ namespace AIStudio.Tools.ToolCallingSystem;
public sealed class ToolExecutionResult
{
/// <summary>
/// How a JSON result is written for the model.
/// </summary>
/// <remarks>
/// The result goes into the request as a string, and the request is serialized once more on its
/// way to the provider, so the model reads whatever this escapes as the escape itself. The
/// default encoder escapes every character outside ASCII and those HTML treats specially, for
/// JSON embedded in a web page, which this never is: a German document would reach the model
/// with every umlaut as six characters. The relaxed encoder escapes only what JSON requires.
/// </remarks>
private static readonly JsonSerializerOptions MODEL_CONTENT_OPTIONS = new() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
public string? TextContent { get; init; }
public JsonNode? JsonContent { get; init; }
@ -29,7 +43,7 @@ public sealed class ToolExecutionResult
public string ToModelContent()
{
if (this.JsonContent is not null)
return this.JsonContent.ToJsonString();
return this.JsonContent.ToJsonString(MODEL_CONTENT_OPTIONS);
return this.TextContent ?? string.Empty;
}

View File

@ -0,0 +1,39 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using AIStudio.Tools.ToolCallingSystem;
namespace AIStudio.Tests.Tools.ToolCalling;
/// <summary>
/// Checks what a model reads of a tool result.
/// </summary>
/// <remarks>
/// The result goes into the request as a string, and the request is serialized once more on its way
/// to the provider. Whatever the first serialization escapes therefore reaches the model as the
/// escape itself: a German document would arrive with every umlaut spelled out as six characters,
/// and a piece of code with every angle bracket. That costs tokens, the budget of all tool results
/// counts it, and a model quoting a name from it may quote the escape.
/// </remarks>
[TestFixture]
public sealed class ToolExecutionResultTests
{
[TestCase("Größe der Übersicht")]
[TestCase("if (a < b && c > d) return 'x';")]
[TestCase("日本語のテキスト")]
public void TextReachesTheModelAsWritten(string text)
{
var result = new ToolExecutionResult { JsonContent = new JsonObject { ["text_content"] = text } };
Assert.That(result.ToModelContent(), Does.Contain(text));
}
[Test]
public void TheResultStaysValidJson()
{
const string TEXT = "A \"quoted\" line\nand a backslash \\ at its end.";
var result = new ToolExecutionResult { JsonContent = new JsonObject { ["text_content"] = TEXT } };
var readBack = JsonSerializer.Deserialize<JsonObject>(result.ToModelContent());
Assert.That(readBack?["text_content"]?.GetValue<string>(), Is.EqualTo(TEXT), "What JSON itself has to escape, it still escapes.");
}
}