Crewai run getting keyerror for newly defined task and agents

Hello,
I install crewai and uv as mentioned in the crewai docs quickstart and then created the crewai project using “crewai create crew latest-ai-development”
then executed “crewai run”. Which executed the run and the crew started showing result. Then I added only one new task and agent in config file and in same format as previous. But the extra thing is only that I added tools for agent. Then I kickoff the crew by running crewai run and its asked me to install databricks and databricks-sdk. So I did it by uv add databricks. After that I executed crew run again and started getting error as below,
Traceback (most recent call last):
File “/home/intellih/F11/latest_ai_development/.venv/bin/run_crew”, line 10, in
sys.exit(run())
File “/home/intellih/F11/latest_ai_development/src/latest_ai_development/main.py”, line 21, in run
LatestAiDevelopment().crew().kickoff(inputs=inputs)
File “/home/intellih/F11/latest_ai_development/.venv/lib/python3.10/site-packages/crewai/project/utils.py”, line 11, in memoized_func
cache[key] = func(*args, **kwargs)
File “/home/intellih/F11/latest_ai_development/.venv/lib/python3.10/site-packages/crewai/project/annotations.py”, line 95, in wrapper
task_instance = task_method(self)
File “/home/intellih/F11/latest_ai_development/.venv/lib/python3.10/site-packages/crewai/project/utils.py”, line 11, in memoized_func
cache[key] = func(*args, **kwargs)
File “/home/intellih/F11/latest_ai_development/.venv/lib/python3.10/site-packages/crewai/project/annotations.py”, line 28, in wrapper
result = func(*args, **kwargs)
File “/home/intellih/F11/latest_ai_development/src/latest_ai_development/crew.py”, line 61, in Player_research_task
config=self.tasks_config[‘Player_research_task’],
KeyError: ‘Player_research_task’
An error occurred while running the crew: Command ‘[‘uv’, ‘run’, ‘run_crew’]’ returned non-zero exit status 1.

I removed the tools mentioned in agent but that did not work. I double checked if anything is missing but everything is same as tasks and agents mentioned in default structure.

Anybody else facing same kind of issue or have solution for this ?

Are you sure you are using the same key for the task as defined in the yaml file. Make sure the capitalisation is the same.

You can share the task and agent definitions as well as the crew definition code here too. It might give better context for people to help you.

Yes, I checked the config and its correct. Sharing here the config files and crew

agents.yaml
researcher:
role: >
{topic} Senior Data Researcher
goal: >
Uncover cutting-edge developments in {topic}
backstory: >
You’re a seasoned researcher with a knack for uncovering the latest
developments in {topic}. Known for your ability to find the most relevant
information and present it in a clear and concise manner.

reporting_analyst:
role: >
{topic} Reporting Analyst
goal: >
Create detailed reports based on {topic} data analysis and research findings
backstory: >
You’re a meticulous analyst with a keen eye for detail. You’re known for
your ability to turn complex data into clear and concise reports, making
it easy for others to understand and act on the information you provide.

Player_Researcher:
role: >
The best Cricket Player Researcher
goal: >
Being the best Cricket Player Researcher in gathering, interpret cricket and players related data
backstory: >
Known the Best CRICKET SPORT PLAYER researcher, you’re skilled researcher who have fine knowledge of cricket and research the individual player

tasks.yaml

research_task:
description: >
Conduct a thorough research about {topic}
Make sure you find any interesting and relevant information given
the current year is 2024.
expected_output: >
A list with 10 bullet points of the most relevant information about {topic}
agent: researcher

reporting_task:
description: >
Review the context you got and expand each topic into a full section for a report.
Make sure the report is detailed and contains any and all relevant information.
expected_output: >
A fully fledge reports with the mains topics, each with a full section of information.
Formatted as markdown without ‘```’
agent: reporting_analyst

Player_research_task:
description: >
Collect the personal details such as DOB, Origin Country, age, Height, Role in Team, Batting Style, Bowling Style, etc. for {player}

expected_output: >
Your final answer MUST be a detailed report of all gathered information for {player}

agent: Player_researcher
#async_execution: true

crew.py

from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
#from crewai_tools import ScrapeWebsiteTool, WebsiteSearchTool, SerperDevTool

#Uncomment the following line to use an example of a custom tool
#from latest_ai_development.tools.custom_tool import MyCustomTool

#Check our tools documentations for more information on how to use them
#from crewai_tools import SerperDevTool

@CrewBase
class LatestAiDevelopment():
“”“LatestAiDevelopment crew”“”

agents_config = 'config/agents.yaml'
tasks_config = 'config/tasks.yaml'

@agent
def researcher(self) -> Agent:
	return Agent(
		config=self.agents_config['researcher'],
		#tools=[MyCustomTool()], # Example of custom tool, loaded on the beginning of file
		verbose=True
	)

@agent
def reporting_analyst(self) -> Agent:
	return Agent(
		config=self.agents_config['reporting_analyst'],
		verbose=True
	)

@agent
def Player_Researcher(self) -> Agent:
	return Agent(
        config=self.agents_config['Player_Researcher'],
        memory=True,
        #tools=[ScrapeWebsiteTool(), WebsiteSearchTool(), SerperDevTool()],
        verbose=True,
        #allow_delegation=True
    )

@task
def research_task(self) -> Task:
	return Task(
		config=self.tasks_config['research_task'],
	)

@task
def reporting_task(self) -> Task:
	return Task(
		config=self.tasks_config['reporting_task'],
		output_file='report.md'
	)



@task
def Player_research_task(self) -> Task:
	return Task(
        config=self.tasks_config['Player_research_task'],
        agent=self.Player_Researcher(),
        #async_execution=True,
        output_file='player_research.txt'
    )

@crew
def crew(self) -> Crew:
	"""Creates the LatestAiDevelopment crew"""
	return Crew(
		agents=self.agents, # Automatically created by the @agent decorator
		tasks=self.tasks, # Automatically created by the @task decorator
		process=Process.sequential,
		verbose=True,
		# process=Process.hierarchical, # In case you wanna use that instead https://docs.crewai.com/how-to/Hierarchical/
	)

Your issue is in the naming differences in your task definition in yaml and the agent name you declared in the crew.py

Player_research_task:
description: >
Collect the personal details such as DOB, Origin Country, age, Height, Role in Team, Batting Style, Bowling Style, etc. for {player}
expected_output: >
  Your final answer MUST be a detailed report of all gathered information for {player}
agent: Player_researcher
#async_execution: true
@agent
def Player_Researcher(self) -> Agent:
	return Agent(
        config=self.agents_config['Player_Researcher'],
        memory=True,
        #tools=[ScrapeWebsiteTool(), WebsiteSearchTool(), SerperDevTool()],
        verbose=True,
        #allow_delegation=True
    )

Your agent is named Player_Researcher but your task has agent Player_researcher. Your crew.py task also has an agent assigned

@task
def Player_research_task(self) -> Task:
	return Task(
        config=self.tasks_config['Player_research_task'],
        agent=self.Player_Researcher(),
        #async_execution=True,
        output_file='player_research.txt'
    )

Delete the agent from crew.py Player_research_task and fix the agent name in your yaml file task definition and it should work.

Thanks, I corrected it and it worked.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.