AttributeError: ‘AgentExecutor’ object has no attribute ‘ask_for_human_input’ with default experimental executor

用户在 CrewAI(1.14.5 或 1.14.6)中创建 Agent 和 Task,并将 Task 的 human_input 参数设置为 True ,然后通过 Crew.kickoff() 运行工作流。运行过程中,当需要触发人工反馈时,代码崩溃并抛出 AttributeError 。此问题在

AttributeError: 'AgentExecutor' object has no attribute 'ask_for_human_input' with default experimental executor

AttributeError: ‘AgentExecutor’ object has no attribute ‘ask_for_human_input’ with default experimental executor

快速结论:此报错发生在 CrewAI 1.14.5+ 版本中,当 Task 设置 human_input=True 时触发。优先排查是否使用了新默认的实验性 AgentExecutor(experimental executor),该 executor 未直接暴露 ask_for_human_input 属性,导致 HumanInputProvider 访问失败。

问题场景

用户在 CrewAI(1.14.5 或 1.14.6)中创建 Agent 和 Task,并将 Task 的 human_input 参数设置为 True,然后通过 Crew.kickoff() 运行工作流。运行过程中,当需要触发人工反馈时,代码崩溃并抛出 AttributeError。此问题在 Windows 11、Python 3.12、crewai 1.14.6、crewai-tools 1.14.6 环境中被复现。

报错原文

An unknown error occurred. Please check the details below.
Error details: 'AgentExecutor' object has no attribute 'ask_for_human_input'

原因分析

根本原因在于新默认的实验性 AgentExecutor(位于 crewai/experimental/agent_executor.py)与 ExecutorContext 协议不兼容。

  • ExecutorContext 协议(定义在 crewai/core/providers/human_input.py:26)要求 executor 对象直接具备 ask_for_human_input: bool 属性。
  • SyncHumanInputProvider._handle_regular_feedback 会直接读取并写入 context.ask_for_human_input(如 while context.ask_for_human_input:context.ask_for_human_input = False)。
  • 旧的 CrewAgentExecutor(位于 crewai/agents/crew_agent_executor.py:129)将 ask_for_human_input 定义为顶层 Pydantic 字段(ask_for_human_input: bool = Field(default=False)),因此满足协议。
  • 新的默认实验性 AgentExecutor(位于 crewai/experimental/agent_executor.py)的处理方式不同:ask_for_human_input 定义在 AgentExecutorState(Flow 状态类,第 132 行)上,而非 executor 本身。executor 的 invoke() 方法虽然正确地通过 self.state.ask_for_human_input 使用,但随后将 self 转换为 ExecutorContext 传递给 _handle_human_feedback(第 3025 行),此时直接的属性访问 self.ask_for_human_input 因为不存在而失败。

额外问题:在 CrewAI 1.14.4 中,human_input=True 也存在退化——结果被隐藏(除非开启 verbose),导致用户被要求审查从未显示的“Final Result above”。这说明该功能从 1.14.4 开始就已退化,在 1.14.5+ 进一步恶化至完全崩溃。

环境排查

  • Python 版本:3.12
  • CrewAI 版本:1.14.5 或 1.14.6(问题在 1.14.4 也可能以不同形式出现)
  • CrewAI Tools 版本:1.14.6
  • 虚拟环境:Venv
  • 触发条件:任何设置了 human_input=True 的 Task

解决步骤

注意:以下解决方案基于 Issue 中提出的修复方案。由于该 Issue 尚未合并到主分支,建议优先尝试前两种修复方式。

  1. 方案一:为 AgentExecutor 添加属性委托(可优先尝试)

    crewai/experimental/agent_executor.py 中的 AgentExecutor 类中添加属性委托,将 ask_for_human_input 的读写委托给 self.state,使其满足 ExecutorContext 协议:

    @property
    def ask_for_human_input(self) -> bool:
        return self.state.ask_for_human_input
    
    @ask_for_human_input.setter
    def ask_for_human_input(self, value: bool) -> None:
        self.state.ask_for_human_input = value
  2. 方案二:切换回旧的 CrewAgentExecutor(临时绕过)

    如果无法修改源代码,可尝试通过配置强制使用旧的 CrewAgentExecutor。具体方法取决于你的 CrewAI 版本和配置方式,请查阅相关文档。注意:此方式仅为临时绕过,可能影响其他功能。

  3. 方案三:等待官方修复

    监控 CrewAI 的版本更新,确认包含此修复的版本发布后升级。

  4. 额外修复(针对 1.14.4 版本隐藏结果的问题):

    human_input 激活时,确保 AgentFinish 输出会无条件显示(而非仅当 verbose=True 时才显示),以便用户能看到需要审查的结果。这可能需要修改 crew_agent_executor.py::_show_logs 或相关渲染逻辑。

验证方法

使用以下最小复现代码(无需 API 密钥)测试:

from crewai import Agent, Crew, Task, Process
from crewai.llms.base_llm import BaseLLM

class StubLLM(BaseLLM):
    def call(self, messages, tools=None, callbacks=None, available_functions=None,
             from_task=None, from_agent=None, response_model=None):
        return "Thought: I know it.\nFinal Answer: The sky is blue."
    def supports_function_calling(self) -> bool:
        return False

agent = Agent(role="Tester", goal="Answer", backstory="bg",
              llm=StubLLM(model="stub"), verbose=False)
task = Task(description="Sky colour?", expected_output="a colour",
            agent=agent, human_input=True)
Crew(agents=[agent], tasks=[task], process=Process.sequential, verbose=False).kickoff()

运行后,预期行为应为:Agent 完成任务后,暂停执行并显示带最终结果的反馈面板供用户输入。如果不再出现 AttributeError,并且结果可见(无需 verbose=True),则问题已解决。

参考来源

crewAIInc/crewAI #6065

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 7886

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注