From 097959d5cc2b79ad062b5b0747d763c2b132af58 Mon Sep 17 00:00:00 2001 From: Dev Khant Date: Thu, 22 May 2025 10:16:32 +0530 Subject: [PATCH] Remove support for passing string as input in the client.add() (#2749) --- docs/features/timestamp.mdx | 20 ++++++++++++++++---- docs/platform/quickstart.mdx | 15 ++++++++++++--- mem0/client/main.py | 22 ++++++++-------------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/docs/features/timestamp.mdx b/docs/features/timestamp.mdx index 81333840..ac38de13 100644 --- a/docs/features/timestamp.mdx +++ b/docs/features/timestamp.mdx @@ -59,7 +59,10 @@ five_days_ago = current_time - timedelta(days=5) unix_timestamp = int(five_days_ago.timestamp()) # Add memory with custom timestamp -client.add("I'm travelling to SF", user_id="user1", timestamp=unix_timestamp) +messages = [ + {"role": "user", "content": "I'm travelling to SF"} +] +client.add(messages, user_id="user1", timestamp=unix_timestamp) ``` ```javascript JavaScript @@ -77,7 +80,10 @@ fiveDaysAgo.setDate(currentTime.getDate() - 5); const unixTimestamp = Math.floor(fiveDaysAgo.getTime() / 1000); // Add memory with custom timestamp -client.add("I'm travelling to SF", { user_id: "user1", timestamp: unixTimestamp }) +const messages = [ + {"role": "user", "content": "I'm travelling to SF"} +] +client.add(messages, { user_id: "user1", timestamp: unixTimestamp }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` @@ -119,14 +125,20 @@ For example, to create a memory with a timestamp of January 1, 2023: # January 1, 2023 timestamp january_2023_timestamp = 1672531200 # Unix timestamp for 2023-01-01 00:00:00 UTC -client.add("Important historical information", user_id="user1", timestamp=january_2023_timestamp) +messages = [ + {"role": "user", "content": "I'm travelling to SF"} +] +client.add(messages, user_id="user1", timestamp=january_2023_timestamp) ``` ```javascript JavaScript // January 1, 2023 timestamp const january2023Timestamp = 1672531200; // Unix timestamp for 2023-01-01 00:00:00 UTC -client.add("Important historical information", { user_id: "user1", timestamp: january2023Timestamp }) +const messages = [ + {"role": "user", "content": "I'm travelling to SF"} +] +client.add(messages, { user_id: "user1", timestamp: january2023Timestamp }) .then(response => console.log(response)) .catch(error => console.error(error)); ``` diff --git a/docs/platform/quickstart.mdx b/docs/platform/quickstart.mdx index 83b4e1d0..2850646b 100644 --- a/docs/platform/quickstart.mdx +++ b/docs/platform/quickstart.mdx @@ -64,7 +64,10 @@ client = AsyncMemoryClient() async def main(): - response = await client.add("I'm travelling to SF", user_id="john") + messages = [ + {"role": "user", "content": "I'm travelling to SF"} + ] + response = await client.add(messages, user_id="john") print(response) await main() @@ -1547,11 +1550,17 @@ Fun fact: You can also delete the memory using the `add()` method by passing a n ```python Python -client.add("Delete all of my food preferences", user_id="alex") +messages = [ + {"role": "user", "content": "Delete all of my food preferences"} +] +client.add(messages, user_id="alex") ``` ```javascript JavaScript -client.add("Delete all of my food preferences", { user_id: "alex" }) +const messages = [ + {"role": "user", "content": "Delete all of my food preferences"} +] +client.add(messages, { user_id: "alex" }) .then(result => console.log(result)) .catch(error => console.error(error)); ``` diff --git a/mem0/client/main.py b/mem0/client/main.py index 8f69fd04..ae6dd5d6 100644 --- a/mem0/client/main.py +++ b/mem0/client/main.py @@ -3,7 +3,7 @@ import logging import os import warnings from functools import wraps -from typing import Any, Dict, List, Optional, Union +from typing import Any, Dict, List, Optional import httpx import requests @@ -129,11 +129,11 @@ class MemoryClient: raise ValueError(f"Error: {error_message}") @api_error_handler - def add(self, messages: Union[str, List[Dict[str, str]]], **kwargs) -> Dict[str, Any]: + def add(self, messages: List[Dict[str, str]], **kwargs) -> Dict[str, Any]: """Add a new memory. Args: - messages: Either a string message or a list of message dictionaries. + messages: A list of message dictionaries. **kwargs: Additional parameters such as user_id, agent_id, app_id, metadata, filters. Returns: @@ -667,7 +667,7 @@ class MemoryClient: return response.json() def _prepare_payload( - self, messages: Union[str, List[Dict[str, str]], None], kwargs: Dict[str, Any] + self, messages: List[Dict[str, str]], kwargs: Dict[str, Any] ) -> Dict[str, Any]: """Prepare the payload for API requests. @@ -679,10 +679,7 @@ class MemoryClient: A dictionary containing the prepared payload. """ payload = {} - if isinstance(messages, str): - payload["messages"] = [{"role": "user", "content": messages}] - elif isinstance(messages, list): - payload["messages"] = messages + payload["messages"] = messages payload.update({k: v for k, v in kwargs.items() if v is not None}) return payload @@ -798,7 +795,7 @@ class AsyncMemoryClient: raise ValueError(f"Error: {error_message}") def _prepare_payload( - self, messages: Union[str, List[Dict[str, str]], None], kwargs: Dict[str, Any] + self, messages: List[Dict[str, str]], kwargs: Dict[str, Any] ) -> Dict[str, Any]: """Prepare the payload for API requests. @@ -810,10 +807,7 @@ class AsyncMemoryClient: A dictionary containing the prepared payload. """ payload = {} - if isinstance(messages, str): - payload["messages"] = [{"role": "user", "content": messages}] - elif isinstance(messages, list): - payload["messages"] = messages + payload["messages"] = messages payload.update({k: v for k, v in kwargs.items() if v is not None}) return payload @@ -850,7 +844,7 @@ class AsyncMemoryClient: await self.async_client.aclose() @api_error_handler - async def add(self, messages: Union[str, List[Dict[str, str]]], **kwargs) -> Dict[str, Any]: + async def add(self, messages: List[Dict[str, str]], **kwargs) -> Dict[str, Any]: kwargs = self._prepare_params(kwargs) if kwargs.get("output_format") != "v1.1": kwargs["output_format"] = "v1.1"