Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Python: [Python] as_agent_framework_tool drops parameter defaults (optionals become required)

未关闭 适合新手
#14,481 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
api

调研方向

从 semantic_kernel/functions/kernel_function.py 中的 as_agent_framework_tool 及其字段构建循环开始。运行 issue 中的最小复现,或检查生成的 InputModel schema,以确认当前必需的字段。完成的标准是:带有默认值的参数仍保持可选,并保留 top_k=5 和 include_meta=False,而 query 仍为必需字段。

由索引模型根据 Issue 内容生成。

描述

python triage

Description

KernelFunction.as_agent_framework_tool builds an agent-framework InputModel but drops parameter defaults. After assigning a Field(..., default=...) for optional params, the next line unconditionally overwrites the same key with a no-default Field, so every optional becomes required in the JSON schema.

Environment

  • semantic-kernel==1.44.1 (Python)
  • Python 3.13

Minimal repro

The bug is in the field-building loop (exact code from as_agent_framework_tool):

from typing import Annotated
from pydantic import Field, create_model
from semantic_kernel.functions import KernelPlugin, kernel_function

@kernel_function(name="search", description="search things")
def search(
    query: Annotated[str, "search query"],
    top_k: Annotated[int, "number of results"] = 5,
    include_meta: Annotated[bool, "include metadata"] = False,
) -> str:
    return f"{query}:{top_k}:{include_meta}"

fn = KernelPlugin(name="S", functions=[search])["search"]

fields = {}
for param in fn.parameters:
    if param.include_in_function_choices:
        if param.default_value is not None:
            fields[param.name] = (
                param.type_,
                Field(description=param.description, default=param.default_value),
            )
        fields[param.name] = (param.type_, Field(description=param.description))  # overwrites

Model = create_model("InputModel", **fields)
print(Model.model_json_schema().get("required"))
# ACTUAL: ['query', 'top_k', 'include_meta']
# EXPECTED: ['query']  (top_k / include_meta optional with defaults)

(as_agent_framework_tool() itself also needs agent-framework-core; the loop above is the same code path.)

Expected

Optional kernel function parameters with defaults remain optional in the agent-framework InputModel / schema (top_k=5, include_meta=False).

Actual

All included params are required; defaults are discarded. Especially misleading for False/0 because default_value is not None is true, yet the default is still overwritten.

Root cause (pointer)

semantic_kernel/functions/kernel_function.py — as_agent_framework_tool (~field loop): missing else: around the no-default assignment.

Suggested fix direction

if param.default_value is not None:
    fields[param.name] = (..., Field(..., default=param.default_value))
else:
    fields[param.name] = (..., Field(...))

(Also prefer param.type_object over the string param.type_ if available.)

主要语言
C#
星标
28.6k
派生
4.8k
平均合并
13 小时 24 分钟
30 天内合并 PR
11

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/semantic-kernel 的其他 Issue

查看 microsoft/semantic-kernel 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。