Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
The get_agent_by_role method expects agents to be iterable, but the copy method initializes None when a list of agents is not provided. The simple solution matches the convention of passing None to cloned_agent.
Here is the exception:
\crewai\task.py", line 296, in get_agent_by_role
return next((agent for agent in agents if agent.role == role), None)
TypeError: 'NoneType' object is not iterable
Where the method is declared and called:
def copy(self, agents: Optional[List["BaseAgent"]] = None) -> "Task":
"""Create a deep copy of the Task."""
exclude = {
"id",
"agent",
"context",
"tools",
}
copied_data = self.model_dump(exclude=exclude)
copied_data = {k: v for k, v in copied_data.items() if v is not None}
cloned_context = (
[task.copy() for task in self.context] if self.context else None
)
def get_agent_by_role(role: str) -> Union["BaseAgent", None]:
return next((agent for agent in agents if agent.role == role), None)
cloned_agent = get_agent_by_role(self.agent.role) if self.agent else None
cloned_tools = copy(self.tools) if self.tools else []
copied_task = Task(
**copied_data,
context=cloned_context,
agent=cloned_agent,
tools=cloned_tools,
)
return copied_task
Solution
def get_agent_by_role(role: str) -> Union["BaseAgent", None]:
return next((agent for agent in agents if agent.role == role), None) if agents else None
The
get_agent_by_role
method expectsagents
to be iterable, but the copy method initializesNone
when a list ofagents
is not provided. The simple solution matches the convention of passingNone
to cloned_agent.Here is the exception:
Where the method is declared and called:
Solution