ImportError: langchain_memgraph not installed when using mem0ai[graph] extra
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 installwithmem0ai[graph]>=0.1.1in pyproject.toml
Steps to Reproduce
- Install mem0ai with graph extras:
pip install mem0ai[graph] - Try to initialize Memory service:
Memory.from_config(config) - Error occurs during import of
MemoryGraphclass
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
- Missing dependency declaration:
langchain-memgraphmight not be properly declared in the[graph]extra dependencies inpyproject.tomlorsetup.py - Conditional import issue: The import is happening unconditionally in
memgraph_memory.pyeven when graph functionality is not being used - Version compatibility issue: There might be a version conflict preventing proper installation
Suggested Solutions
- Fix dependency declaration: Ensure
langchain-memgraphis properly included in the[graph]extra dependencies - Make import conditional: Only import
langchain_memgraphwhen actually needed, not at module level - 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.
from langchain_memgraph.graphs.memgraph import Memgraph
@aryanranderiya Closing as this was fixed!
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.
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.