Skip to content

Commit 9a70006

Browse files
authored
Python: FIX: honor overwrite=False for default execution settings in PromptTemplateConfig (#14586)
## Why `PromptTemplateConfig.add_execution_settings(settings, overwrite=False)` still replaced the existing entry when `settings.service_id` is None (the common case). Fixes #14582. ## Root cause The membership check used `settings.service_id` (None) while the entry is stored under `settings.service_id or DEFAULT_SERVICE_NAME` ("default"). `None in self.execution_settings` is always False, so `overwrite=False` never protected the default entry. The "already exist" warning was also logged after every successful add, and never when an add was actually refused. ## Fix Use the same resolved key (`settings.service_id or DEFAULT_SERVICE_NAME`) for both the membership check and the store, and emit the warning only on the refused path. ## Tests - `test_add_execution_settings_no_overwrite_default_service`: `overwrite=False` keeps the original default settings. - `test_add_execution_settings_warning_only_when_refused`: warning appears only when an add is refused, not on a fresh add. Both fail on the unpatched code and pass with the fix; the 18 pre-existing tests in the file still pass. --------- Co-authored-by: inchang-ing <[email protected]>
1 parent b5b526d commit 9a70006

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎python/semantic_kernel/prompt_template/prompt_template_config.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ def rewrite_execution_settings(
7171

7272
def add_execution_settings(self, settings: PromptExecutionSettings, overwrite: bool = True) -> None:
7373
"""Add execution settings to the prompt template."""
74-
if settings.service_id in self.execution_settings and not overwrite:
74+
# Check existence with the same key that storage uses: when service_id is None the settings are
75+
# stored under DEFAULT_SERVICE_NAME, so testing service_id (None) never matches and overwrite=False
76+
# silently has no effect (see issue #14582).
77+
key = settings.service_id or DEFAULT_SERVICE_NAME
78+
if key in self.execution_settings and not overwrite:
79+
logger.warning("Execution settings already exist and overwrite is set to False")
7580
return
76-
self.execution_settings[settings.service_id or DEFAULT_SERVICE_NAME] = settings
77-
logger.warning("Execution settings already exist and overwrite is set to False")
81+
self.execution_settings[key] = settings
7882

7983
def get_kernel_parameter_metadata(self) -> Sequence[KernelParameterMetadata]:
8084
"""Get the kernel parameter metadata for the input variables."""

‎python/tests/unit/prompt_template/test_prompt_templates.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import json
5+
import logging
56

67
import yaml
78
from pytest import raises
@@ -72,6 +73,32 @@ def test_add_execution_settings_with_overwrite():
7273
assert config.execution_settings["test"].extension_data["setting_value"] == "new_value2"
7374

7475

76+
def test_add_execution_settings_no_overwrite_default_service():
77+
"""overwrite=False protects the entry stored under DEFAULT_SERVICE_NAME (service_id is None)."""
78+
config = PromptTemplateConfig(template="Example template")
79+
first = PromptExecutionSettings(extension_data={"temperature": 0.1})
80+
config.add_execution_settings(first)
81+
assert config.execution_settings["default"] == first
82+
83+
second = PromptExecutionSettings(extension_data={"temperature": 0.9})
84+
config.add_execution_settings(second, overwrite=False)
85+
assert config.execution_settings["default"].extension_data["temperature"] == 0.1
86+
87+
88+
def test_add_execution_settings_warning_only_when_refused(caplog):
89+
"""The "already exists" warning is emitted only when an add is refused, not after every add."""
90+
with caplog.at_level(logging.WARNING):
91+
config = PromptTemplateConfig(template="Example template")
92+
settings = PromptExecutionSettings(extension_data={"temperature": 0.1})
93+
config.add_execution_settings(settings, overwrite=False)
94+
assert "overwrite is set to False" not in caplog.text
95+
96+
caplog.clear()
97+
other = PromptExecutionSettings(extension_data={"temperature": 0.9})
98+
config.add_execution_settings(other, overwrite=False)
99+
assert "overwrite is set to False" in caplog.text
100+
101+
75102
def test_get_kernel_parameter_metadata_empty():
76103
config = PromptTemplateConfig(template="Example template")
77104
metadata = config.get_kernel_parameter_metadata()

0 commit comments

Comments
 (0)