I’m following the CrewAI Getting Started Guide and running into a KeyError: 'key_name'
when executing the crewai run
command in the root of my project directory.
(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.
Here is my setup:
(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)
I also tried using the virtual environment created by in the project directory with no luck.
Has anyone encountered the KeyError: ‘key_name’ issue while working with CrewAI?
2
Answers
Ok added a conditional check in agent.py to skip processing key_name unless it exists. This prevents the error and ensures compatibility for setups where key_name is not required.
Proposed Fix: A conditional check can be added in agent.py to skip processing key_name unless it's present and valid:
This ensures the error doesn’t occur when key_name is absent but unnecessary for the task.
I created a Pull request here
In agent.py, try to change the code where the KeyError occurs to include a check for ‘key_name’ in env_var before accessing it.
This makes it so that the code only tries to access ‘key_name’ if it is actually present in env_var. Here is a nice implementation using CrewAI [1].
[1] https://devnavigator.com/home/setting-up-langchain-crewai-duckduckgo-a7d7b852-a3c7-4c8c-b6b2-c62e3ccae1d0