Python SDK for MuninnDB — the cognitive memory database
Project description
MuninnDB Python SDK
An async-first Python client for MuninnDB, a cognitive memory database with semantic search, graph traversal, and real-time subscriptions.
Features
- Async/await throughout — Built on
httpxfor concurrent, non-blocking operations - Semantic memory activation — Query memories by meaning, not keywords
- Graph associations — Link engrams and traverse relationships
- Real-time subscriptions — Server-Sent Events (SSE) with auto-reconnect
- Automatic retry logic — Exponential backoff with jitter for transient failures
- Type-safe — Full type hints for IDE support and runtime validation
- Connection pooling — Configurable keepalive and concurrent connections
Installation
Requirements
- Python 3.11+
Setup
pip install -r requirements.txt
Quick Start
import asyncio
from muninn import MuninnClient
async def main():
async with MuninnClient("http://127.0.0.1:8476") as client:
# Write a memory
engram_id = await client.write(
vault="default",
concept="neural plasticity",
content="The brain's ability to reorganize neural connections.",
tags=["neuroscience", "learning"]
)
print(f"Created: {engram_id}")
# Activate memory (semantic search)
results = await client.activate(
vault="default",
context=["how does learning work?"],
max_results=10
)
for item in results.activations:
print(f"[{item.score:.2f}] {item.concept}")
asyncio.run(main())
API Reference
Core Methods
write(vault, concept, content, tags=None, confidence=0.9, stability=0.5) → str
Write an engram (memory) to the database.
engram_id = await client.write(
vault="default",
concept="example",
content="Long-form content",
tags=["tag1", "tag2"],
confidence=0.95,
stability=0.8
)
Returns: ULID string ID of created engram
activate(vault, context, max_results=10, threshold=0.1, brief_mode="auto") → ActivateResponse
Activate memory using semantic search and optional graph traversal.
result = await client.activate(
vault="default",
context=["query", "terms"],
max_results=10,
threshold=0.1,
brief_mode="extractive" # "auto", "extractive", "abstractive"
)
for item in result.activations:
print(f"Score: {item.score}, Concept: {item.concept}")
for sentence in result.brief or []:
print(f"Brief: {sentence.text}")
Returns: ActivateResponse with:
query_id— Query identifiertotal_found— Total matching engramsactivations— List ofActivationItem(id, concept, content, score, confidence, why, hop_path, dormant)latency_ms— Query latencybrief— Optional extractive/abstractive summary
read(id, vault="default") → ReadResponse
Read a specific engram by ID.
engram = await client.read("01JM2345...", vault="default")
print(engram.concept, engram.confidence)
Returns: ReadResponse with full engram details
forget(id, vault="default", hard=False) → bool
Delete an engram (soft or hard).
# Soft delete (recoverable)
await client.forget(engram_id, vault="default")
# Hard delete (permanent)
await client.forget(engram_id, vault="default", hard=True)
Returns: True on success
link(source_id, target_id, vault="default", rel_type=5, weight=1.0) → bool
Create an association between two engrams.
await client.link(
source_id="01JM...",
target_id="01JM...",
vault="default",
rel_type=5,
weight=0.9
)
Returns: True on success
stats() → StatResponse
Get database statistics and coherence metrics.
stats = await client.stats()
print(f"Engrams: {stats.engram_count}")
print(f"Storage: {stats.storage_bytes} bytes")
if stats.coherence:
for vault_name, coherence in stats.coherence.items():
print(f"Vault {vault_name} coherence: {coherence.score:.2f}")
Returns: StatResponse with engram_count, vault_count, storage_bytes, and coherence dict
subscribe(vault="default", push_on_write=True, threshold=0.0) → SSEStream
Subscribe to real-time vault events via Server-Sent Events.
stream = client.subscribe(vault="default", push_on_write=True)
async for push in stream:
print(f"New engram: {push.engram_id}")
if condition:
await stream.close()
Returns: Async iterable yielding Push events with:
subscription_id— Subscription IDtrigger— Event type ("new_write", etc.)push_number— Push sequence numberengram_id— ID of written engramat— Unix timestamp
health() → bool
Check if MuninnDB server is reachable and healthy.
if await client.health():
print("Server OK")
Returns: True if server responds with 200 OK
Configuration
Create a client with custom settings:
client = MuninnClient(
base_url="http://127.0.0.1:8476", # Server address
token="your-bearer-token", # Optional auth token
timeout=5.0, # Request timeout (seconds)
max_retries=3, # Max retry attempts
retry_backoff=0.5, # Initial backoff multiplier
max_connections=20, # Max concurrent connections
keepalive_connections=10 # Max keepalive pool size
)
Error Handling
The SDK raises semantic error types:
from muninn import (
MuninnError, # Base error
MuninnAuthError, # 401 Unauthorized
MuninnNotFound, # 404 Not Found
MuninnConflict, # 409 Conflict
MuninnServerError, # 5xx Server errors
MuninnConnectionError, # Network errors
MuninnTimeoutError # Request timeout
)
async with MuninnClient() as client:
try:
result = await client.activate(vault="default", context=["query"])
except MuninnAuthError:
print("Invalid token")
except MuninnNotFound:
print("Vault not found")
except MuninnConnectionError:
print("Network error - will retry automatically")
except MuninnError as e:
print(f"Error: {e.status_code} - {e}")
Examples
Example 1: Write and Activate
python examples/write_activate.py
Writes multiple neuroscience engrams and activates them with a query.
Example 2: Real-Time Subscriptions
python examples/subscribe.py
Subscribes to a vault and writes an engram, demonstrating SSE push events.
Example 3: Cognitive Loop
python examples/cognitive_loop.py
Full workflow: write → activate → link → inspect coherence.
Retry Logic
The client automatically retries transient failures:
- Retried errors: 502, 503, 504, network errors, timeouts
- Not retried: 4xx client errors
- Backoff: Exponential with jitter:
retry_backoff * (2^attempt) + random(0, 0.1) - Max attempts: Configured via
max_retries(default: 3)
Example with custom retry settings:
async with MuninnClient(
base_url="http://127.0.0.1:8476",
max_retries=5,
retry_backoff=1.0
) as client:
# This will retry up to 5 times with longer delays
result = await client.activate(vault="default", context=["query"])
Type System
Full type hints enable IDE autocomplete:
from muninn import (
MuninnClient,
ActivateResponse,
ActivationItem,
ReadResponse,
StatResponse,
Push
)
async with MuninnClient() as client:
result: ActivateResponse = await client.activate(vault="default", context=[])
for item: ActivationItem in result.activations:
print(item.score, item.concept)
Performance Tips
- Reuse client: Create one
MuninnClientand reuse it for multiple operations - Batch operations: Use concurrent tasks for parallel writes/activations
- Connection pooling: Adjust
max_connectionsandkeepalive_connectionsbased on workload - Timeout tuning: Increase
timeoutfor large activation queries
import asyncio
async with MuninnClient() as client:
# Parallel writes
tasks = [
client.write(vault="default", concept=f"item-{i}", content="...")
for i in range(100)
]
ids = await asyncio.gather(*tasks)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file muninn_python-0.4.11a0.tar.gz.
File metadata
- Download URL: muninn_python-0.4.11a0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
670b7480af76ab8b6f2898238006034e4459e287dc12850911313caa9451e680
|
|
| MD5 |
1892a2fed4be273704a5d1c27780d47c
|
|
| BLAKE2b-256 |
59921361df64f9cccfcf689e3d76e8f107f68927cd43643ff58ace46718b5101
|
Provenance
The following attestation bundles were made for muninn_python-0.4.11a0.tar.gz:
Publisher:
publish-sdk.yml on scrypster/muninndb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
muninn_python-0.4.11a0.tar.gz -
Subject digest:
670b7480af76ab8b6f2898238006034e4459e287dc12850911313caa9451e680 - Sigstore transparency entry: 1239303370
- Sigstore integration time:
-
Permalink:
scrypster/muninndb@2f2f59ea3865c311c03647359e0096c89a1b0465 -
Branch / Tag:
refs/tags/v0.4.11-alpha - Owner: https://github.com/scrypster
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-sdk.yml@2f2f59ea3865c311c03647359e0096c89a1b0465 -
Trigger Event:
push
-
Statement type:
File details
Details for the file muninn_python-0.4.11a0-py3-none-any.whl.
File metadata
- Download URL: muninn_python-0.4.11a0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
afbf72b5c5d741dc6652d458acbf1f8756c3d6d4e1505a290e00ceed5f3fc8b5
|
|
| MD5 |
a53714e650b570f59d28fd92d0649688
|
|
| BLAKE2b-256 |
5b36bcac85c83895d1fd74e6d66c383e1f35ca5d3bb65e0c5aa9c17d7dcff471
|
Provenance
The following attestation bundles were made for muninn_python-0.4.11a0-py3-none-any.whl:
Publisher:
publish-sdk.yml on scrypster/muninndb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
muninn_python-0.4.11a0-py3-none-any.whl -
Subject digest:
afbf72b5c5d741dc6652d458acbf1f8756c3d6d4e1505a290e00ceed5f3fc8b5 - Sigstore transparency entry: 1239303372
- Sigstore integration time:
-
Permalink:
scrypster/muninndb@2f2f59ea3865c311c03647359e0096c89a1b0465 -
Branch / Tag:
refs/tags/v0.4.11-alpha - Owner: https://github.com/scrypster
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-sdk.yml@2f2f59ea3865c311c03647359e0096c89a1b0465 -
Trigger Event:
push
-
Statement type: