diff --git a/src/crewai/crew.py b/src/crewai/crew.py index 9eb93a16c..d331599b5 100644 --- a/src/crewai/crew.py +++ b/src/crewai/crew.py @@ -275,12 +275,26 @@ class Crew(BaseModel): if self.entity_memory else EntityMemory(crew=self, embedder_config=self.embedder) ) - if hasattr(self, "memory_config") and self.memory_config is not None: - self._user_memory = ( - self.user_memory if self.user_memory else UserMemory(crew=self) - ) + if ( + self.memory_config and "user_memory" in self.memory_config + ): # Check for user_memory in config + user_memory_config = self.memory_config["user_memory"] + if isinstance( + user_memory_config, UserMemory + ): # Check if it is already an instance + self._user_memory = user_memory_config + elif isinstance( + user_memory_config, dict + ): # Check if it's a configuration dict + self._user_memory = UserMemory( + crew=self, **user_memory_config + ) # Initialize with config + else: + raise TypeError( + "user_memory must be a UserMemory instance or a configuration dictionary" + ) else: - self._user_memory = None + self._user_memory = None # No user memory if not in config return self @model_validator(mode="after") @@ -455,8 +469,6 @@ class Crew(BaseModel): ) return self - - @property def key(self) -> str: source = [agent.key for agent in self.agents] + [ @@ -928,13 +940,13 @@ class Crew(BaseModel): def _create_crew_output(self, task_outputs: List[TaskOutput]) -> CrewOutput: if not task_outputs: raise ValueError("No task outputs available to create crew output.") - + # Filter out empty outputs and get the last valid one as the main output valid_outputs = [t for t in task_outputs if t.raw] if not valid_outputs: raise ValueError("No valid task outputs available to create crew output.") final_task_output = valid_outputs[-1] - + final_string_output = final_task_output.raw self._finish_execution(final_string_output) token_usage = self.calculate_usage_metrics()