semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

Python: @kernel_function mutates shared Annotated metadata dicts (description lost after first use)

Open ManoharPaturi opened this issue 2 months ago • 1 comments

Description

@kernel_function's _parse_parameter() calls meta.pop("description") on dict metadata inside typing.Annotated, mutating the caller's dict. Reusing one shared metadata dict across kernel functions silently strips the parameter description from every function after the first, and leaves the user's dict empty.

Reproduction

from typing import Annotated
from semantic_kernel.functions.kernel_function_decorator import kernel_function
from semantic_kernel.functions import KernelFunctionFromMethod

shared = {"description": "the answer count"}

@kernel_function
def a(x: Annotated[int, shared]) -> int: ...

@kernel_function
def b(y: Annotated[int, shared]) -> int: ...

KernelFunctionFromMethod(a, "a").metadata.parameters[0].description  # 'the answer count'
KernelFunctionFromMethod(b, "b").metadata.parameters[0].description  # None  <-- lost
shared  # {}  <-- caller's dict mutated

Expected behavior

Decoration never mutates caller-owned metadata; both functions see the description.

Environment

semantic-kernel python main (ca40aa72), Python 3.12

ManoharPaturi avatar Sep 13 '26 16:09 ManoharPaturi

I reproduced this on current main. _parse_parameter() mutates caller-owned Annotated dict metadata via meta.pop("description"), so reusing the same metadata dict loses the description after the first decoration. I'm taking this issue with a focused fix that copies the metadata dict before parsing and a regression test covering reuse across two @kernel_function decorators. Targeted function tests and Ruff checks are passing; I'll open a small PR.

nslabhwan avatar Sep 16 '26 23:09 nslabhwan