crewAIInc / crewAI

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
https://crewai.com
MIT License
19.71k stars 2.73k forks source link

BUG+FIX get_agent_by_role expects agents, but signature offers None #881

Open Maralai opened 2 months ago

Maralai commented 2 months ago

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
github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.