Skip to content
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 @@ -14,6 +14,7 @@
using System.Text.Json.Nodes;
using System.Text.Json.Schema;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using Microsoft.Shared.Diagnostics;

Expand Down Expand Up @@ -289,6 +290,12 @@ JsonNode TransformSchemaNode(JsonSchemaExporterContext schemaExporterContext, Js
objSchema.InsertAtStart(TypePropertyName, "string");
}

// Include a trivial items keyword if missing
if (ctx.TypeInfo.Kind is JsonTypeInfoKind.Enumerable && !objSchema.ContainsKey(ItemsPropertyName))
{
objSchema.Add(ItemsPropertyName, new JsonObject());
}

// Some consumers of the JSON schema, including Ollama as of v0.3.13, don't understand
// schemas with "type": [...], and only understand "type" being a single value.
// In certain configurations STJ represents .NET numeric types as ["string", "number"], which will then lead to an error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ public static void CreateJsonSchema_DefaultParameters_GeneratesExpectedJsonSchem
AssertDeepEquals(expected, actual);
}

[Fact]
public static void CreateJsonSchema_TrivialArray_GeneratesExpectedJsonSchema()
{
JsonElement expected = JsonDocument.Parse("""
{
"type": "array",
"items": {}
}
""").RootElement;

JsonElement actual = AIJsonUtilities.CreateJsonSchema(typeof(object[]), serializerOptions: JsonContext.Default.Options);

AssertDeepEquals(expected, actual);
}

[Fact]
public static void CreateJsonSchema_OverriddenParameters_GeneratesExpectedJsonSchema()
{
Expand Down Expand Up @@ -1326,6 +1341,7 @@ private class DerivedAIContent : AIContent
[JsonSerializable(typeof(DerivedAIContent))]
[JsonSerializable(typeof(MyPoco))]
[JsonSerializable(typeof(MyEnumValue?))]
[JsonSerializable(typeof(object[]))]
private partial class JsonContext : JsonSerializerContext;

private static bool DeepEquals(JsonElement element1, JsonElement element2)
Expand Down
Loading