🦜️🔗 LangChain Memgraph
This package contains the LangChain integration with Memgraph graph database.
📦 Installation
In order to start running the examples or tests you need to install the LangChain integration.
You can do it via pip:
pip install -U langchain-memgraph
Before running the examples below, make sure to start Memgraph, you can do it via following command:
docker run -p 7687:7687 \
--name memgraph \
memgraph/memgraph-mage:latest \
--schema-info-enabled=true
💻 Integration features
Memgraph
The Memgraph class is a wrapper around the database client that supports the
query operation.
import os
from langchain_memgraph.graphs.memgraph import MemgraphLangChain
url = os.getenv("MEMGRAPH_URL", "bolt://localhost:7687")
username = os.getenv("MEMGRAPH_USER", "")
password = os.getenv("MEMGRAPH_PASSWORD", "")
graph = MemgraphLangChain(url=url, username=username, password=password)
results = graph.query("MATCH (n) RETURN n LIMIT 1")
print(results)
MemgraphQAChain
The MemgraphQAChain class enables natural language interactions with a Memgraph database.
It uses an LLM and the database's schema to translate a user's question into a Cypher query, which is executed against the database.
The resulting data is then sent along with the user's question to the LLM to generate a natural language response.
For the example below you need to install an extra dependency the lanchain_openai, you can do it by running:
pip install lanchain_openai
import os
from langchain_memgraph.graphs.memgraph import MemgraphLangChain
from langchain_memgraph.chains.graph_qa import MemgraphQAChain
from langchain_openai import ChatOpenAI
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "")
url = os.getenv("MEMGRAPH_URL", "bolt://localhost:7687")
username = os.getenv("MEMGRAPH_USER", "")
password = os.getenv("MEMGRAPH_PASSWORD", "")
graph = MemgraphLangChain(url=url, username=username, password=password, refresh_schema=False)
chain = MemgraphQAChain.from_llm(
ChatOpenAI(temperature=0, model_name="gpt-4.1-mini"),
graph=graph,
model_name="gpt-4.1-mini",
allow_dangerous_requests=True,
)
response = chain.invoke("Is there a any Person node in the dataset?")
result = response["result"].lower()
print(result)
Memgraph toolkit
The MemgraphToolkit contains different tools agents can leverage to perform specific tasks the user has given them. Toolkit
needs a database object and LLM access since different tools leverage different operations.
Currently supported tools:
- RunQueryTool - Cypher query execution
- RunSearchSchemaTool - Search the graph schema by regex pattern
- RunNodeSchemaTool - Get full schema definition of a node by its labels
- RunRelationshipSchemaTool - Get full schema definition of a relationship
- RunEnumSchemaTool - Get schema definition of an enum by its name
import os
import pytest
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
from langchain_memgraph import MemgraphToolkit
from langchain_memgraph.graphs.memgraph import MemgraphLangChain
from langgraph.prebuilt import create_react_agent
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "")
url = os.getenv("MEMGRAPH_URL", "bolt://localhost:7687")
username = os.getenv("MEMGRAPH_USER", "")
password = os.getenv("MEMGRAPH_PASSWORD", "")
llm = init_chat_model("gpt-4o-mini", model_provider="openai")
db = MemgraphLangChain(url=url, username=username, password=password)
toolkit = MemgraphToolkit(db=db, llm=llm)
agent_executor = create_react_agent(
llm, toolkit.get_tools(), prompt="You will get a cypher query, try to execute it on the Memgraph database."
)
example_query = "MATCH (n) WHERE n.name = 'Jon Snow' RETURN n"
events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
last_event = None
for event in events:
last_event = event
event["messages"][-1].pretty_print()
print(last_event)
🧪 Test
Install the test dependencies to run the tests:
- Install dependencies
poetry install --with test,test_integration
- Start Memgraph in the background.
- Create an
.envfile that points to Memgraph and OpenAI API
MEMGRAPH_URL=bolt://localhost:7687
MEMGRAPH_USER=
MEMGRAPH_PASSWORD=
OPENAI_API_KEY=your_openai_api_key
Run tests
Run the unit tests using:
make tests
Run the integration test using:
make integration_tests
🧹 Code Formatting and Linting
Install the codespell, lint, and typing dependencies to lint and format your code:
poetry install --with codespell,lint,typing
To format your code, run:
make format
To lint it, run:
make lint
Metadata
Release files for langchain-memgraph 0.1.17
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| langchain_memgraph-0.1.17.tar.gz | 213.7 kB | Without provenance | Details for langchain_memgraph-0.1.17.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_memgraph-0.1.17-py3-none-any.whl | Python 3 | none | any | Details for langchain_memgraph-0.1.17-py3-none-any.whl |
Total release size: 232.7 kB
Release files / langchain_memgraph-0.1.17.tar.gz
| Download URL | langchain_memgraph-0.1.17.tar.gz |
|---|---|
| Size | 213.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ce64bfd42d2a96c1ba430983fef0a0bf6fd7e662c2b66e5545a71ce8e0cae47b
|
|
BLAKE2b-256 checksum How to use checksums |
3db627b986e47c012d3a35df8527da4e8e34873abead968deb86fac7c9056efb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / langchain_memgraph-0.1.17-py3-none-any.whl
| Download URL | langchain_memgraph-0.1.17-py3-none-any.whl |
|---|---|
| Size | 19.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7f3b9ee8e5736f9fed8e073fdc0d2d696d3e6fb62e896ae7a0c0a215c795a189
|
|
BLAKE2b-256 checksum How to use checksums |
1509e6309421f3709968a6736ee77501524d560880724f18dcf9c76ed873ee7e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for langchain-memgraph 0.1.17
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.