From ff7fbf2d7113462759eeb72c293858c0be212b9d Mon Sep 17 00:00:00 2001 From: ump45nose Date: Mon, 5 Oct 2026 00:37:51 +0800 Subject: [PATCH 1/2] fix(python): preserve optional agent-framework tool parameter defaults --- .../functions/kernel_function.py | 7 ++++-- .../test_kernel_function_from_method.py | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/python/semantic_kernel/functions/kernel_function.py b/python/semantic_kernel/functions/kernel_function.py index 15f2ab34c6b7..48c626a3ef2c 100644 --- a/python/semantic_kernel/functions/kernel_function.py +++ b/python/semantic_kernel/functions/kernel_function.py @@ -447,12 +447,15 @@ def as_agent_framework_tool( fields = {} for param in self.parameters: if param.include_in_function_choices: - if param.default_value is not None: + # Requiredness is explicit metadata: None can be a legitimate + # default for optional parameters and must remain a Pydantic default. + if param.is_required: + fields[param.name] = (param.type_, Field(description=param.description)) + else: fields[param.name] = ( param.type_, Field(description=param.description, default=param.default_value), ) - fields[param.name] = (param.type_, Field(description=param.description)) input_model = create_model("InputModel", **fields) # type: ignore async def wrapper(*args: Any, **kwargs: Any) -> Any: diff --git a/python/tests/unit/functions/test_kernel_function_from_method.py b/python/tests/unit/functions/test_kernel_function_from_method.py index 3b868625d87c..3231fd157fd7 100644 --- a/python/tests/unit/functions/test_kernel_function_from_method.py +++ b/python/tests/unit/functions/test_kernel_function_from_method.py @@ -140,6 +140,28 @@ def decorated_function() -> None: assert len(native_function.parameters) == 0 +def test_as_agent_framework_tool_preserves_optional_parameter_default(monkeypatch): + class MockAIFunction: + def __init__(self, *, input_model: Any, **kwargs: Any): + self.input_model = input_model + + monkeypatch.setitem(__import__("sys").modules, "agent_framework", Mock(AIFunction=MockAIFunction)) + + @kernel_function() + def function(count: int = 5, label: str | None = None) -> int: + return count + + native_function = KernelFunction.from_method(method=function, plugin_name="MockPlugin") + tool = native_function.as_agent_framework_tool(kernel=Mock()) + count_field = tool.input_model.model_fields["count"] + label_field = tool.input_model.model_fields["label"] + + assert count_field.default == 5 + assert not count_field.is_required() + assert label_field.default is None + assert not label_field.is_required() + + def test_init_method_is_none(): with pytest.raises(FunctionInitializationError): KernelFunction.from_method(method=None, plugin_name="MockPlugin") From 87a81fddd18375659f8ff68a131457191cb78ef3 Mon Sep 17 00:00:00 2001 From: Vincent <1729065730@qq.com> Date: Thu, 5 Nov 2026 17:30:43 +0800 Subject: [PATCH 2/2] fix(python): honor callable defaults in agent framework tools Build agent-framework Fields from the underlying method signature so nullable annotations without a default stay required, while `T | None = None` remains optional with a real None default. --- .../functions/kernel_function.py | 42 ++++++++++++++----- .../test_kernel_function_from_method.py | 28 +++++++++++++ 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/python/semantic_kernel/functions/kernel_function.py b/python/semantic_kernel/functions/kernel_function.py index 67835830c91b..fb7281c0c982 100644 --- a/python/semantic_kernel/functions/kernel_function.py +++ b/python/semantic_kernel/functions/kernel_function.py @@ -6,8 +6,8 @@ from abc import abstractmethod from collections.abc import AsyncGenerator, Callable, Mapping, Sequence from copy import copy, deepcopy -from inspect import isasyncgen, isgenerator -from typing import TYPE_CHECKING, Any +from inspect import Parameter, isasyncgen, isgenerator, signature +from typing import TYPE_CHECKING, Annotated, Any from opentelemetry import metrics, trace from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE @@ -449,17 +449,39 @@ def as_agent_framework_tool( name = name or self.name description = description or self.description fields = {} + # Prefer the underlying callable signature when present so nullable + # annotations (str | None) stay distinct from optional defaults (= None), + # and so Field annotations remain nullable when None is allowed. + method = getattr(self, "method", None) + sig_params = signature(method).parameters if callable(method) else {} for param in self.parameters: - if param.include_in_function_choices: - # Requiredness is explicit metadata: None can be a legitimate - # default for optional parameters and must remain a Pydantic default. - if param.is_required: - fields[param.name] = (param.type_, Field(description=param.description)) - else: + if not param.include_in_function_choices: + continue + sp = sig_params.get(param.name) if param.name else None + if sp is not None: + annotation: Any = ( + sp.annotation if sp.annotation is not Parameter.empty else (param.type_object or Any) + ) + origin = getattr(annotation, "__origin__", None) + if origin is Annotated: + annotation = annotation.__args__[0] + if sp.default is not Parameter.empty: fields[param.name] = ( - param.type_, - Field(description=param.description, default=param.default_value), + annotation, + Field(description=param.description, default=sp.default), ) + else: + fields[param.name] = (annotation, Field(description=param.description)) + continue + + annotation = param.type_object or param.type_ or Any + if param.is_required: + fields[param.name] = (annotation, Field(description=param.description)) + else: + fields[param.name] = ( + annotation, + Field(description=param.description, default=param.default_value), + ) input_model = create_model("InputModel", **fields) # type: ignore async def wrapper(*args: Any, **kwargs: Any) -> Any: diff --git a/python/tests/unit/functions/test_kernel_function_from_method.py b/python/tests/unit/functions/test_kernel_function_from_method.py index 08dc325d0537..18a5cb41b036 100644 --- a/python/tests/unit/functions/test_kernel_function_from_method.py +++ b/python/tests/unit/functions/test_kernel_function_from_method.py @@ -191,6 +191,34 @@ def function(count: int = 5, label: str | None = None) -> int: assert not count_field.is_required() assert label_field.default is None assert not label_field.is_required() + assert tool.input_model().label is None + assert tool.input_model(label=None).label is None + + +def test_as_agent_framework_tool_keeps_nullable_without_default_required(monkeypatch): + from pydantic import ValidationError + + class MockAIFunction: + def __init__(self, *, input_model: Any, **kwargs: Any): + self.input_model = input_model + + monkeypatch.setitem(__import__("sys").modules, "agent_framework", Mock(AIFunction=MockAIFunction)) + + @kernel_function() + def function(x: str | None) -> str | None: + return x + + native_function = KernelFunction.from_method(method=function, plugin_name="MockPlugin") + tool = native_function.as_agent_framework_tool(kernel=Mock()) + x_field = tool.input_model.model_fields["x"] + assert x_field.is_required() + assert tool.input_model(x=None).x is None + try: + tool.input_model() + raised = False + except ValidationError: + raised = True + assert raised, "nullable-without-default must reject a missing argument" def test_init_method_is_none():