Repository: crewAIInc/crewAI
File: src/crewai/memory/storage/mem0_storage.py (Python)

Code before:
                if self.mem0_run_id:
                    params["run_id"] = self.mem0_run_id
            self.memory.add(value, **params)

    def search(
        self,
        query: str,
        limit: int = 3,
        score_threshold: float = 0.35,
    ) -> List[Any]:
        params = {"query": query, "limit": limit, "output_format": "v2", "run_id": self.mem0_run_id}
        
        if user_id := self._get_user_id():
            params["user_id"] = user_id

        agent_name = self._get_agent_name()
        if self.memory_type == "short_term":
            params["agent_id"] = agent_name
            params["metadata"] = {"type": "short_term"}
        elif self.memory_type == "long_term":
            params["agent_id"] = agent_name
            params["metadata"] = {"type": "long_term"}
        elif self.memory_type == "entities":
            params["agent_id"] = agent_name
            params["metadata"] = {"type": "entity"}
        elif self.memory_type == "external":
            params["agent_id"] = agent_name
            params["metadata"] = {"type": "external"}

        # Discard the filters for now since we create the filters
        # automatically when the crew is created.
        if isinstance(self.memory, Memory):
            del params["metadata"], params["output_format"], params["run_id"]
            
        results = self.memory.search(**params)
        return [r for r in results["results"] if r["score"] >= score_threshold]

    def _get_user_id(self) -> str:
        return self._get_config().get("user_id", "")

    def _get_agent_name(self) -> str:
        if not self.crew:
            return ""

        agents = self.crew.agents
        agents = [self._sanitize_role(agent.role) for agent in agents]
        agents = "_".join(agents)
        return agents

    def _get_config(self) -> Dict[str, Any]:
        return self.config or getattr(self, "memory_config", {}).get("config", {}) or {}

Review comment (suggestion):
First, notice if you provide `run_id`, that means you create short-term memory for a user session and no long-term memory is created - [mem0 docs](https://docs.mem0.ai/platform/quickstart#4-1-create-memories). 
`run_id` should be only allowed to be used for short-term memory.

Code after:
            "short_term": "short_term",
            "long_term": "long_term",
            "entities": "entity",
            "external": "external"
        }

        # Shared base params
        params: dict[str, Any] = {
            "metadata": {"type": base_metadata[self.memory_type], **metadata},
            "infer": self.infer
        }

        if self.memory_type == "external":
            params["user_id"] = user_id

        
        if params:
            # MemoryClient-specific overrides
            if isinstance(self.memory, MemoryClient):
                params["includes"] = self.includes
                params["excludes"] = self.excludes
                params["output_format"] = "v1.1"
                params["version"]="v2"

                if self.memory_type == "short_term":
                    params["run_id"] = self.mem0_run_id

            self.memory.add(assistant_message, **params)

    def search(self,query: str,limit: int = 3,score_threshold: float = 0.35) -> List[Any]:
        params = {
            "query": query, 
            "limit": limit, 
            "version": "v2",
            "output_format": "v1.1"
            }
        
        if user_id := self.config.get("user_id", ""):
            params["user_id"] = user_id

        memory_type_map = {
            "short_term": {"type": "short_term"},
            "long_term": {"type": "long_term"},
            "entities": {"type": "entity"},
            "external": {"type": "external"},
        }
        
        if self.memory_type in memory_type_map:
            params["metadata"] = memory_type_map[self.memory_type]
            if self.memory_type == "short_term":
                params["run_id"] = self.mem0_run_id