diff --git a/python/semantic_kernel/schema/kernel_json_schema_builder.py b/python/semantic_kernel/schema/kernel_json_schema_builder.py index 5ec519b5b377..03b9e73fb473 100644 --- a/python/semantic_kernel/schema/kernel_json_schema_builder.py +++ b/python/semantic_kernel/schema/kernel_json_schema_builder.py @@ -18,6 +18,7 @@ dict: "object", set: "array", tuple: "array", + type(None): "null", "int": "integer", "str": "string", "bool": "boolean", @@ -28,6 +29,8 @@ "tuple": "array", "object": "object", "array": "array", + "None": "null", + "NoneType": "null", } diff --git a/python/tests/unit/schema/test_schema_builder.py b/python/tests/unit/schema/test_schema_builder.py index 5d24a599c96c..91c521150a71 100644 --- a/python/tests/unit/schema/test_schema_builder.py +++ b/python/tests/unit/schema/test_schema_builder.py @@ -228,6 +228,27 @@ def test_build_optional(): assert schema == {"type": ["integer", "null"]} +def test_get_json_schema_none_type(): + # NoneType maps to the JSON Schema "null" type, not "object". + assert KernelJsonSchemaBuilder.get_json_schema(type(None)) == {"type": "null"} + + +def test_build_none_type(): + assert KernelJsonSchemaBuilder.build(type(None)) == {"type": "null"} + + +def test_build_union_with_none(): + # A union of more than two members that includes None must represent the + # None member as {"type": "null"}, not {"type": "object"}. + schema = KernelJsonSchemaBuilder.build(Union[int, str, None]) + assert schema == {"anyOf": [{"type": "integer"}, {"type": "string"}, {"type": "null"}]} + + +def test_build_from_type_name_none(): + assert KernelJsonSchemaBuilder.build_from_type_name("None") == {"type": "null"} + assert KernelJsonSchemaBuilder.build_from_type_name("NoneType") == {"type": "null"} + + def test_build_model_schema_for_many_types(): schema = KernelJsonSchemaBuilder.build(MockModel) expected = """