fix: handle None tool arguments from Claude API for no-parameter tools#8136
fix: handle None tool arguments from Claude API for no-parameter tools#8136lingyun14beta wants to merge 5 commits intoAstrBotDevs:masterfrom
Conversation
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Since this
None-handling for no-parameter tools is now duplicated in both the OpenAI source and the runner, consider centralizing the normalization of tool arguments (e.g., a helper that guarantees{}) so the behavior stays consistent in one place. - The new comments reference Claude-specific behavior in
openai_source.py; if this code path is used by multiple providers, consider rephrasing the comment to describe the generic contract ("tool arguments may be None for no-parameter tools") rather than naming a single provider.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Since this `None`-handling for no-parameter tools is now duplicated in both the OpenAI source and the runner, consider centralizing the normalization of tool arguments (e.g., a helper that guarantees `{}`) so the behavior stays consistent in one place.
- The new comments reference Claude-specific behavior in `openai_source.py`; if this code path is used by multiple providers, consider rephrasing the comment to describe the generic contract ("tool arguments may be None for no-parameter tools") rather than naming a single provider.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request introduces checks to handle cases where tool arguments are null (specifically for the Claude API) by defaulting them to an empty dictionary in both tool_loop_agent_runner.py and openai_source.py. The reviewer recommends refactoring this duplicated logic into a shared helper function to maintain consistency and suggests moving the normalization earlier in the process to avoid displaying null values in the UI.
| # Some Claude API may return None for tools with no parameters | ||
| if func_tool_args is None: | ||
| func_tool_args = {} |
There was a problem hiding this comment.
The check for None arguments is correctly placed to prevent crashes. However, this normalization logic is duplicated in openai_source.py. Per repository rules, please refactor this into a shared helper function to avoid code duplication. Additionally, moving this normalization to the beginning of the loop (around line 992) would ensure the UI doesn't display null values.
| # Some Claude API may return None for tools with no parameters | |
| if func_tool_args is None: | |
| func_tool_args = {} | |
| func_tool_args = self.normalize_tool_args(func_tool_args) |
References
- When implementing similar functionality for different cases (e.g., direct vs. quoted attachments), refactor the logic into a shared helper function to avoid code duplication.
| # Some Claude API may return None for tools with no parameters | ||
| if args is None: | ||
| args = {} |
There was a problem hiding this comment.
This normalization logic is duplicated in tool_loop_agent_runner.py. Please refactor this into a shared helper function to avoid code duplication and ensure consistency across different providers.
| # Some Claude API may return None for tools with no parameters | |
| if args is None: | |
| args = {} | |
| args = self.normalize_tool_args(args) |
References
- When implementing similar functionality for different cases (e.g., direct vs. quoted attachments), refactor the logic into a shared helper function to avoid code duplication.
|
? |
修复部分 Claude 模型调用无参数工具时,API 返回
none,导致TypeError的问题。Fixes #8130Modifications / 改动点
astrbot/core/provider/sources/openai_source.py:在解析工具调用参数后,添加对args is None的判断,确保其被正确转换为 {}astrbot/core/agent/runners/tool_loop_agent_runner.py:在执行工具前,添加对func_tool_args is None的判断,确保其被正确转换为 {}Screenshots or Test Results / 运行截图或测试结果
添加防御性处理
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Bug Fixes: