LightAgent icon indicating copy to clipboard operation
LightAgent copied to clipboard

[Feature]: Add lightweight validation for tool_info schemas before tool registration

Open helloJamest opened this issue 4 months ago • 1 comments

Problem

LightAgent's tool-calling reliability depends heavily on the quality of each tool's tool_info schema.

Today, ToolRegistry.register_tool() converts tool_info into OpenAI-compatible tool schemas, and AsyncToolDispatcher._validate_tool_params() validates runtime arguments. However, there is no small built-in way to validate the tool schema itself before or after registration.

This makes several common mistakes hard to catch early:

  • empty or missing tool_description
  • invalid or duplicate tool_name
  • duplicate parameter names
  • parameter entries missing name, type, or description
  • unsupported JSON-schema-like parameter types
  • required values that are not booleans
  • tools that register successfully but later confuse model-side tool selection

As LightAgent supports runtime tools, MCP tools, generated tools, and skill tools, schema quality becomes more important for tool routing and debugging.

Proposed solution

Add a lightweight, dependency-free tool schema validation helper.

A minimal first version could include one of these APIs:

issues = ToolRegistry.validate_tool_info(tool_info)

or:

issues = agent.validate_tools()

The validator would return structured diagnostics instead of changing runtime behavior by default.

Example diagnostic shape:

{
    "tool": "search_database",
    "level": "warning",
    "field": "tool_description",
    "message": "tool_description should not be empty"
}

Suggested initial checks:

  • tool_name exists and matches the same safe-name pattern used by ToolLoader
  • tool_description is present and non-empty
  • tool_params is a list
  • each param has a unique name
  • each param has a supported type: string, integer, number, boolean, array, object
  • required is a boolean when present
  • duplicate tool names in the registry can be reported

To keep this backward compatible, the first PR can expose validation as an explicit helper and documentation example, without making registration fail by default. A future strict mode could raise on errors if maintainers want that.

Alternatives considered

  • Do nothing and rely on runtime model/tool-call failures. This keeps the core simple but makes schema mistakes hard to debug.
  • Add a full vector-based tool retrieval system. That may be useful later, but it is much larger than a first schema-quality layer.
  • Make invalid schemas fail registration immediately. That could break existing users, so an opt-in validator seems safer for the first step.

Additional context

I am happy to contribute a small PR with:

  • the validator implementation
  • focused unit tests with fake tool definitions
  • documentation in the tools guide
  • no live LLM calls, no network calls, and no new dependencies

helloJamest avatar Jul 09 '26 01:07 helloJamest

感谢这个提议。这个方向与 LightAgent 当前的工具可靠性建设一致,维护侧将其作为 P1 enhancement 纳入后续版本,并开放贡献。

建议首个 PR 保持小而兼容:

  • 提供显式的 ToolRegistry.validate_tool_info(tool_info) 校验入口,返回结构化 diagnostics;
  • 检查工具名、描述、tool_params 类型、参数字段、参数名重复、支持的参数类型以及 required 布尔值;
  • 提供注册表级重复工具名检查;
  • 默认不改变 register_tool() 的行为,不因历史 schema 直接抛错;
  • 不增加依赖,不调用网络或真实模型;
  • 补充聚焦的单元测试和 docs/tools.md 使用示例。

严格注册模式可以在该基础能力稳定后另行设计,避免破坏现有用户。欢迎按上述范围提交一个聚焦 PR,并在描述中关联 #75。

wxai-space avatar Jul 10 '26 09:07 wxai-space