Skip to content

Python: fix: do not mutate KernelArguments execution settings when selecting an AI service - #14838

Open
DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:fix/ai-service-selector-kwargs-mutation
Open

DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:fix/ai-service-selector-kwargs-mutation

Conversation

@DZH166

@DZH166 DZH166 commented Nov 20, 2026

Copy link
Copy Markdown

Problem

AIServiceSelector.select_ai_service() seeds its working dict from the caller's KernelArguments.execution_settings and then writes the function's own prompt_execution_settings into that same object:

execution_settings = arguments.execution_settings or {}
...
execution_settings[service_id] = function.prompt_execution_settings

KernelArguments keeps the dict it was given by reference, so the caller's object is mutated. Because the id lookup iterates the merged dict in insertion order, a later selection with the same arguments finds the previous function's service id first and resolves the wrong service/settings - silently.

before: after first call arguments.execution_settings == ['service-a', 'unrelated'];
        second function resolved to 'service-a' (expected 'service-b')
after : arguments untouched; second function resolves to 'service-b'

Fix

Work on a copy of the caller's settings so selection never writes back into KernelArguments.

Tests

tests/unit/services/test_ai_service_selector.py (new case; the existing selector/content suites stay green):

pytest tests/unit/services/test_ai_service_selector.py tests/unit/contents/test_chat_message_content.py -q
# before: 2 failed, 53 passed   after: 55 passed

@DZH166
DZH166 requested a review from a team as a code owner November 20, 2026 16:49
Copilot AI balanced review requested due to automatic review settings November 20, 2026 16:49
@DZH166
DZH166 temporarily deployed to github-app-auth November 20, 2026 16:50 — with GitHub Actions Inactive

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

No active review issues were identified in the change addressing stale service selection when reusing KernelArguments.

No open issues 🎉

What changed in this PR

Fixes Python AI service selection so reusing KernelArguments does not select a service left over from a previous function call.

Changes:

  • Copies the caller’s execution-settings mapping before merging function settings, preserving existing selection precedence.
  • Adds a regression test verifying that sequential selections choose the correct services and leave caller-owned mappings unchanged.
File Description
python/​tests/​unit/​services/​test_ai_service_selector.py Tests sequential service selection and caller-settings isolation.
python/​semantic_kernel/​services/​ai_service_selector.py Copies the settings mapping before merging function defaults.

Review effort: Balanced

This branch was previously deployed

1 inactive deployment
github-app-auth — af9befae Deployed Nov 20, 2026 by DZH166 via add_label #29765
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.

2 participants