Skip to content

Python: preserve shared Annotated parameter metadata - #14452

Open
NS Agent (nslabhwan) wants to merge 1 commit into
microsoft:mainfrom
nslabhwan:fix/python-annotated-metadata-mutation
Open

NS Agent (nslabhwan) wants to merge 1 commit into
microsoft:mainfrom
nslabhwan:fix/python-annotated-metadata-mutation

Conversation

@nslabhwan

Copy link
Copy Markdown

Motivation and Context

Fixes #14441.

@kernel_function currently mutates caller-owned dict metadata inside typing.Annotated when _parse_parameter() extracts description with pop(). Reusing the same metadata dict therefore loses the description after the first decoration and leaves the caller's dict modified.

Description

Copy dict metadata before the existing parsing logic mutates it. This keeps the current pop() / update() behavior local to the parser while preserving the caller-owned object.

Adds a regression test that reuses one metadata dict across two @kernel_function decorators and verifies that both functions retain the description and the original dict remains unchanged.

Validation run locally:

  • pytest -q tests/unit/functions — passed
  • ruff check semantic_kernel/functions/kernel_function_decorator.py tests/unit/functions/test_kernel_function_decorators.py — passed
  • ruff format --check semantic_kernel/functions/kernel_function_decorator.py tests/unit/functions/test_kernel_function_decorators.py — passed

Contribution Checklist

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.

🟢 Approval recommended

The changes are covered by regression tests and no unresolved issues remain.

Pull request overview

Fixes mutation of caller-owned Annotated metadata during kernel function parameter parsing.

Changes:

  • Copies metadata dictionaries before parsing mutations.
  • Adds regression coverage for reused metadata dictionaries.
File summaries
File Description
python/tests/unit/functions/test_kernel_function_decorators.py Verifies descriptions and shared metadata remain intact.
python/semantic_kernel/functions/kernel_function_decorator.py Prevents mutation of caller-owned metadata.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

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

@nslabhwan

Copy link
Copy Markdown
Author

CI note: the two failing workflow checks (Label pull request / add_label and DevFlow PR Review / review) both completed in ~2 seconds with zero executed steps on this fork PR. This matches the repository-wide fork PR environment-gate regression tracked in #14438, rather than a failure in this change. Local validation for this PR remains: pytest -q tests/unit/functions passed; Ruff 0.15.5 lint and format checks passed on the two changed files.

@nslabhwan

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@nslabhwan

Copy link
Copy Markdown
Author

@microsoft/octo-semantickernel-pr-python could you please review this focused Python bug fix when convenient? The CLA is now complete. The two red workflow checks are the existing fork-PR environment-gate regression tracked in #14438; the changed Python area passes tests/unit/functions and Ruff 0.15.5 locally.

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