[Bug] 在使用SubAgent 编排时会出现'HandoffTool' object has no attribute 'agent'的错误
What happened / 发生了什么
如下我的配置与日志截图 在调用HandoffTool工具时,出现HandoffTool不具有angent的错误,然后子angent就会停止运行
然后我实现了以下代码
在astrbot初始化后立马检测angent属性,结果还是不存在
于是,我在astrbot.core.subagent_orchestrator.py的第89行在添加了日志,发现在HandoffTool被创建后立马就没有了angent属性
handoff = HandoffTool(
agent=agent,
tool_description=public_description or None,
)
logger.debug(f"[SubAgentOrchestrator] handoff.agent.name:{handoff.agent.name}")
# 图片放不出来了,贴个代码
所以问题出现在HandoffTool的__init__里面
def __init__(
self,
agent: Agent[TContext],
parameters: dict | None = None,
tool_description: str | None = None,
**kwargs,
) -> None:
self.agent = agent
# Avoid passing duplicate `description` to the FunctionTool dataclass.
# Some call sites (e.g. SubAgentOrchestrator) pass `description` via kwargs
# to override what the main agent sees, while we also compute a default
# description here.
# `tool_description` is the public description shown to the main LLM.
# Keep a separate kwarg to avoid conflicting with FunctionTool's `description`.
description = tool_description or self.default_description(agent.name)
super().__init__(
name=f"transfer_to_{agent.name}",
parameters=parameters or self.default_parameters(),
description=description,
**kwargs,
)
# Optional provider override for this subagent. When set, the handoff
# execution will use this chat provider id instead of the global/default.
self.provider_id: str | None = None
self.agent = agent在super().__init__前,导致了super().__init__的一些行为会让self.agent属性消失
Reproduce / 如何复现?
创建一个SubAgent 编排并调用
AstrBot version, deployment method (e.g., Windows Docker Desktop deployment), provider used, and messaging platform used. / AstrBot 版本、部署方式(如 Windows Docker Desktop 部署)、使用的提供商、使用的消息平台适配器
ubt22.04 Lts Astrbot 4.14.8
OS
Linux
Logs / 报错日志
[22:57:46] [Core] [INFO] [runners.tool_loop_agent_runner:437]: Agent 使用工具: ['transfer_to_forum'] [22:57:47] [Core] [INFO] [runners.tool_loop_agent_runner:474]: 使用工具:transfer_to_forum,参数:{'input': '用户四(ID: 3527679745)要求调用 transfer_to_forum 工具进行一次测试。当前时间:2026-02-09 22:57 (CST)。'} [22:57:47] [Core] [WARN] [v4.14.8] [runners.tool_loop_agent_runner:653]: Traceback (most recent call last): File "/home/orangepi/pythonDma/AstrBot/astrbot/core/agent/runners/tool_loop_agent_runner.py", line 531, in _handle_function_tools async for resp in executor: # type: ignore File "/home/orangepi/pythonDma/AstrBot/astrbot/core/astr_agent_tool_exec.py", line 48, in execute async for r in cls._execute_handoff(tool, run_context, **tool_args): File "/home/orangepi/pythonDma/AstrBot/astrbot/core/astr_agent_tool_exec.py", line 97, in _execute_handoff tools = tool.agent.tools ^^^^^^^^^^ AttributeError: 'HandoffTool' object has no attribute 'agent'
Are you willing to submit a PR? / 你愿意提交 PR 吗?
- [x] Yes!
Code of Conduct
- [x] I have read and agree to abide by the project's Code of Conduct。
你的分析是对的:HandoffTool 的 agent 属性在 super().init 之后丢失,根本原因很可能是父类 FunctionTool(或其父类)用了 @dataclass 或 slots,导致只允许声明的字段存在,其他属性(比如 agent)会被“清空”或根本无法存储。这种情况下,即使你在 super().init 前赋值,初始化后依然会丢失 参考1 参考2。
常见的修复方式有:
- 把 self.agent = agent 放到 super().init 之后。如果父类没有禁止动态属性,这样可以保留 agent 属性。
- 检查 FunctionTool 是否用 @dataclass(frozen=True) 或 slots。如果用了 slots,需要在 slots 里加上 agent,否则无法动态添加。
- 如果 FunctionTool 是 dataclass 并且禁止额外属性,可以考虑用 composition(组合)而不是继承,或者把 agent 作为参数传递/存储到允许的字段里。
你可以先试试把 self.agent = agent 移到 super().init 之后。如果还是不行,建议检查 FunctionTool 的实现,确认是否有 slots 或 frozen dataclass 限制。如果有,考虑在 slots 里加 agent,或者换一种存储方式。
如果你用的是 4.14.8 旧版本,HandoffTool 和 Agent 相关代码近期有过大幅重构甚至移除,建议同步下主分支的实现方式,避免和新版不兼容 参考。
如需进一步定位,可以贴下 FunctionTool 的完整实现。这样能更快判断是 slots 还是 dataclass 的问题。
To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other