Skip to content

fix: @kernel_function: do not mutate shared Annotated metadata dicts - #14479

Open
Wu Shuwen (dajiaohuang) wants to merge 1 commit into
microsoft:mainfrom
dajiaohuang:fix/kernel-function-metadata-mutation-v2
Open

Wu Shuwen (dajiaohuang) wants to merge 1 commit into
microsoft:mainfrom
dajiaohuang:fix/kernel-function-metadata-mutation-v2

Conversation

@dajiaohuang

Copy link
Copy Markdown

Description

_parse_parameter() called meta.pop("description") on the typing.Annotated metadata dict, which mutates the caller's dict. When one shared metadata dict is reused across multiple kernel functions, the description is lost from all functions after the first decoration.

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: ...

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

Fix

Replace meta.pop("description") with meta.get("description") so the shared dict is never modified.

Fixes #14441.

_parse_parameter() called meta.pop('description') on the typing.Annotated
metadata dict, which mutates the caller's dict. When one shared metadata
dict is reused across multiple kernel functions, the description is lost
from all functions after the first decoration.

Replace pop with get() so the shared dict is never modified.

Fixes microsoft#14441.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Exclude description from bulk metadata updates to preserve existing description precedence.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

Fixes mutation of shared Annotated metadata dictionaries during kernel function decoration.

Changes:

  • Replaces destructive metadata access with non-mutating lookup.
  • Preserves caller-owned metadata for reuse.
File Description
python/​semantic_kernel/​functions/​kernel_function_decorator.py Updates metadata description handling; preserve existing description precedence during bulk updates.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +155 to 157
if "description" not in ret and (description := meta.get("description")):
ret["description"] = description
ret.update(meta)

This branch had an error being deployed

1 failed deployment
github-app-auth — e618add8 Deployed Sep 25, 2026 by dajiaohuang via review #539
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants