Skip to content

Enable Nullable options for CreateSchemaJson to use OpenAPI recommendation. #6306

Description

@rogerbarreto

Description

Currently when a schema is generated for nullable properties, the type is defined as an array ["null","<property_type>"].

This does not follow the standard for OpenAPI 3.0 spec:

This becomes a problem when API's for Structured Outputs follows the OpenAPI 3.0 standard and some workarounds needs to be put in place as in this PR, having a setting, would be ideal.

Reproduction Steps

Reproduction Code

using Microsoft.Extensions.AI;

var schema = AIJsonUtilities.CreateJsonSchema(typeof(Class1));

Console.WriteLine(schema.GetRawText());

class Class1 
{
    public string? Age { get; set; }
}

Output:

{
  "type": "object",
  "properties": {
    "age": {
      "type": [
        "string",
        "null"
      ]
    }
  },
  "additionalProperties": false,
  "required": [
    "age"
  ]
}

Expected behavior

A SchemaGeneration option that comply with OpenAPI 3.0 spec, generating one type and an additional Nullable boolean field in the schema.

Following the RFC (https://spec.openapis.org/oas/v3.0.3#schema-object)

Output:

{
  "type": "object",
  "properties": {
    "age": {
      "type": "string",
      "nullable": true
    }
  },
  "additionalProperties": false,
  "required": [
    "age"
  ]
}

Actual behavior

Output:

{
  "type": "object",
  "properties": {
    "age": {
      "type": [
        "string",
        "null"
      ]
    }
  },
  "additionalProperties": false,
  "required": [
    "age"
  ]
}

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

When using other types (not string) this gets trickier as the array contains multiple types for the same property.

Using int? example:

{
  "type": "object",
  "properties": {
    "age": {
      "type": [
        "string",
        "integer", 
        "null"
      ]
    }
  },
  "additionalProperties": false,
  "required": [
    "age"
  ]
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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