Skip to content

Changed Mem0 Storage v1.1 -> v2 #2893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

Vidit-Ostwal
Copy link
Contributor

@Vidit-Ostwal Vidit-Ostwal commented May 23, 2025

This PR bumps the Mem0 storage version from v1.1 to v2, for MemoryClient.
Fixes #2776

This is the documentation I have refered to:
https://docs.mem0.ai/platform/features/contextual-add#using-user-id-with-run-id

Following is the list of changed done:

  • Add run_id parameter for short_term memory docs
  • Added features like Custom Categories, includes, excludes docs, docs
  • Changed to version from v1.1 to v2
  • Refactored the code base for better readability.

@joaomdmoura
Copy link
Collaborator

Disclaimer: This review was made by a crew of AI Agents.

Code Review Comment for PR #2893

Overview

This pull request introduces significant changes to the mem0_storage.py file, including an update of the output format from "v1.1" to "v2" and the introduction of a run_id parameter for better operational context tracking. The changes are aimed at improving the functionality while ensuring backward compatibility.

Positive Aspects

  • The implementation of the run_id parameter is consistent across methods.
  • The changes maintain backward compatibility with local Memory instances.
  • The new configuration parameter is cleanly integrated.

Issues and Suggestions

  1. Documentation Enhancement
    The new run_id parameter is crucial for understanding the context of operations but is lacking in documentation. Please add docstring documentation to the constructor:

    def __init__(self, type, crew=None, config=None):
        """Initialize Mem0Storage.
        
        Args:
            type: Type of storage
            crew: Crew instance
            config (dict): Configuration dictionary containing:
                - api_key: Mem0 API key
                - org_id: Organization ID
                - project_id: Project ID
                - local_mem0_config: Local configuration
                - run_id: Run identifier for tracking related operations
        """
  2. Null Safety for run_id
    Ensure that there’s proper handling for cases when run_id may be None. Implement null checking in methods that utilize this parameter:

    def search(self, query: str, limit: int = 3, score_threshold: float = 0.35) -> List[Any]:
        params = {
            "query": query,
            "limit": limit,
            "output_format": "v2"
        }
        
        if self.mem0_run_id:
            params["run_id"] = self.mem0_run_id
  3. Constant for Versioning
    Rather than hard-coding the output format, define it as a constant at the top of the file:

    OUTPUT_FORMAT_VERSION = "v2"
    params["output_format"] = OUTPUT_FORMAT_VERSION
  4. Error Handling Improvements
    Add validation to ensure that run_id, when present, is of the correct type:

    def __init__(self, type, crew=None, config=None):
        run_id = config.get("run_id")
        if run_id is not None and not isinstance(run_id, str):
            raise ValueError("run_id must be a string or None")
        self.mem0_run_id = run_id
  5. Code Organization
    The parameter deletion within the search() method could be structured more elegantly to enhance readability:

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

General Recommendations

  • Introduce unit tests to specifically validate the functionality associated with the new run_id feature.
  • Update the changelog to reflect the version change from v1.1 to v2, ensuring users are aware of the modifications.
  • Consider validating the format of the run_id, particularly if specific patterns are expected.
  • Implement logging to capture the usage and context of run_id in operations.
  • Consider having a method to allow runtime updates of run_id if needed.

Breaking Changes

  • The transition from "v1.1" to "v2" may disrupt clients expecting the older format. It’s advisable to include deprecation warnings for the previous version.

Security Considerations

  • Ensure input values related to run_id are sanitized before being utilized in API calls.
  • Implement length limits for run_id to prevent potential abuse.

This review highlights several areas of improvement while appreciating the overall trajectory toward better functionality. The suggestions focus on enhancing code quality, maintainability, and robustness to support future development in the project.

Copy link

@rusXL rusXL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall:

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.
run_id should be only allowed to be used for short-term memory.

Secondly, there is no ability to turn off agent memories. The agent name is always being passed as agent ID with every add API call, but in some situations, it is necessary that agents are memory-less.

Thirdly, it would be cool to have support for mem0 new features such as (I find the most relevant once) memory inclusion, custom categories.

You can add support for custom categories in init:

def __init__(self, type, crew=None, config=None):
...
# get new_categories from config, then
self.memory.update_project(custom_categories=new_categories)

You can add support for memory inclusion

# get includes from config, then with each save
params["includes"] = includes

And last but not the least, would be great to move from deprecated search v1 to search v2 - mem0 api reference.
As far as I am concerned, there is not such thing as output_format=v2

We are ready to help :)

@rusXL
Copy link

rusXL commented May 25, 2025

And no, you did not really change mem0 v1.1 -> v2

@Vidit-Ostwal
Copy link
Contributor Author

And no, you did not really change mem0 v1.1 -> v2

Hi @rusXL, I look into this.

@Vidit-Ostwal
Copy link
Contributor Author

I have to fix the test cases.

@Vidit-Ostwal
Copy link
Contributor Author

Vidit-Ostwal commented May 28, 2025

Hi @rusXL, do let me know your thoughts on the new codebase?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Mem0 Memory transition to V2
4 participants