diff --git a/python/semantic_kernel/prompt_template/prompt_template_config.py b/python/semantic_kernel/prompt_template/prompt_template_config.py index 7124f73e5e38..da836c173cca 100644 --- a/python/semantic_kernel/prompt_template/prompt_template_config.py +++ b/python/semantic_kernel/prompt_template/prompt_template_config.py @@ -71,10 +71,14 @@ def rewrite_execution_settings( def add_execution_settings(self, settings: PromptExecutionSettings, overwrite: bool = True) -> None: """Add execution settings to the prompt template.""" - if settings.service_id in self.execution_settings and not overwrite: + # Check existence with the same key that storage uses: when service_id is None the settings are + # stored under DEFAULT_SERVICE_NAME, so testing service_id (None) never matches and overwrite=False + # silently has no effect (see issue #14582). + key = settings.service_id or DEFAULT_SERVICE_NAME + if key in self.execution_settings and not overwrite: + logger.warning("Execution settings already exist and overwrite is set to False") return - self.execution_settings[settings.service_id or DEFAULT_SERVICE_NAME] = settings - logger.warning("Execution settings already exist and overwrite is set to False") + self.execution_settings[key] = settings def get_kernel_parameter_metadata(self) -> Sequence[KernelParameterMetadata]: """Get the kernel parameter metadata for the input variables.""" diff --git a/python/tests/unit/prompt_template/test_prompt_templates.py b/python/tests/unit/prompt_template/test_prompt_templates.py index 80e345f2431c..61916c5c8033 100644 --- a/python/tests/unit/prompt_template/test_prompt_templates.py +++ b/python/tests/unit/prompt_template/test_prompt_templates.py @@ -2,6 +2,7 @@ import json +import logging import yaml from pytest import raises @@ -72,6 +73,32 @@ def test_add_execution_settings_with_overwrite(): assert config.execution_settings["test"].extension_data["setting_value"] == "new_value2" +def test_add_execution_settings_no_overwrite_default_service(): + """overwrite=False protects the entry stored under DEFAULT_SERVICE_NAME (service_id is None).""" + config = PromptTemplateConfig(template="Example template") + first = PromptExecutionSettings(extension_data={"temperature": 0.1}) + config.add_execution_settings(first) + assert config.execution_settings["default"] == first + + second = PromptExecutionSettings(extension_data={"temperature": 0.9}) + config.add_execution_settings(second, overwrite=False) + assert config.execution_settings["default"].extension_data["temperature"] == 0.1 + + +def test_add_execution_settings_warning_only_when_refused(caplog): + """The "already exists" warning is emitted only when an add is refused, not after every add.""" + with caplog.at_level(logging.WARNING): + config = PromptTemplateConfig(template="Example template") + settings = PromptExecutionSettings(extension_data={"temperature": 0.1}) + config.add_execution_settings(settings, overwrite=False) + assert "overwrite is set to False" not in caplog.text + + caplog.clear() + other = PromptExecutionSettings(extension_data={"temperature": 0.9}) + config.add_execution_settings(other, overwrite=False) + assert "overwrite is set to False" in caplog.text + + def test_get_kernel_parameter_metadata_empty(): config = PromptTemplateConfig(template="Example template") metadata = config.get_kernel_parameter_metadata()