Skip to content

JsonSchema created with AIFunctionFactory ignores reference types nullability for method arguments #7182

@NoofSaeidh

Description

@NoofSaeidh

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}}}

Metadata

Metadata

Labels

area-aiMicrosoft.Extensions.AI librariesbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions