Description
JsonSchema created with AIFunctionFactory ignores reference types nullability and always add such types as not nullable.
Also when ref parameters has default null value it still adds default value to null, but disallow null as value for field, which leads to errors during llm calls.
Reproduction Steps
execute code:
using System.Text.Json;
using Microsoft.Extensions.AI;
Console.WriteLine(JsonSerializer.Serialize(AIFunctionFactory.Create(Test).JsonSchema));
void Test(string ? nullableString, int ? nullableInt, string ? nullableStringWithDefault = null, int ? nullableIntWithDefault = null) { }
Expected behavior
{
"type": "object",
"properties": {
"nullableString": {
"type": [
"string",
"null"
]
},
"nullableInt": {
"type": [
"integer",
"null"
]
},
"nullableStringWithDefault": {
"type": [
"string",
"null"
],
"default": null
},
"nullableIntWithDefault": {
"type": [
"integer",
"null"
],
"default": null
}
},
"required": [
"nullableString",
"nullableInt"
]
}
Actual behavior
{
"type": "object",
"properties": {
"nullableString": {
"type": "string"
},
"nullableInt": {
"type": [
"integer",
"null"
]
},
"nullableStringWithDefault": {
"type": "string",
"default": null
},
"nullableIntWithDefault": {
"type": [
"integer",
"null"
],
"default": null
}
},
"required": [
"nullableString",
"nullableInt"
]
}
Regression?
No response
Known Workarounds
Create or change schema manually by setting TransformSchemaNode in AIJsonSchemaCreateOptions
Configuration
tested on
- .NET 8
- .NET 10
- .NET Framework 4.8 with PolySharp
- Microsoft.Extensions.AI / AI.Abstraction 10.0.1
Other information
For object with nullable properties it works fine, only top level arguments itself.
Example:
void Test(Obj? obj = null) { }
class Obj
{
public string? StrProp { get; set; }
}
{"type":"object","properties":{"obj":{"type":"object","properties":{"strProp":{"type":["string","null"]}},"default":null}}}
Description
JsonSchema created with AIFunctionFactory ignores reference types nullability and always add such types as not nullable.
Also when ref parameters has default null value it still adds default value to null, but disallow null as value for field, which leads to errors during llm calls.
Reproduction Steps
execute code:
Expected behavior
{ "type": "object", "properties": { "nullableString": { "type": [ "string", "null" ] }, "nullableInt": { "type": [ "integer", "null" ] }, "nullableStringWithDefault": { "type": [ "string", "null" ], "default": null }, "nullableIntWithDefault": { "type": [ "integer", "null" ], "default": null } }, "required": [ "nullableString", "nullableInt" ] }Actual behavior
{ "type": "object", "properties": { "nullableString": { "type": "string" }, "nullableInt": { "type": [ "integer", "null" ] }, "nullableStringWithDefault": { "type": "string", "default": null }, "nullableIntWithDefault": { "type": [ "integer", "null" ], "default": null } }, "required": [ "nullableString", "nullableInt" ] }Regression?
No response
Known Workarounds
Create or change schema manually by setting TransformSchemaNode in AIJsonSchemaCreateOptions
Configuration
tested on
Other information
For object with nullable properties it works fine, only top level arguments itself.
Example:
{"type":"object","properties":{"obj":{"type":"object","properties":{"strProp":{"type":["string","null"]}},"default":null}}}