diff --git a/python/semantic_kernel/schema/kernel_json_schema_builder.py b/python/semantic_kernel/schema/kernel_json_schema_builder.py index 5ec519b5b377..883efa649242 100644 --- a/python/semantic_kernel/schema/kernel_json_schema_builder.py +++ b/python/semantic_kernel/schema/kernel_json_schema_builder.py @@ -92,9 +92,9 @@ def build_model_schema( if isinstance(field_info.metadata, dict): field_description = field_info.metadata.get("description") elif isinstance(field_info.metadata, list) and field_info.metadata: - field_description = field_info.metadata[0] - elif hasattr(field_info, "description"): - field_description = field_info.description + field_description = next((item for item in field_info.metadata if isinstance(item, str)), None) + if field_description is None: + field_description = getattr(field_info, "description", None) if not cls._is_optional(field_type): required.append(field_name) properties[field_name] = cls.build(field_type, field_description, structured_output) diff --git a/python/tests/unit/schema/test_schema_builder.py b/python/tests/unit/schema/test_schema_builder.py index 5d24a599c96c..2f14adeab940 100644 --- a/python/tests/unit/schema/test_schema_builder.py +++ b/python/tests/unit/schema/test_schema_builder.py @@ -8,7 +8,7 @@ import pytest from semantic_kernel.connectors.utils.structured_output_schema import generate_structured_output_response_format_schema -from semantic_kernel.kernel_pydantic import KernelBaseModel +from semantic_kernel.kernel_pydantic import Field, KernelBaseModel from semantic_kernel.schema.kernel_json_schema_builder import KernelJsonSchemaBuilder @@ -156,6 +156,26 @@ def test_build_model_schema(): assert result == expected_schema +def test_build_model_schema_with_constrained_field_with_description(): + class Params(KernelBaseModel): + top_p: float = Field(default=0.9, ge=0.0, le=1.0, description="nucleus sampling") + + schema = KernelJsonSchemaBuilder.build_model_schema(Params) + + assert schema["properties"]["top_p"] == {"type": "number", "description": "nucleus sampling"} + json.dumps(schema) + + +def test_build_model_schema_with_constrained_field_without_description(): + class Params(KernelBaseModel): + top_p: float = Field(default=0.9, ge=0.0, le=1.0) + + schema = KernelJsonSchemaBuilder.build_model_schema(Params) + + assert schema["properties"]["top_p"] == {"type": "number"} + json.dumps(schema) + + def test_build_from_type_name(): expected_schema = {"type": "string", "description": "A simple string"} result = KernelJsonSchemaBuilder.build_from_type_name("str", description="A simple string")