AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Bug]在查询的时候提示:未找到指定的工具: astr_kb_searchastr_kb_search,将跳过。

Open MegawattJ opened this issue 7 months ago • 4 comments

What happened / 发生了什么

创建了一个数据库:法律知识库 上传了一份最近的法律条文

查询是 如果开启了 Agentic 知识库检索 有一定概率会出现

ID:
call_95fae017db5b4a91b1259abacall_95fae017db5b4a91b1259aba
Args:
{
  "query": "反外国不当域外管辖条例 中国法律"
}
Result:
error: Tool astr_kb_searchastr_kb_search not found. Available tools are: astr_kb_search, astrbot_execute_shell, astrbot_execute_python, astrbot_file_read_tool, astrbot_file_write_tool, astrbot_file_edit_tool, astrbot_grep_tool, future_task, send_message_to_user
Image

Reproduce / 如何复现?

知识库:打开 Agentic 知识库检索

AstrBot version, deployment method (e.g., Windows Docker Desktop deployment), provider used, and messaging platform used. / AstrBot 版本、部署方式(如 Windows Docker Desktop 部署)、使用的提供商、使用的消息平台适配器

Windows Docker Desktop docker-compose部署


services:
  astrbot:
    image: soulter/astrbot:latest
    container_name: astrbot
    restart: always
    ports:
      - "6185:6185"
      - "6199:6199"
      # - "11451:11451"
    volumes:
      - ./data:/AstrBot/data
    environment:
      - TZ=Asia/Shanghai
    tty: true
    stdin_open: true
    deploy:
      resources:
        limits:
          memory: 2G
          cpus: "2.0"
    networks:
      - com_net
  ollama:
    restart: always
    container_name: ollama-bge-m3
    image: ollama/ollama:latest
    ports:
      - 11434:11434
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - ./ollama/models/:/root/.ollama
    command: serve
    networks:
      - com_net

networks:
  com_net:
    name: common
    external: true

对话模型使用nvidia/minimaxai/minimax-m2.7 嵌入模型使用ollama/bge-m3

OS

Windows

Logs / 报错日志

2026-04-20 23:04:44.211 | [23:04:44.211] [Core] [INFO] [routes.config:307]: Saving config, is_core=True 2026-04-20 23:04:50.370 | [23:04:50.369] [Core] [INFO] [core.event_bus:61]: [lawyer] [webchat(webchat)] Tiziano/Tiziano: 你知道《中华人民共和国反外国不当域外管辖条例》么? 2026-04-20 23:04:50.408 | [23:04:50.408] [Core] [INFO] [respond.stage:183]: Prepare to send - Tiziano/Tiziano: 2026-04-20 23:04:50.408 | [23:04:50.408] [Core] [INFO] [respond.stage:199]: 应用流式输出(webchat) 2026-04-20 23:05:25.111 | [23:05:25.110] [Core] [INFO] [runners.tool_loop_agent_runner:883]: Agent 使用工具: ['astr_kb_search', 'astr_kb_searchastr_kb_search'] 2026-04-20 23:05:25.111 | [23:05:25.111] [Core] [INFO] [runners.tool_loop_agent_runner:932]: 使用工具:astr_kb_search,参数:{'query': '中华人民共和国反外国不当域外管辖条例'} 2026-04-20 23:05:25.437 | [23:05:25.437] [Core] [INFO] [runners.tool_loop_agent_runner:932]: 使用工具:astr_kb_searchastr_kb_search,参数:{'query': '中国反外国制裁法 域外管辖'} 2026-04-20 23:05:25.437 | [23:05:25.437] [Core] [WARN] [v4.23.2] [runners.tool_loop_agent_runner:935]: 未找到指定的工具: astr_kb_searchastr_kb_search,将跳过。 2026-04-20 23:05:25.437 | [23:05:25.437] [Core] [INFO] [runners.tool_loop_agent_runner:1130]: Tool astr_kb_searchastr_kb_search Result: error: Tool astr_kb_searchastr_kb_search not found. Available tools are: astr_kb_search, astrbot_execute_shell, astrbot_execute_python, astrbot_file_read_tool, astrbot_file_write_tool, astrbot_file_edit_tool, astrbot_grep_tool, future_task, send_message_to_user 2026-04-20 23:05:26.484 | [23:05:26.483] [Core] [INFO] [core.astr_main_agent:1020]: Generated chatui title for session baf32a4b-d6f8-44b1-9d0a-4a1c5864f6a9: 了解《反外国不当域外管辖条例》 2026-04-20 23:06:12.144 | [23:06:12.144] [Core] [INFO] [result_decorate.stage:189]: 流式输出已启用,跳过结果装饰阶段 2026-04-20 23:07:23.003 | [23:07:23.003] [Core] [INFO] [core.event_bus:61]: [lawyer] [webchat(webchat)] Tiziano/Tiziano: 你知道《中华人民共和国反外国不当域外管辖条例》么? 2026-04-20 23:07:23.039 | [23:07:23.039] [Core] [INFO] [respond.stage:183]: Prepare to send - Tiziano/Tiziano: 2026-04-20 23:07:23.039 | [23:07:23.039] [Core] [INFO] [respond.stage:199]: 应用流式输出(webchat) 2026-04-20 23:08:00.170 | [23:08:00.169] [Core] [INFO] [runners.tool_loop_agent_runner:883]: Agent 使用工具: ['astr_kb_searchastr_kb_search'] 2026-04-20 23:08:00.170 | [23:08:00.170] [Core] [INFO] [runners.tool_loop_agent_runner:932]: 使用工具:astr_kb_searchastr_kb_search,参数:{'query': '反外国不当域外管辖条例 中国法律'} 2026-04-20 23:08:00.170 | [23:08:00.170] [Core] [WARN] [v4.23.2] [runners.tool_loop_agent_runner:935]: 未找到指定的工具: astr_kb_searchastr_kb_search,将跳过。 2026-04-20 23:08:00.171 | [23:08:00.170] [Core] [INFO] [runners.tool_loop_agent_runner:1130]: Tool astr_kb_searchastr_kb_search Result: error: Tool astr_kb_searchastr_kb_search not found. Available tools are: astr_kb_search, astrbot_execute_shell, astrbot_execute_python, astrbot_file_read_tool, astrbot_file_write_tool, astrbot_file_edit_tool, astrbot_grep_tool, future_task, send_message_to_user 2026-04-20 23:08:04.367 | [23:08:04.366] [Core] [INFO] [core.astr_main_agent:1020]: Generated chatui title for session f983889c-2e31-477d-aebe-f58539d2ff75: 中国反外国域外管辖条例 2026-04-20 23:08:43.767 | [23:08:43.766] [Core] [INFO] [runners.tool_loop_agent_runner:883]: Agent 使用工具: ['astr_kb_searchastr_kb_search'] 2026-04-20 23:08:43.767 | [23:08:43.767] [Core] [INFO] [runners.tool_loop_agent_runner:932]: 使用工具:astr_kb_searchastr_kb_search,参数:{'query': '反外国不当域外管辖条例 中国法律'} 2026-04-20 23:08:43.767 | [23:08:43.767] [Core] [WARN] [v4.23.2] [runners.tool_loop_agent_runner:935]: 未找到指定的工具: astr_kb_searchastr_kb_search,将跳过。 2026-04-20 23:08:43.767 | [23:08:43.767] [Core] [INFO] [runners.tool_loop_agent_runner:1130]: Tool astr_kb_searchastr_kb_search Result: error: Tool astr_kb_searchastr_kb_search not found. Available tools are: astr_kb_search, astrbot_execute_shell, astrbot_execute_python, astrbot_file_read_tool, astrbot_file_write_tool, astrbot_file_edit_tool, astrbot_grep_tool, future_task, send_message_to_user 2026-04-20 23:09:27.135 | [23:09:27.134] [Core] [INFO] [runners.tool_loop_agent_runner:883]: Agent 使用工具: ['astr_kb_searchastr_kb_search'] 2026-04-20 23:09:27.135 | [23:09:27.135] [Core] [INFO] [runners.tool_loop_agent_runner:932]: 使用工具:astr_kb_searchastr_kb_search,参数:{'query': '反外国不当域外管辖条例 中国法律'} 2026-04-20 23:09:27.135 | [23:09:27.135] [Core] [WARN] [v4.23.2] [runners.tool_loop_agent_runner:935]: 未找到指定的工具: astr_kb_searchastr_kb_search,将跳过。 2026-04-20 23:09:27.135 | [23:09:27.135] [Core] [INFO] [runners.tool_loop_agent_runner:1130]: Tool astr_kb_searchastr_kb_search Result: error: Tool astr_kb_searchastr_kb_search not found. Available tools are: astr_kb_search, astrbot_execute_shell, astrbot_execute_python, astrbot_file_read_tool, astrbot_file_write_tool, astrbot_file_edit_tool, astrbot_grep_tool, future_task, send_message_to_user 2026-04-20 23:10:08.463 | [23:10:08.462] [Core] [INFO] [result_decorate.stage:189]: 流式输出已启用,跳过结果装饰阶段

Are you willing to submit a PR? / 你愿意提交 PR 吗?

  • [ ] Yes!

Code of Conduct

MegawattJ avatar Apr 20 '26 15:04 MegawattJ

Hi @MegawattJ! I'm Dosu and I’m helping the AstrBot team.

这个问题的根本原因是 LLM 返回的 tool call 响应中,ID 和工具名都被重复拼接了:

  • ID: call_95fae017db5b4a91b1259aba → call_95fae017db5b4a91b1259abacall_95fae017db5b4a91b1259aba
  • Name: astr_kb_search → astr_kb_searchastr_kb_search

这不是 AstrBot 内部逻辑的拼接错误,而是流式响应处理过程中 chunk 被重复累积导致的。AstrBot 当前使用 OpenAI SDK 的 ChatCompletionStreamState.handle_chunk() 来处理流式 tool call [1],但没有对最终拼接结果做去重或格式校验 [2]。

可能的触发原因:你使用的 nvidia/minimaxai/minimax-m2.7 模型通过 NVIDIA 代理访问,该代理可能在流式传输中发送了重复的 chunk,或者 MiniMax 模型本身在 tool call 的流式输出上存在兼容性问题。此问题在 #7365 中也有类似讨论(工具名不匹配导致调用失败)[3]。

临时解决方案:

  1. 尝试关闭流式输出,看问题是否消失——这可以确认是否为流式 chunk 重复导致
  2. 尝试换一个模型(如 DeepSeek 或其他 OpenAI 兼容模型)测试,排除是 MiniMax 特有问题

建议的代码修复方向:在 openai_source.py 的 _parse_openai_completion 方法中增加防御性校验,检测 tool call ID 和 name 是否为"前半段 == 后半段"的自重复字符串,若是则取前半段作为正确值。

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about AstrBot with Dosu Add Dosu to your team

dosubot[bot] avatar Apr 20 '26 15:04 dosubot[bot]

我试了一下 目前nvidia提供的nvidia/minimaxai/minimax-m2.7存在这个问题 相同配置下 火山coding plan的minimax-m2.7没有这个问题 应该是nvidia的问题 当然 如果能在代码里兼容一下 就更好了

MegawattJ avatar Apr 28 '26 13:04 MegawattJ

[现场报告] 流式 tool_call 的 id/name 重复拼接在 v4.26.7 仍可复现 —— 附线上事故完整链路与流式侧防护方案

大家好,我们是一个长期运行的生产部署,昨天(2026-09-03)在 v4.26.7(openai 2.33.0)上线上实弹踩中了这个 bug,补充一份具体证据和方案建议,希望能帮上忙。

线上事故(litellm 网关,OpenAI 兼容协议)

一轮图片溯源 Agent 完全静默失效:

22:09:37 Agent 使用工具: ['reverse_image_searchreverse_image_search']
22:09:37 WARN 未找到指定的工具: reverse_image_searchreverse_image_search,将跳过。
22:13:03 openai.BadRequestError: 400 - litellm.BadRequestError:
        [StringParam] [input[8].call_id] [string_above_max_length]
        Invalid 'input[8].call_id': string too long. Expected max 64, got 86.
22:14:34 Agent 使用工具: ['brave_web_searchbrave_web_search']
22:14:34 WARN 未找到指定的工具: brave_web_searchbrave_web_search,将跳过。

同一根因引出两种故障形态:

  1. 工具名翻倍 → 工具注册表查不到 → error: Tool ... not found → 整个检索步骤静默降级(bot 只能对用户说"检索链路出了点问题");
  2. call_id 翻倍(86 字符)→ 随对话上下文持久化 → 下一轮请求直接被上游 400 拒收(litellm 执行 Responses API 的 64 字符上限)。这个次生影响比表面更糟:畸形消息会一直毒化会话,直到上下文压缩把它挤出去才自愈。

用 SDK 真实的累加器做过仿真确认机制:3 个 chunk 各自携带完整 id+name → accumulate_delta 字符串拼接 → len(id) == 87,与线上观测到的 86+后缀 吻合。

现有两个 PR 的覆盖范围

#7735 和 #7765 都是在完成解析时做事后去重,能覆盖常见情况,但事后自重复检测(s+s → s)处理不了:

  • 3 次以上重复产生 abcabcabc——除非检测逻辑循环到稳定为止(建议确认两个 PR 是否都做了);
  • 同 index 复用且换了新 id —— 部分网关会复用 index=0 开启第二个 tool_call。SDK 快照按 index 合并,两个不同逻辑调用的 id/args 会被串接成一条(call_AAAcall_BBB)。这种情况下不存在自重复模式,#7694 式的检测不会触发;
  • 字段重复但内容不一致(网关中途改写了 id 等)。

建议:把防护放到流式边界上

我们在 openai_source._query_stream 的流式循环里(紧挨着现有的 #6661 index 修复)打了一个补丁,用一个每次请求独立的 dict[int, tuple[id, name]] 做三件事:

  1. 同 index、相同 id/name 重复出现 → 把 delta 里的这两个字段丢弃,只保留 arguments 的增量拼接;
  2. 同 index、不同 id → 视为新的 tool_call,重映射到一个全新槽位(防止跨调用串接);
  3. 保留原有 1-based index 重映射。

已在真实 _query_stream 代码路径上端到端验证(假客户端重放恶意流,检查 ChatCompletionStreamState.get_final_completion 产出):正常流、双工具并行、与本线上事故完全一致的重复重放模式、同 index 换新 id,四种场景都产出干净的单条 tool_call。

这个方案在 state.handle_chunk 上游生效,SDK 快照完全见不到畸形 delta,不需要任何事后字符串手术。如果维护者倾向流式侧方案,我们乐意整理成 PR;或者它也可以和 #7735/#7765 互为纵深防御(每个 chunk 只是两次字典查询,开销可忽略)。

复现环境

  • AstrBot v4.26.7(Windows 原生部署),openai>=1.78.0 实际装到 2.33.0
  • Provider:openai_chat_completion,经 litellm 网关转发,该网关会间歇性地在 arguments chunk 里重复携带完整的 tool_call 元数据
  • 值得一提:当晚同一网关还有另一次不稳定(37 次 httpx.ReadTimeout),故障出现时 fallback 路由正在生效——fallback 会把网关的偶发怪癖放大成高频问题。

如需完整事故日志片段或最小复现脚本,随时可以提供。这个问题从 4 月(#7694)挂到现在,5 个月后仍在咬生产用户,希望这份 400 报错的实锤证据能帮两个修复 PR 早日合并。🙏

x1051445024 avatar Sep 03 '26 15:09 x1051445024

Root cause of the duplicated tool name is at the streaming boundary, reproducible with openai 3.36.0 and AstrBot 4.29.1.

Some OpenAI-compatible gateways resend the full tool call id and function.name on every chunk of one call while reusing the same index:

{"tool_calls":[{"index":1,"id":"call_a","function":{"name":"read","arguments":"{\"pa"}}]}
{"tool_calls":[{"index":1,"id":"call_a","function":{"name":"read","arguments":"th\": \"x\"}"}}]}

ChatCompletionStreamState accumulates delta fields with openai.lib.streaming._deltas.accumulate_delta, which concatenates string fields, so a name carried by every frame accumulates to readreadread. The same index can also be sparse (a reasoning block takes index 0) while openai/lib/streaming/chat/_completions.py uses it directly as a position in its tool call list, which raises list index out of range and leaves the final completion without the tool call.

Proposed fix, keeping the identity of a tool call on its first frame and renumbering the indices in call order at the streaming boundary: #10703

Same defect on the gateway side (local OpenAI relay used by the reporter): Ebony-Vinyl/dsh-our-free-model#622

OMSociety avatar Oct 23 '26 18:10 OMSociety