diff --git a/python/semantic_kernel/schema/kernel_json_schema_builder.py b/python/semantic_kernel/schema/kernel_json_schema_builder.py index 5a2e2c0200a4..67a0c90a5a0b 100644 --- a/python/semantic_kernel/schema/kernel_json_schema_builder.py +++ b/python/semantic_kernel/schema/kernel_json_schema_builder.py @@ -2,6 +2,7 @@ import sys import types +from collections.abc import Mapping, MutableMapping, MutableSequence, Sequence from enum import Enum from typing import Any, ClassVar, Literal, Union, get_args, get_origin, get_type_hints @@ -54,10 +55,10 @@ def build( return cls.build_model_schema(parameter_type, description, structured_output) if isinstance(parameter_type, type) and issubclass(parameter_type, Enum): return cls.build_enum_schema(parameter_type, description) + if get_origin(parameter_type) is not None or hasattr(parameter_type, "__args__"): + return cls.handle_complex_type(parameter_type, description, structured_output) if hasattr(parameter_type, "__annotations__"): return cls.build_model_schema(parameter_type, description, structured_output) - if hasattr(parameter_type, "__args__"): - return cls.handle_complex_type(parameter_type, description, structured_output) schema = cls.get_json_schema(parameter_type) if description: schema["description"] = description @@ -170,6 +171,9 @@ def handle_complex_type( ) -> dict[str, Any]: """Handles building the JSON schema for complex types. + Supports standard generic containers including list, set, Sequence, + MutableSequence, dict, Mapping, MutableMapping, tuple, Literal, and Union. + Args: parameter_type: The parameter type. description: The description of the parameter. Defaults to None. @@ -182,7 +186,7 @@ def handle_complex_type( args = get_args(parameter_type) schema: dict[str, Any] = {} - if origin is list or origin is set: + if origin is list or origin is set or origin is Sequence or origin is MutableSequence: item_type = args[0] schema = { "type": "array", @@ -191,7 +195,7 @@ def handle_complex_type( if description: schema["description"] = description return schema - if origin is dict: + if origin is dict or origin is Mapping or origin is MutableMapping: _, value_type = args additional_properties = cls.build(value_type, structured_output=structured_output) if additional_properties == {"type": "object"}: diff --git a/python/tests/unit/schema/test_schema_builder.py b/python/tests/unit/schema/test_schema_builder.py index 5f9687d010b2..e531b6d8b56d 100644 --- a/python/tests/unit/schema/test_schema_builder.py +++ b/python/tests/unit/schema/test_schema_builder.py @@ -1,6 +1,7 @@ # Copyright (c) Microsoft. All rights reserved. import json +from collections.abc import Mapping, MutableMapping, MutableSequence, Sequence from enum import Enum from typing import Annotated, Any, ClassVar, Literal, Optional, Union from unittest.mock import Mock @@ -251,6 +252,59 @@ def test_build_optional_variadic_tuple(): assert schema == {"type": ["array", "null"], "items": {"type": "string"}} +def test_build_sequence(): + schema = KernelJsonSchemaBuilder.build(Sequence[int], "items") + assert schema == {"type": "array", "items": {"type": "integer"}, "description": "items"} + + +def test_build_mutable_sequence(): + schema = KernelJsonSchemaBuilder.build(MutableSequence[str]) + assert schema == {"type": "array", "items": {"type": "string"}} + + +def test_build_mapping(): + schema = KernelJsonSchemaBuilder.build(Mapping[str, int], "counts") + assert schema == { + "type": "object", + "additionalProperties": {"type": "integer"}, + "description": "counts", + } + + +def test_build_mutable_mapping(): + schema = KernelJsonSchemaBuilder.build(MutableMapping[str, float]) + assert schema == { + "type": "object", + "additionalProperties": {"type": "number"}, + } + + +def test_build_optional_sequence(): + schema = KernelJsonSchemaBuilder.build(Optional[Sequence[int]]) + assert schema == {"type": ["array", "null"], "items": {"type": "integer"}} + + +def test_build_optional_mapping(): + schema = KernelJsonSchemaBuilder.build(Optional[Mapping[str, str]]) + assert schema == {"type": ["object", "null"], "additionalProperties": {"type": "string"}} + + +def test_build_sequence_and_mapping_with_structured_output(): + seq_schema = KernelJsonSchemaBuilder.build(Sequence[str], description="a list of names", structured_output=True) + assert seq_schema == { + "type": "array", + "items": {"type": "string"}, + "description": "a list of names", + } + + map_schema = KernelJsonSchemaBuilder.build(Mapping[str, int], description="lookup table", structured_output=True) + assert map_schema == { + "type": "object", + "additionalProperties": False, + "description": "lookup table", + } + + def test_build_union(): schema = KernelJsonSchemaBuilder.build(Union[int, str]) assert schema == {"anyOf": [{"type": "integer"}, {"type": "string"}]}