Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix tasks and agents ordering

João Moura6fa2b8983141
diff --git a/src/crewai/project/annotations.py b/src/crewai/project/annotations.py
index a2a054923..6c0b9942a 100644
--- a/src/crewai/project/annotations.py
+++ b/src/crewai/project/annotations.py
@@ -76,29 +76,13 @@ def crew(func) -> Callable[..., Crew]:
instantiated_agents = []
agent_roles = set()
- # Collect methods from crew instance (not class)
- all_functions = [
- (name, getattr(self, name))
- for name in dir(self)
- if callable(getattr(self, name)) and not name.startswith("__")
- ]
-
- # Filter tasks and agents
- tasks = [
- (name, method)
- for name, method in all_functions
- if hasattr(method, "is_task") and method.is_task
- ]
-
- agents = [
- (name, method)
- for name, method in all_functions
- if hasattr(method, "is_agent") and method.is_agent
- ]
+ # Use the preserved task and agent information
+ tasks = self._original_tasks.items()
+ agents = self._original_agents.items()
# Instantiate tasks in order
for task_name, task_method in tasks:
- task_instance = task_method()
+ task_instance = task_method(self)
instantiated_tasks.append(task_instance)
agent_instance = getattr(task_instance, "agent", None)
if agent_instance and agent_instance.role not in agent_roles:
@@ -107,7 +91,7 @@ def crew(func) -> Callable[..., Crew]:
# Instantiate agents not included by tasks
for agent_name, agent_method in agents:
- agent_instance = agent_method()
+ agent_instance = agent_method(self)
if agent_instance.role not in agent_roles:
instantiated_agents.append(agent_instance)
agent_roles.add(agent_instance.role)
diff --git a/src/crewai/project/crew_base.py b/src/crewai/project/crew_base.py
index 6ad8a8c3c..a420c4dd2 100644
--- a/src/crewai/project/crew_base.py
+++ b/src/crewai/project/crew_base.py
@@ -34,6 +34,18 @@ def CrewBase(cls: T) -> T:
self.map_all_agent_variables()
self.map_all_task_variables()
+ # Preserve task and agent information
+ self._original_tasks = {
+ name: method
+ for name, method in cls.__dict__.items()
+ if hasattr(method, "is_task") and method.is_task
+ }
+ self._original_agents = {
+ name: method
+ for name, method in cls.__dict__.items()
+ if hasattr(method, "is_agent") and method.is_agent
+ }
+
@staticmethod
def load_yaml(config_path: Path):
try: