Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,25 @@ 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 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 = {
Expand All @@ -188,7 +206,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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading