You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CrewAI执行crewai run时出现KeyError: 'key_name'错误的解决求助

解决CrewAI执行crewai run时的KeyError: 'key_name'错误

问题场景

按照CrewAI入门指南操作,在项目根目录执行crewai run命令时触发KeyError: 'key_name'错误,报错详情如下:

(ai-crew) userk@mycelium:~/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development$ crewai run
Running the Crew
warning: `VIRTUAL_ENV=/home/userk/development/venv/ai-crew` does not match the project environment path `.venv` and will be ignored
Traceback (most recent call last):
  File "/home/userk/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development/.venv/bin/run_crew", line 8, in <module>
    sys.exit(run())
             ^^^^^
  File "/home/userk/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development/src/latest_ai_development/main.py", line 13, in run
    LatestAiDevelopmentCrew().crew().kickoff(inputs=inputs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/userk/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development/.venv/lib/python3.12/site-packages/crewai/project/crew_base.py", line 35, in __init__
    self.map_all_task_variables()
  File "/home/userk/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development/.venv/lib/python3.12/site-packages/crewai/project/crew_base.py", line 145, in map_all_task_variables
    self._map_task_variables(
  File "/home/userk/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development/.venv/lib/python3.12/site-packages/crewai/project/crew_base.py", line 178, in _map_task_variables
    self.tasks_config[task_name]["agent"] = agents[agent_name]()
                                            ^^^^^^^^^^^^^^^^^^^^
  File "/home/userk/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development/.venv/lib/python3.12/site-packages/crewai/project/utils.py", line 7, in memoized_func
    cache[key] = func(*args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^
  File "/home/userk/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development/src/latest_ai_development/crew.py", line 12, in researcher
    return Agent(
           ^^^^^^
  File "/home/userk/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development/.venv/lib/python3.12/site-packages/pydantic/main.py", line 212, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/userk/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development/.venv/lib/python3.12/site-packages/crewai/agent.py", line 160, in post_init_setup
    if env_var["key_name"] in unnacepted_attributes:
       ~~~~~~~^^^^^^^^^^^^
KeyError: 'key_name'
An error occurred while running the crew: Command '['uv', 'run', 'run_crew']' returned non-zero exit status 1.

环境配置

(ai-crew) userk@mycelium:~/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development$ python --version
Python 3.12.3
(ai-crew) userk@mycelium:~/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development$ pip freeze --local | grep crewai
crewai==0.80.0
crewai-tools==0.14.0
(ai-crew) userk@mycelium:~/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development$ pip -V
pip 24.0 from /home/userk/development/venv/ai-crew/lib/python3.12/site-packages/pip (python 3.12)

已尝试使用项目目录内的虚拟环境,问题未解决。

解决方案

1. 降级CrewAI版本

该错误是CrewAI 0.80.0版本的已知bug,卸载当前版本并安装稳定的旧版本即可修复:

pip uninstall -y crewai
pip install crewai==0.79.6

2. 规范Agent环境变量配置

检查项目中crew.py文件的Agent配置,确保环境变量使用包含key_name和value的字典格式,避免直接使用字符串:

# 正确格式
env_vars = [
    {"key_name": "OPENAI_API_KEY", "value": os.getenv("OPENAI_API_KEY")}
]

# 错误格式(需避免)
env_vars = ["OPENAI_API_KEY"]

3. 统一虚拟环境路径(可选)

若要消除虚拟环境路径警告,可将项目虚拟环境路径与系统VIRTUAL_ENV变量保持一致:

# 删除项目内的旧虚拟环境
rm -rf .venv
# 创建与VIRTUAL_ENV一致的虚拟环境
python -m venv /home/userk/development/venv/ai-crew
# 激活并重新安装依赖
source /home/userk/development/venv/ai-crew/bin/activate
pip install -r requirements.txt

验证修复

重新执行crewai run命令,确认错误不再出现。


内容的提问来源于stack exchange,提问作者UserK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 07:09:50