From 1d02a012817f9b80d62580e0e92c30b265847e4a Mon Sep 17 00:00:00 2001 From: DZH166 <248446924+DZH166@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:40:55 +0800 Subject: [PATCH 1/2] Python: default dict function_choice_behavior to auto-invoking for auto and required --- .../connectors/ai/function_choice_behavior.py | 9 ++++-- .../ai/test_function_choice_behavior.py | 29 +++++++++++++++++++ .../ai/test_prompt_execution_settings.py | 19 ++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/python/semantic_kernel/connectors/ai/function_choice_behavior.py b/python/semantic_kernel/connectors/ai/function_choice_behavior.py index 18f4e53e9b04..df7bc40a5311 100644 --- a/python/semantic_kernel/connectors/ai/function_choice_behavior.py +++ b/python/semantic_kernel/connectors/ai/function_choice_behavior.py @@ -179,7 +179,12 @@ def Required( @classmethod def from_dict(cls: type[_T], data: dict) -> _T: - """Create a FunctionChoiceBehavior from a dictionary.""" + """Create a FunctionChoiceBehavior from a dictionary. + + The default for `auto_invoke` matches `from_string` and the classmethods: + auto-invocation is enabled for the "auto" and "required" types and disabled + for "none". An explicit `auto_invoke` key in the dict always wins. + """ from semantic_kernel.connectors.ai.function_calling_utils import _combine_filter_dicts type_map = { @@ -188,7 +193,7 @@ def from_dict(cls: type[_T], data: dict) -> _T: "required": cls.Required, } behavior_type = data.pop("type", "auto") - auto_invoke = data.pop("auto_invoke", False) + auto_invoke = data.pop("auto_invoke", behavior_type != "none") functions = data.pop("functions", None) filters = data.pop("filters", None) diff --git a/python/tests/unit/connectors/ai/test_function_choice_behavior.py b/python/tests/unit/connectors/ai/test_function_choice_behavior.py index 58787be12988..e606445f5b74 100644 --- a/python/tests/unit/connectors/ai/test_function_choice_behavior.py +++ b/python/tests/unit/connectors/ai/test_function_choice_behavior.py @@ -94,6 +94,35 @@ def test_auto_function_choice_behavior_from_dict_with_different_filters_and_func assert behavior.maximum_auto_invoke_attempts == max_auto_invoke_attempts +@pytest.mark.parametrize( + ("data", "expected_attempts"), + [ + ({"type": "auto"}, DEFAULT_MAX_AUTO_INVOKE_ATTEMPTS), + ({"type": "required"}, 1), + ({"type": "none"}, 0), + ({"type": "auto", "auto_invoke": False}, 0), + ({"type": "auto", "auto_invoke": True}, DEFAULT_MAX_AUTO_INVOKE_ATTEMPTS), + ({"type": "auto", "maximum_auto_invoke_attempts": 3}, 3), + ], +) +def test_from_dict_defaults_auto_invoke_consistently(data: dict, expected_attempts: int): + """from_dict defaults should match from_string and the classmethods. + + `{"type": "auto"}` must behave like `from_string("auto")` and `Auto()`, and + `{"type": "required"}` like `Required()`; an explicit `auto_invoke` key still wins. + """ + behavior = FunctionChoiceBehavior.from_dict(dict(data)) + assert behavior.maximum_auto_invoke_attempts == expected_attempts + assert behavior.auto_invoke_kernel_functions is (expected_attempts > 0) + + +def test_from_dict_matches_from_string_for_auto(): + assert ( + FunctionChoiceBehavior.from_dict({"type": "auto"}).maximum_auto_invoke_attempts + == FunctionChoiceBehavior.from_string("auto").maximum_auto_invoke_attempts + ) + + def test_function_choice_behavior_get_set(function_choice_behavior: FunctionChoiceBehavior): function_choice_behavior.enable_kernel_functions = False assert function_choice_behavior.enable_kernel_functions is False diff --git a/python/tests/unit/connectors/ai/test_prompt_execution_settings.py b/python/tests/unit/connectors/ai/test_prompt_execution_settings.py index fae89e44425b..b38e60e383ee 100644 --- a/python/tests/unit/connectors/ai/test_prompt_execution_settings.py +++ b/python/tests/unit/connectors/ai/test_prompt_execution_settings.py @@ -14,3 +14,22 @@ def test_init_with_data(): settings = PromptExecutionSettings(service_id="test", extension_data=ext_data) assert settings.service_id == "test" assert settings.extension_data["test"] == "test" + + +def test_function_choice_behavior_dict_keeps_auto_invoke_default(): + """A dict function_choice_behavior of type 'auto' must keep auto-invocation enabled. + + It should parse the same way as the string form ("auto"), which yields + maximum_auto_invoke_attempts == DEFAULT_MAX_AUTO_INVOKE_ATTEMPTS. + """ + settings = PromptExecutionSettings(function_choice_behavior={"type": "auto"}) + assert settings.function_choice_behavior is not None + assert settings.function_choice_behavior.type_.value == "auto" + assert settings.function_choice_behavior.auto_invoke_kernel_functions is True + + +def test_function_choice_behavior_dict_explicit_auto_invoke_false_is_kept(): + """An explicit `auto_invoke: False` in the dict still disables auto-invocation.""" + settings = PromptExecutionSettings(function_choice_behavior={"type": "auto", "auto_invoke": False}) + assert settings.function_choice_behavior is not None + assert settings.function_choice_behavior.auto_invoke_kernel_functions is False From 2b12e2bbeb3d0425cdf1575d89b1a7a4bbdf36bb Mon Sep 17 00:00:00 2001 From: DZH166 <248446924+DZH166@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:24:16 +0800 Subject: [PATCH 2/2] docs(fcb): document the two cases where auto_invoke=False does not disable invocation from_dict() lets an explicit maximum_auto_invoke_attempts win over auto_invoke, and the none behavior ignores auto_invoke entirely. Spell both out, and copy the input dict so from_dict() does not strip the caller's keys. --- .../connectors/ai/function_choice_behavior.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/python/semantic_kernel/connectors/ai/function_choice_behavior.py b/python/semantic_kernel/connectors/ai/function_choice_behavior.py index df7bc40a5311..217c87fe44fd 100644 --- a/python/semantic_kernel/connectors/ai/function_choice_behavior.py +++ b/python/semantic_kernel/connectors/ai/function_choice_behavior.py @@ -183,8 +183,21 @@ def from_dict(cls: type[_T], data: dict) -> _T: The default for `auto_invoke` matches `from_string` and the classmethods: auto-invocation is enabled for the "auto" and "required" types and disabled - for "none". An explicit `auto_invoke` key in the dict always wins. + for "none". + + An explicit `auto_invoke` key overrides that default, with two exceptions: + + * an explicit `maximum_auto_invoke_attempts` in the same dict wins, because the + classmethods only fill the count in when it is absent -- so + `{"type": "auto", "auto_invoke": False, "maximum_auto_invoke_attempts": 3}` + still auto-invokes. Leave the count out when `auto_invoke=False` is meant to + disable invocation for "auto" and "required". + * the "none" behavior ignores `auto_invoke` entirely: `NoneInvoke()` always sets + the attempt count to 0, whatever the dict says. + + The input dict is copied, so its keys are left in place for the caller. """ + data = dict(data) from semantic_kernel.connectors.ai.function_calling_utils import _combine_filter_dicts type_map = {