Skip to content

Commit 5cb90de

Browse files
committed
fix(python): [Semantic Kernel/Agent Framework] 保留 None 默认值
- 依据参数必填元数据构造 Pydantic 字段 - 覆盖显式 None 与非 None 两类可选默认值
1 parent 65dcb80 commit 5cb90de

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

‎python/semantic_kernel/functions/kernel_function.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,15 @@ def as_agent_framework_tool(
447447
fields = {}
448448
for param in self.parameters:
449449
if param.include_in_function_choices:
450-
if param.default_value is not None:
450+
# Requiredness is explicit metadata: None can be a legitimate
451+
# default for optional parameters and must remain a Pydantic default.
452+
if param.is_required:
453+
fields[param.name] = (param.type_, Field(description=param.description))
454+
else:
451455
fields[param.name] = (
452456
param.type_,
453457
Field(description=param.description, default=param.default_value),
454458
)
455-
else:
456-
fields[param.name] = (param.type_, Field(description=param.description))
457459
input_model = create_model("InputModel", **fields) # type: ignore
458460

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

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,18 @@ def __init__(self, *, input_model: Any, **kwargs: Any):
148148
monkeypatch.setitem(__import__("sys").modules, "agent_framework", Mock(AIFunction=MockAIFunction))
149149

150150
@kernel_function()
151-
def function(count: int = 5) -> int:
151+
def function(count: int = 5, label: str | None = None) -> int:
152152
return count
153153

154154
native_function = KernelFunction.from_method(method=function, plugin_name="MockPlugin")
155155
tool = native_function.as_agent_framework_tool(kernel=Mock())
156-
field = tool.input_model.model_fields["count"]
156+
count_field = tool.input_model.model_fields["count"]
157+
label_field = tool.input_model.model_fields["label"]
157158

158-
assert field.default == 5
159-
assert not field.is_required()
159+
assert count_field.default == 5
160+
assert not count_field.is_required()
161+
assert label_field.default is None
162+
assert not label_field.is_required()
160163

161164

162165
def test_init_method_is_none():

0 commit comments

Comments
 (0)