mem0 icon indicating copy to clipboard operation
mem0 copied to clipboard

ImportError: langchain_memgraph not installed when using mem0ai[graph] extra

Open aryanranderiya opened this issue 7 months ago • 1 comments

Bug Description

When installing mem0ai[graph] and initializing the Memory service, the application crashes with an ImportError stating that langchain_memgraph is not installed, even though it should be included as part of the [graph] extra dependencies.

Error Details

ImportError: langchain_memgraph is not installed. Please install it using pip install langchain-memgraph

Full Stack Trace:

File "/app/app/services/memory_service.py", line 20, in __init__
  self.client = get_memory_client()
File "/app/app/memory/client.py", line 60, in get_memory_client
  return Memory.from_config(config)
File "/usr/local/lib/python3.12/site-packages/mem0/memory/main.py", line 161, in from_config
  return cls(config)
File "/usr/local/lib/python3.12/site-packages/mem0/memory/main.py", line 135, in __init__
  from mem0.memory.memgraph_memory import MemoryGraph
File "/usr/local/lib/python3.12/site-packages/mem0/memory/memgraph_memory.py", line 8, in <module>
  raise ImportError("langchain_memgraph is not installed. Please install it using pip install langchain-memgraph")

Error Location in Code: The error occurs in /mem0/memory/memgraph_memory.py at line 8:

try:
    from langchain_memgraph import Memgraph
except ImportError:
    raise ImportError(
        "langchain_memgraph is not installed. Please install it using pip install langchain-memgraph"
    )

Environment

  • mem0ai version: 0.1.93+ (installed via mem0ai[graph]>=0.1.1)
  • Python version: 3.12
  • Operating System: Linux (Docker container)
  • Installation method: uv pip install with mem0ai[graph]>=0.1.1 in pyproject.toml

Steps to Reproduce

  1. Install mem0ai with graph extras: pip install mem0ai[graph]
  2. Try to initialize Memory service: Memory.from_config(config)
  3. Error occurs during import of MemoryGraph class

Expected Behavior

When installing mem0ai[graph], all necessary dependencies including langchain-memgraph should be installed automatically, and the Memory service should initialize without ImportError.

Actual Behavior

The application crashes with ImportError because langchain_memgraph is not installed, despite being required for the graph functionality.

Possible Root Causes

  1. Missing dependency declaration: langchain-memgraph might not be properly declared in the [graph] extra dependencies in pyproject.toml or setup.py
  2. Conditional import issue: The import is happening unconditionally in memgraph_memory.py even when graph functionality is not being used
  3. Version compatibility issue: There might be a version conflict preventing proper installation

Suggested Solutions

  1. Fix dependency declaration: Ensure langchain-memgraph is properly included in the [graph] extra dependencies
  2. Make import conditional: Only import langchain_memgraph when actually needed, not at module level
  3. Add proper error handling: Provide clearer error messages about which extra dependencies are needed

Workaround

Manually install the missing dependency:

pip install langchain-memgraph

Related Issues

This appears to be related to dependency management for optional graph functionality. Similar issues might occur with other optional dependencies if they're not properly declared in the extras.


Additional Context: This issue prevents using mem0ai with graph functionality in containerized environments where only explicitly declared dependencies are installed.

aryanranderiya avatar May 27 '25 05:05 aryanranderiya

from langchain_memgraph.graphs.memgraph import Memgraph

linshibo avatar May 28 '25 08:05 linshibo

@aryanranderiya Closing as this was fixed!

parshvadaftari avatar Sep 05 '25 12:09 parshvadaftari

I noticed that langchain-memgraph has been added to [pyproject.toml] (https://github.com/mem0ai/mem0/blob/main/pyproject.toml) under the "extras" dependencies, and not the "graph" dependencies. This has caused some confusion when the official documentation says only mem0ai[graph] is required.

hunganhdau avatar Oct 02 '25 14:10 hunganhdau

Hey @hunganhdau this has been fixed with the latest commit. Really sorry for the trouble and thanks a lot for pointing out the bug. The new pypi will be released soon.

parshvadaftari avatar Oct 02 '25 16:10 parshvadaftari