Skip to content

Commit 65dcb80

Browse files
committed
fix(python): preserve agent tool parameter defaults
1 parent f8c5ba7 commit 65dcb80

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎python/semantic_kernel/functions/kernel_function.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ def as_agent_framework_tool(
452452
param.type_,
453453
Field(description=param.description, default=param.default_value),
454454
)
455-
fields[param.name] = (param.type_, Field(description=param.description))
455+
else:
456+
fields[param.name] = (param.type_, Field(description=param.description))
456457
input_model = create_model("InputModel", **fields) # type: ignore
457458

458459
async def wrapper(*args: Any, **kwargs: Any) -> Any:

‎python/tests/unit/functions/test_kernel_function_from_method.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,25 @@ def decorated_function() -> None:
140140
assert len(native_function.parameters) == 0
141141

142142

143+
def test_as_agent_framework_tool_preserves_optional_parameter_default(monkeypatch):
144+
class MockAIFunction:
145+
def __init__(self, *, input_model: Any, **kwargs: Any):
146+
self.input_model = input_model
147+
148+
monkeypatch.setitem(__import__("sys").modules, "agent_framework", Mock(AIFunction=MockAIFunction))
149+
150+
@kernel_function()
151+
def function(count: int = 5) -> int:
152+
return count
153+
154+
native_function = KernelFunction.from_method(method=function, plugin_name="MockPlugin")
155+
tool = native_function.as_agent_framework_tool(kernel=Mock())
156+
field = tool.input_model.model_fields["count"]
157+
158+
assert field.default == 5
159+
assert not field.is_required()
160+
161+
143162
def test_init_method_is_none():
144163
with pytest.raises(FunctionInitializationError):
145164
KernelFunction.from_method(method=None, plugin_name="MockPlugin")

0 commit comments

Comments
 (0)