Skip to content

fix: handle None tool arguments from Claude API for no-parameter tools#8136

Open
lingyun14beta wants to merge 5 commits intoAstrBotDevs:masterfrom
lingyun14beta:fix/tool-args-none-crash
Open

fix: handle None tool arguments from Claude API for no-parameter tools#8136
lingyun14beta wants to merge 5 commits intoAstrBotDevs:masterfrom
lingyun14beta:fix/tool-args-none-crash

Conversation

@lingyun14beta
Copy link
Copy Markdown
Contributor

@lingyun14beta lingyun14beta commented May 10, 2026

修复部分 Claude 模型调用无参数工具时,API 返回none,导致TypeError的问题。Fixes #8130

Modifications / 改动点

astrbot/core/provider/sources/openai_source.py:在解析工具调用参数后,添加对args is None的判断,确保其被正确转换为 {}
astrbot/core/agent/runners/tool_loop_agent_runner.py:在执行工具前,添加对func_tool_args is None的判断,确保其被正确转换为 {}

  • This is NOT a breaking change. / 这不是一个破坏性变更。

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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Bug Fixes:

  • Prevent TypeError when Claude models return None as tool arguments for tools without parameters by normalizing them to empty dictionaries in both OpenAI source parsing and tool-loop execution.

@auto-assign auto-assign Bot requested review from Soulter and anka-afk May 10, 2026 10:36
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels May 10, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +1025 to +1027
# Some Claude API may return None for tools with no parameters
if func_tool_args is None:
func_tool_args = {}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
# 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
  1. 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.

Comment on lines +904 to +906
# Some Claude API may return None for tools with no parameters
if args is None:
args = {}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
# Some Claude API may return None for tools with no parameters
if args is None:
args = {}
args = self.normalize_tool_args(args)
References
  1. 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.

@lingyun14beta
Copy link
Copy Markdown
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Claude 模型调用无参数工具时参数解析为 None,导致 TypeError: argument after ** must be a mapping

1 participant