Skip to main content

mnem (Python)

Python bindings for mnem - git for knowledge graphs, with retrieval under a token budget.

pip install mnem-py
import pymnem

# In-memory (tests, notebooks, agent sessions without persistence)
repo = pymnem.Repo.init_memory

# Or on-disk via the embedded redb backend
# repo = pymnem.Repo.open_or_init("/path/to/repo.redb")

# Write one node at a time
repo.commit_node(
    author="alice@example.com",
    message="seed",
    ntype="Memory",
    summary="Alice lives in Berlin and works at Globex",
    props={"name": "Alice", "city": "Berlin"},
)

# Or batch with a transaction (context-manager commits on clean exit)
with repo.transaction(author="alice@example.com", message="seed batch") as tx:
    tx.add_node(ntype="Memory", summary="Alice's hobby is climbing")
    tx.add_node(ntype="Memory", summary="Bob moved to Paris last month")

# Retrieve under a token budget - dense vector + optional learned-sparse, RRF-fused
result = repo.retrieve(
    text="Alice Berlin",
    token_budget=500,
    limit=10,
)
for item in result:
    print(f"{item.score:.3f} [{item.tokens}t] {item.summary}")

print(f"used {result.tokens_used}/{result.tokens_budget} tokens,",
      f"{result.dropped} dropped of {result.candidates_seen} candidates")

What you get from Python

  • pymnem.Repo - the repository handle, with init_memory, open_or_init, op_id, head_commit_cid, commit_node, transaction, retrieve, query.
  • pymnem.Transaction - a context manager for batched writes. Commits on clean exit, abandons on exception. Supports add_node and add_embedding_f32.
  • pymnem.RetrievalResult / pymnem.RetrievedItem - plain dataclass-shaped results with node_id, ntype, summary, rendered, tokens, score plus cost metadata.
  • pymnem.MnemError - the exception base class every mnem error inherits from.

What's deferred

The v1 Python surface deliberately leaves out:

  • Signing and verification. Callers that need Ed25519 signatures build their signer in their own tooling; the Rust side remains the source of truth.
  • CAS on refs, diff, merge. These are powerful but rarely touched from Python; open an issue with a concrete use case if you need them.
  • Structured Edge writes. add_edge lands once the first Python caller asks for it.
  • MCP server bindings. The mnem mcp binary is already Python-host-friendly (it speaks JSON-RPC 2.0 over stdio); no need to rewrap it.

Build from source

If you want to develop the bindings locally:

pip install maturin
cd crates/mnem-py
maturin develop --release      # installs into your active venv
python -c "import pymnem; print(pymnem.__version__)"

Publishing wheels:

maturin build --release --strip
# Upload target/wheels/mnem_py-*.whl to PyPI with `twine upload`.

Pre-release verification (manual step before tagging 0.1.0)

cargo test -p mnem-py --lib covers the Rust-side parser and gate unit tests, but the PyO3 ABI layer is only exercised through a real Python interpreter. Before cutting a release tag, run the Python-linked regression once on the target platform:

cd crates/mnem-py
maturin develop
pytest tests/

The pytest suite includes tests/test_mnem_bench_gate.py which pins the MNEM_BENCH coercion behaviour across Repo.commit_node, Transaction.add_node, Repo.retrieve, and Repo.query. Skipping this step means shipping the pyo3 bindings without having loaded them.

Performance envelope

The Python wrapper is a thin pyo3 layer; retrieval throughput is what the Rust core measures in docs/benchmarks/ai-native.md. At 1000 Doc nodes on laptop hardware:

  • retrieve(text=...) fresh-index end-to-end: ~6 ms (in-memory) / ~14 ms (redb)
  • Amortised text search (pre-built index held for a session): ~11 µs (memory) / ~21 µs (redb)
  • Fused text + vector retrieve: ~10 ms (memory) / ~21 ms (redb)

The Python-to-Rust boundary costs <50 µs per call in practice, well below the retrieval work.

License

Apache-2.0, same as the core crate. See LICENSE.

Metadata

Release files for mnem-py 0.1.0

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

Built distributions (wheels)

Table of built distributions (wheels) for mnem-py 0.1.0
File
mnem_py-0.1.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
mnem_py-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
mnem_py-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details
mnem_py-0.1.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details

Total release size: 4.7 MB

Release files / mnem_py-0.1.0-cp39-abi3-win_amd64.whl

Download URL mnem_py-0.1.0-cp39-abi3-win_amd64.whl
Size 1.2 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
06b3490b9ecb71bc9f752619934cce62fd71178328d05b27030c6ccf09e6a3f9
BLAKE2b-256 checksum
How to use checksums
0b96aeef8b3a1578a453803f66acebff36b3258f0cfd45121abd18eda8ac96d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 3, 2026.

Transparency log

Release files / mnem_py-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL mnem_py-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.3 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
d843fb5a9aa45ba60a8ab320af5d5367fdd8de3af5b8cbd5e4f175d41c15ec9c
BLAKE2b-256 checksum
How to use checksums
794681c94977287af339458bd0e27090269907dea14ea30e2a92f2c1ee39ae7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 3, 2026.

Transparency log

Release files / mnem_py-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL mnem_py-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 1.2 MB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
f5805b4f0dd25dd7ccc3db22c01b5a63abecdb24555055c6b83c49ec5a3f2ce9
BLAKE2b-256 checksum
How to use checksums
8294e091b6fe79421531b2276a23a5a936b2820e6f8935aff30a58242433710f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 3, 2026.

Transparency log

Release files / mnem_py-0.1.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL mnem_py-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fc6ea57425e9eec0172d9a78935c3ed59f7afbe8456ce09e0a73f374ce86e2cf
BLAKE2b-256 checksum
How to use checksums
02c876e97d1e0555c1d356222fb3e7eb1c14e401d7a1ce3c5d6361fc348891a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 3, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

4 release files

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