Skip to content

feat: add sanitation for malformed tool call names in ToolLoopAgentRunner#9144

Merged
Soulter merged 3 commits into
masterfrom
fix/8929
Jul 5, 2026
Merged

feat: add sanitation for malformed tool call names in ToolLoopAgentRunner#9144
Soulter merged 3 commits into
masterfrom
fix/8929

Conversation

@Soulter

@Soulter Soulter commented Jul 5, 2026

Copy link
Copy Markdown
Member

fixes: #8929
closes: #8911

Modifications / 改动点

  • 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

Enhancements:

  • Normalize empty or null tool call names to a placeholder value while preserving list alignment in LLM responses.

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 5, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 5, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
astrbot-docs b7df91d Commit Preview URL

Branch Preview URL
Jul 05 2026, 03:02 AM

@dosubot dosubot Bot added 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 Jul 5, 2026

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've found 1 issue, and left some high level feedback:

  • Consider guarding _sanitize_malformed_tool_calls against llm_resp.tools_call_name being None or a non-list to avoid runtime errors when the LLM returns unexpected structures.
  • If MALFORMED_TOOL_NAME_PLACEHOLDER might surface in user-facing logs or messages, consider making it more descriptive (e.g., including a brief explanation or prefix) so it's clear that the tool name was missing or malformed.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider guarding `_sanitize_malformed_tool_calls` against `llm_resp.tools_call_name` being `None` or a non-list to avoid runtime errors when the LLM returns unexpected structures.
- If `MALFORMED_TOOL_NAME_PLACEHOLDER` might surface in user-facing logs or messages, consider making it more descriptive (e.g., including a brief explanation or prefix) so it's clear that the tool name was missing or malformed.

## Individual Comments

### Comment 1
<location path="astrbot/core/agent/runners/tool_loop_agent_runner.py" line_range="703-708" />
<code_context>
+        Args:
+            llm_resp: The LLM response whose tool call lists should be sanitized.
+        """
+        llm_resp.tools_call_name = [
+            self.MALFORMED_TOOL_NAME_PLACEHOLDER
+            if tool_name is None or tool_name == ""
+            else tool_name
+            for tool_name in llm_resp.tools_call_name
</code_context>
<issue_to_address>
**suggestion:** Consider treating whitespace-only tool names as malformed as well.

Right now `None` and `""` are normalized, but names like `"   "` pass through. If whitespace-only names should also be considered malformed/empty, update the condition to use `strip()` (e.g. `if tool_name is None or tool_name.strip() == ""`).

```suggestion
        llm_resp.tools_call_name = [
            self.MALFORMED_TOOL_NAME_PLACEHOLDER
            if tool_name is None or tool_name.strip() == ""
            else tool_name
            for tool_name in llm_resp.tools_call_name
        ]
```
</issue_to_address>

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.

Comment thread astrbot/core/agent/runners/tool_loop_agent_runner.py
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

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

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.

Code Review

This pull request introduces a mechanism to sanitize malformed tool call names in LLM responses by replacing empty or null names with a placeholder (__malformed_tool_name__). This sanitization is applied during LLM response iteration and tool execution resolution (including requery and repair steps). The reviewer suggested enhancing this sanitization method to defensively handle None values and align the lengths of tools_call_name, tools_call_ids, and tools_call_args to prevent potential IndexError or silent truncation issues during processing.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread astrbot/core/agent/runners/tool_loop_agent_runner.py
@Soulter Soulter merged commit 25cbd41 into master Jul 5, 2026
20 checks passed
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:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]某些情况下,开启流式输出调用工具时有概率出现错误

1 participant