Skip to main content

🔗 lightrag-memgraph

lightrag-memgraph is an integration that connects lightrag and memgraph. The library began as a small wrapper designed to specifically configure Memgraph within a pipeline that processes unstructured data (various texts) and transforms it into an ontology/entity schema graph. In other words, it enables you to extract and enhance entities from unstructured documents, storing them in a graph for powerful querying and analysis. Ideal for building knowledge graphs, improving data discovery, and leveraging advanced AI techniques on top of your domain data.

General Notes

  • Entity/relationship extraction is high-quality, but also high-cost and relatively slow.
  • The goal over time is to expose time and cost metrics (e.g., $ per your specific document page or chunk).

Quick start

Prerequisites: Memgraph MAGE running (default bolt://localhost:7687; the memgraph-mage image, not plain memgraph, since the default embeddings below need its embeddings module), and an LLM API key (e.g. OPENAI_API_KEY or ANTHROPIC_API_KEY).

Install:

pip install lightrag-memgraph

Minimal example (async): create the wrapper, initialize with a working *directory, insert text, then finalize.

import asyncio
from lightrag_memgraph import MemgraphLightRAGWrapper


async def main():
    wrapper = MemgraphLightRAGWrapper()
    await wrapper.initialize(working_dir="./lightrag_storage")
    await wrapper.ainsert(input="Your document text here.", file_paths=["doc1"])
    # optional: rag = wrapper.get_lightrag(); print(await rag.get_graph_labels())
    await wrapper.afinalize()


asyncio.run(main())

See example.py in this repo for a full run with sample texts and graph output.

initialize(**lightrag_kwargs) forwards anything to LightRAG(...), and explicit values override the Memgraph-storage defaults — e.g. max_parallel_insert=8, or addon_params={"entity_types_guidance": "..."} to steer entity extraction toward a specific vocabulary. unstructured2graph produces that addon_params from an ontology file via Ontology.addon_params().

Storage / persistence

By default the wrapper persists LightRAG's entire working state into Memgraph, not just the entity/relationship graph:

LightRAG store Backend Memgraph node label
Graph (entities + relations) MemgraphStorage (built into lightrag) <workspace> (default base)
Key/value (full_docs, text_chunks, llm_response_cache, ...) MemgraphKVStorage LightRAGKV_<workspace>_<namespace>
Vector (entities, relationships, chunks) MemgraphVectorStorage LightRAGVector_<workspace>_<namespace>
Document status MemgraphDocStatusStorage LightRAGDocStatus_<workspace>

The KV/vector/doc-status labels are namespaced by workspace + namespace so they never collide with the graph's entity nodes. Vectors always persist to Memgraph's native vector index (CREATE VECTOR INDEX ... {"metric": "cos"} + CALL vector_search.search(...)), so a real embedding_func is required -- see Embeddings below for the default.

Connection settings are read from the same environment variables as lightrag's graph backend: MEMGRAPH_URI (or MEMGRAPH_URL, which the wrapper bridges to MEMGRAPH_URI), MEMGRAPH_USERNAME, MEMGRAPH_PASSWORD, MEMGRAPH_DATABASE and the optional MEMGRAPH_WORKSPACE.

Opting out

  • MemgraphLightRAGWrapper(full_memgraph_persistence=False) keeps only the graph in Memgraph and stores KV/vector/doc-status as local JSON files in working_dir (the previous behaviour).

To register the backends manually (e.g. when calling LightRAG(...) directly rather than through the wrapper):

from lightrag import LightRAG
from lightrag_memgraph import register_memgraph_storage

register_memgraph_storage()  # idempotent; call before constructing LightRAG
rag = LightRAG(
    graph_storage="MemgraphStorage",
    kv_storage="MemgraphKVStorage",
    vector_storage="MemgraphVectorStorage",
    doc_status_storage="MemgraphDocStatusStorage",
    ...,
)

Embeddings

If embedding_func is not passed to initialize(), the wrapper defaults to memgraph_sentence_embed: a local sentence-transformer (all-MiniLM-L6-v2, 384 dims) run by Memgraph itself, via the embeddings MAGE module's embeddings.text() procedure. This requires no API key and makes no external network calls -- unlike LightRAG's own openai_embed, which used to be this wrapper's silent default and would bill your OPENAI_API_KEY for every insert/query even if you only meant to use OpenAI for the LLM, or not at all. Applying this default is logged as a warning so it's never silent.

This default requires the memgraph-mage Docker image (not plain memgraph) so the embeddings module is loaded.

To use a different local model served the same way, build a variant with build_memgraph_sentence_embed:

from lightrag_memgraph import build_memgraph_sentence_embed

# A different local model -- embedding_dim must match what it actually outputs.
embedding_func = build_memgraph_sentence_embed(model_name="all-mpnet-base-v2", embedding_dim=768)

To use OpenAI/Anthropic-compatible/other Python embedding functions instead, pass embedding_func explicitly, as shown in the Anthropic and OpenAI sections below.

Using Anthropic (Claude) as the LLM

LightRAG supports Claude via the lightrag.llm.anthropic module. Set your API key and pass the LLM function and model name when initializing the wrapper. The list of Anthropic models is available under https://platform.claude.com/docs/en/about-claude/models.

  1. Set the API key (required for Claude):

    export ANTHROPIC_API_KEY="your-anthropic-api-key"
    
  2. Use Anthropic in code by passing llm_model_func and llm_model_name to initialize():

    from lightrag.llm.anthropic import anthropic_complete
    from lightrag_memgraph import MemgraphLightRAGWrapper
    
    wrapper = MemgraphLightRAGWrapper()
    await wrapper.initialize(
        working_dir="./lightrag_storage",
        llm_model_func=anthropic_complete,
        llm_model_name="claude-sonnet-4-20250514",  # any current Claude model id
        # embedding_func omitted: defaults to Memgraph's local sentence-transformer,
        # see Embeddings above. Anthropic itself has no embeddings API.
    )
    

    Preset functions are also available: claude_3_opus_complete, claude_3_sonnet_complete, claude_3_haiku_complete (fixed older model IDs). For current models, use anthropic_complete with the desired llm_model_name.

  3. Embeddings: Anthropic does not provide embeddings. Leaving embedding_func unset (as above) uses Memgraph's local sentence-transformer default -- no additional API key needed, no silent no-op option exists (a real embedding_func always runs since vectors persist to Memgraph's native vector index). To use a different provider instead, set embedding_func explicitly, e.g. openai_embed from lightrag.llm.openai with OPENAI_API_KEY, or Voyage AI via lightrag.llm.anthropic.anthropic_embed with VOYAGE_API_KEY.

Using OpenAI as the LLM

Set your API key and optionally choose a model.

  1. Set the API key:

    export OPENAI_API_KEY="your-openai-api-key"
    
  2. Use a specific OpenAI model by passing llm_model_func and optionally llm_model_name:

    from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
    from lightrag_memgraph import MemgraphLightRAGWrapper
    
    wrapper = MemgraphLightRAGWrapper()
    await wrapper.initialize(
        working_dir="./lightrag_storage",
        llm_model_func=gpt_4o_mini_complete,
        embedding_func=openai_embed,
    )
    

Metadata

Release files for lightrag-memgraph 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lightrag-memgraph 0.3.4
File Size Uploaded Provenance Actions
lightrag_memgraph-0.3.4.tar.gz 36.4 kB Without provenance Details for lightrag_memgraph-0.3.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lightrag-memgraph 0.3.4
File Interpreter ABI Platform Actions
lightrag_memgraph-0.3.4-py3-none-any.whl Python 3 none any Details for lightrag_memgraph-0.3.4-py3-none-any.whl

Total release size: 65.9 kB

Release files / lightrag_memgraph-0.3.4.tar.gz

Download URL lightrag_memgraph-0.3.4.tar.gz
Size 36.4 kB
Tags Source
SHA-256 checksum
How to use checksums
567f778bd8d101d2c618c19a62bc46155e2bfd65407db539d7457254d81523fb
BLAKE2b-256 checksum
How to use checksums
04df409fb5e42c4d7bc1343f460c5a8203751001b51011f64eaefd13b4bc6644
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","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 / lightrag_memgraph-0.3.4-py3-none-any.whl

Download URL lightrag_memgraph-0.3.4-py3-none-any.whl
Size 29.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cfc65ce1a3f166a5a2a5cfeecc2179b9e60de74ec9d3819cbb9df2256736797f
BLAKE2b-256 checksum
How to use checksums
66f50d59d98ff5b1063596a91081dda6ea907c1102202b9ad202658a8293e938
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","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 lightrag-memgraph 0.3.4

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page