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.

Crew's key must remain stable after input interpolation

Thiago Moretto2bbf8ed8a89b
diff --git a/src/crewai/agents/agent_builder/base_agent.py b/src/crewai/agents/agent_builder/base_agent.py
index 900fb9134..f42ab3172 100644
--- a/src/crewai/agents/agent_builder/base_agent.py
+++ b/src/crewai/agents/agent_builder/base_agent.py
@@ -176,7 +176,11 @@ class BaseAgent(ABC, BaseModel):
@property
def key(self):
- source = [self.role, self.goal, self.backstory]
+ source = [
+ self._original_role or self.role,
+ self._original_goal or self.goal,
+ self._original_backstory or self.backstory,
+ ]
return md5("|".join(source).encode(), usedforsecurity=False).hexdigest()
@abstractmethod
diff --git a/tests/crew_test.py b/tests/crew_test.py
index 0bf0dfbaa..c01e84f80 100644
--- a/tests/crew_test.py
+++ b/tests/crew_test.py
@@ -2335,6 +2335,50 @@ def test_key():
assert crew.key == hash
+def test_key_with_interpolated_inputs():
+ researcher = Agent(
+ role="{topic} Researcher",
+ goal="Make the best research and analysis on content {topic}",
+ backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
+ allow_delegation=False,
+ )
+
+ writer = Agent(
+ role="{topic} Senior Writer",
+ goal="Write the best content about {topic}",
+ backstory="You're a senior writer, specialized in technology, software engineering, AI and startups. You work as a freelancer and are now working on writing content for a new customer.",
+ allow_delegation=False,
+ )
+
+ tasks = [
+ Task(
+ description="Give me a list of 5 interesting ideas about {topic} to explore for an article, what makes them unique and interesting.",
+ expected_output="Bullet point list of 5 important events.",
+ agent=researcher,
+ ),
+ Task(
+ description="Write a 1 amazing paragraph highlight for each idea of {topic} that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
+ expected_output="A 4 paragraph article about AI.",
+ agent=writer,
+ ),
+ ]
+
+ crew = Crew(
+ agents=[researcher, writer],
+ process=Process.sequential,
+ tasks=tasks,
+ )
+ hash = hashlib.md5(
+ f"{researcher.key}|{writer.key}|{tasks[0].key}|{tasks[1].key}".encode()
+ ).hexdigest()
+
+ assert crew.key == hash
+
+ curr_key = crew.key
+ crew._interpolate_inputs({"topic": "AI"})
+ assert crew.key == curr_key
+
+
def test_conditional_task_requirement_breaks_when_singular_conditional_task():
def condition_fn(output) -> bool:
return output.raw.startswith("Andrew Ng has!!")