Skip to content

Commit eff85dd

Browse files
Anvit D.AnvitDevadiga
andauthored
Python: fix(python): preserve Annotated metadata in kernel functions (#14496)
Fixes #14441\n\n## Summary\n- copy Annotated dictionary metadata before extracting the description\n- preserve caller-owned metadata when the same annotation is reused\n- add a regression test covering repeated decorated functions\n\n## Validation\n- focused kernel function decorator tests: 32 passed\n- Ruff checks passed\n Co-authored-by: AnvitDevadiga <[email protected]>
1 parent 629f9a4 commit eff85dd

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎python/semantic_kernel/functions/kernel_function_decorator.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def _parse_parameter(name: str, param: Any, default: Any) -> dict[str, Any]:
154154
if isinstance(meta, str):
155155
ret["description"] = meta
156156
elif isinstance(meta, dict):
157+
meta = meta.copy()
157158
# only override from the metadata if it is not already set
158159
if "description" not in ret and (description := meta.pop("description", None)):
159160
ret["description"] = description

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,22 @@ def test_kernel_function_param_annotated():
139139
assert my_func.__kernel_function_parameters__[0]["name"] == "input"
140140

141141

142+
def test_kernel_function_does_not_mutate_shared_annotated_metadata():
143+
shared_metadata = {"description": "the answer count"}
144+
145+
@kernel_function
146+
def first(value: Annotated[int, shared_metadata]) -> int:
147+
return value
148+
149+
@kernel_function
150+
def second(value: Annotated[int, shared_metadata]) -> int:
151+
return value
152+
153+
assert first.__kernel_function_parameters__[0]["description"] == "the answer count"
154+
assert second.__kernel_function_parameters__[0]["description"] == "the answer count"
155+
assert shared_metadata == {"description": "the answer count"}
156+
157+
142158
def test_kernel_function_param_optional():
143159
decorator_test = MiscClass()
144160
my_func = getattr(decorator_test, "func_input_optional")

0 commit comments

Comments
 (0)