Skip to content

Commit c057217

Browse files
committed
Python: skip coercion for None arguments
Explicit None for optional parameters crashed or became the string None.
1 parent b714e97 commit c057217

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎python/semantic_kernel/functions/kernel_function_from_method.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ def gather_function_parameters(self, context: FunctionInvocationContext) -> dict
177177
if param.name in context.arguments:
178178
value: Any = context.arguments[param.name]
179179
if (
180-
param.type_
180+
value is not None
181+
and param.type_
181182
and "," not in param.type_
182183
and param.type_object
183184
and param.type_object is not inspect._empty

‎python/tests/unit/functions/test_kernel_function_from_method.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,24 @@ async def override_stream(stream):
682682
]
683683

684684

685+
async def test_explicit_none_for_optional_parameters_is_not_coerced(kernel: Kernel):
686+
@kernel_function
687+
def func_optional(
688+
count: int | None = None,
689+
label: Annotated[str | None, "A label"] = None,
690+
item: CustomTypeDataclass | None = None,
691+
) -> Any:
692+
return (count, label, item)
693+
694+
func = kernel.add_function(plugin_name="test", function=func_optional)
695+
696+
res = await kernel.invoke(func, arguments=KernelArguments(count=None, label=None, item=None))
697+
assert res.value == (None, None, None)
698+
699+
res = await kernel.invoke(func, arguments=KernelArguments(count="3", label="x"))
700+
assert res.value == (3, "x", None)
701+
702+
685703
async def test_default_handling(kernel: Kernel):
686704
@kernel_function
687705
def func_default(input: str = "test"):

0 commit comments

Comments
 (0)