fix: Correctly copy memory objects during crew training (#2593)

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-04-12 02:35:03 +00:00
parent 40a441f30e
commit e8c9c734a8
2 changed files with 30 additions and 3 deletions

View File

@@ -1214,6 +1214,13 @@ class Crew(BaseModel):
copied_data = self.model_dump(exclude=exclude)
copied_data = {k: v for k, v in copied_data.items() if v is not None}
if self.short_term_memory:
copied_data["short_term_memory"] = self.short_term_memory.model_copy(deep=True)
if self.long_term_memory:
copied_data["long_term_memory"] = self.long_term_memory.model_copy(deep=True)
if self.entity_memory:
copied_data["entity_memory"] = self.entity_memory.model_copy(deep=True)
copied_data.pop("agents", None)
copied_data.pop("tasks", None)

View File

@@ -1,6 +1,11 @@
"""Test Agent creation and execution basic functionality."""
import hashlib
import tempfile
import os
import pydantic_core
from crewai.memory.short_term.short_term_memory import ShortTermMemory
import json
import os
from concurrent.futures import Future
@@ -4116,6 +4121,21 @@ def test_crew_kickoff_for_each_works_with_manager_agent_copy():
assert crew_copy.manager_agent.id != crew.manager_agent.id
assert crew_copy.manager_agent.role == crew.manager_agent.role
assert crew_copy.manager_agent.goal == crew.manager_agent.goal
assert crew_copy.manager_agent.backstory == crew.manager_agent.backstory
assert isinstance(crew_copy.manager_agent.agent_executor, CrewAgentExecutor)
assert isinstance(crew_copy.manager_agent.cache_handler, CacheHandler)
def test_crew_train_with_memory():
"""Test that training a crew with memory enabled does not raise validation errors."""
agent = Agent(role="Test Agent", goal="Test Goal", backstory="Test Backstory")
task = Task(description="Test Task", expected_output="Test Output", agent=agent)
crew = Crew(agents=[agent], tasks=[task], memory=True)
with tempfile.TemporaryDirectory() as tmpdir:
filename = os.path.join(tmpdir, "training_data.pkl")
try:
crew.train(n_iterations=1, filename=filename)
except pydantic_core.ValidationError as e:
if "Input should be an instance of" in str(e) and ("Memory" in str(e)):
pytest.fail(f"Training with memory raised Pydantic ValidationError, likely due to incorrect memory copy: {e}")
else:
raise e
except Exception as e:
print(f"Warning: Training raised an unexpected exception: {e}")