我在按照CrewAI的入门指南操作时,执行项目根目录下的crewai run
命令时遇到了KeyError: 'key_name'
错误。
(ai-crew) userk@mycelium:~/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development$ crewai runRunning the Crewwarning: `VIRTUAL_ENV=/home/userk/development/venv/ai-crew` does not match the project environment path `.venv` and will be ignoredTraceback (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 --versionPython 3.12.3(ai-crew) userk@mycelium:~/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development$ pip freeze --local | grep crewaicrewai==0.80.0crewai-tools==0.14.0(ai-crew) userk@mycelium:~/development/git/vanto_ai_agents_cornerstone/crewai/latest_ai_development$ pip -Vpip 24.0 from /home/userk/development/venv/ai-crew/lib/python3.12/site-packages/pip (python 3.12)
我还尝试使用项目目录中创建的虚拟环境,但没有成功。
如何解决这个错误?
回答:
好的,我在agent.py中添加了一个条件检查,只有在key_name存在时才处理它。这样可以防止错误发生,并确保在不需要key_name的设置中保持兼容性。
建议的修复方法:可以在agent.py中添加一个条件检查,只有在key_name存在且有效时才处理它:
if "key_name" in env_var and env_var["key_name"] in unnacepted_attributes:continue
这样可以确保在key_name不存在但对任务不必要时不会发生错误。
我已经创建了一个拉取请求,在这里