|
| 1 | +# Memoria Python SDK |
| 2 | + |
| 3 | +Official Python SDK for the [Memoria](https://github.com/matrixorigin/Memoria) memory engine. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +**v1 — GitHub Release wheel:** |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install https://github.com/matrixorigin/Memoria/releases/download/python-sdk-v1.0.0/memoria_client-1.0.0-py3-none-any.whl |
| 11 | +``` |
| 12 | + |
| 13 | +**Offline / air-gapped:** |
| 14 | + |
| 15 | +```bash |
| 16 | +pip install ./memoria_client-1.0.0-py3-none-any.whl |
| 17 | +``` |
| 18 | + |
| 19 | +**From source (development):** |
| 20 | + |
| 21 | +```bash |
| 22 | +pip install -e ".[dev]" |
| 23 | +``` |
| 24 | + |
| 25 | +## Quick Start |
| 26 | + |
| 27 | +```python |
| 28 | +from memoria import MemoriaClient |
| 29 | + |
| 30 | +with MemoriaClient(base_url="http://localhost:8100", api_key="sk-...") as client: |
| 31 | + # Store a memory |
| 32 | + mem = client.memories.store(content="Prefers concise answers", memory_type="profile") |
| 33 | + |
| 34 | + # Retrieve relevant memories |
| 35 | + result = client.memories.retrieve(query="answer style", top_k=5) |
| 36 | + for item in result.items: |
| 37 | + print(item.content) |
| 38 | + |
| 39 | + # Observe a conversation turn (auto-extracts memories) |
| 40 | + client.observe(messages=[ |
| 41 | + {"role": "user", "content": "What is the capital of France?"}, |
| 42 | + {"role": "assistant", "content": "Paris."}, |
| 43 | + ]) |
| 44 | +``` |
| 45 | + |
| 46 | +### Async usage |
| 47 | + |
| 48 | +```python |
| 49 | +from memoria import AsyncMemoriaClient |
| 50 | + |
| 51 | +async with AsyncMemoriaClient(base_url="http://localhost:8100", api_key="sk-...") as client: |
| 52 | + await client.ping() |
| 53 | + mem = await client.memories.store(content="...", memory_type="semantic") |
| 54 | + result = await client.memories.retrieve(query="...") |
| 55 | +``` |
| 56 | + |
| 57 | +## Resources |
| 58 | + |
| 59 | +### memories |
| 60 | + |
| 61 | +```python |
| 62 | +# Store |
| 63 | +mem = client.memories.store(content="...", memory_type="semantic") |
| 64 | + |
| 65 | +# Batch store (max 100 items, max 32 KiB per item) |
| 66 | +mems = client.memories.store_batch([{"content": "a"}, {"content": "b"}]) |
| 67 | + |
| 68 | +# Retrieve (hybrid vector + fulltext) |
| 69 | +result = client.memories.retrieve(query="...", top_k=5) |
| 70 | + |
| 71 | +# Search |
| 72 | +result = client.memories.search(query="...", top_k=10) |
| 73 | + |
| 74 | +# List with pagination |
| 75 | +page = client.memories.list(limit=100, cursor=None) |
| 76 | +# page.next_cursor — pass as cursor= to get the next page |
| 77 | + |
| 78 | +# Correct by ID |
| 79 | +mem = client.memories.correct("mem_id", new_content="...", reason="...") |
| 80 | + |
| 81 | +# Correct by semantic query |
| 82 | +mem = client.memories.correct_by_query(query="old content", new_content="new content") |
| 83 | + |
| 84 | +# Delete |
| 85 | +client.memories.delete("mem_id", reason="done") |
| 86 | + |
| 87 | +# Purge (choose one selector) |
| 88 | +result = client.memories.purge(memory_ids=["id1", "id2"], reason="cleanup") |
| 89 | +result = client.memories.purge(topic="debug session", reason="done") |
| 90 | +result = client.memories.purge(session_id="sess_x", memory_types=["working"], reason="end") |
| 91 | + |
| 92 | +# Feedback |
| 93 | +client.memories.feedback("mem_id", signal="useful") # useful|irrelevant|outdated|wrong |
| 94 | +``` |
| 95 | + |
| 96 | +### snapshots |
| 97 | + |
| 98 | +```python |
| 99 | +snap = client.snapshots.create(name="before-cleanup") |
| 100 | +snaps = client.snapshots.list(limit=20) |
| 101 | +client.snapshots.rollback("before-cleanup") |
| 102 | + |
| 103 | +client.snapshots.delete("before-cleanup") # single |
| 104 | +client.snapshots.delete(names=["s1", "s2"]) # multiple |
| 105 | +client.snapshots.delete(prefix="pre_") # by prefix |
| 106 | +client.snapshots.delete(older_than="2026-01-01") # by date |
| 107 | +``` |
| 108 | + |
| 109 | +### branches |
| 110 | + |
| 111 | +```python |
| 112 | +client.branches.create(name="experiment-1") |
| 113 | +branches = client.branches.list() |
| 114 | +client.branches.checkout(name="experiment-1") |
| 115 | + |
| 116 | +diff = client.branches.diff("experiment-1") # summary stats |
| 117 | +items = client.branches.diff_items("experiment-1", limit=50) # per-entry |
| 118 | + |
| 119 | +client.branches.merge("experiment-1", strategy="accept") |
| 120 | +client.branches.apply("experiment-1", adds=["mem_id_1"]) |
| 121 | +client.branches.pick("experiment-1", selector={"type": "key_list", "keys": ["mem_id_1"]}) |
| 122 | +client.branches.delete("experiment-1") |
| 123 | +``` |
| 124 | + |
| 125 | +### governance |
| 126 | + |
| 127 | +```python |
| 128 | +result = client.governance.run() |
| 129 | +if result.skipped: |
| 130 | + print(f"Cooldown: {result.cooldown_remaining_s}s remaining") |
| 131 | +else: |
| 132 | + print(f"Cleaned {result.cleaned_stale} stale memories") |
| 133 | + |
| 134 | +result = client.governance.consolidate() |
| 135 | +result = client.governance.reflect(mode="auto") # auto|candidates|internal |
| 136 | +result = client.governance.reflect(mode="candidates") # never on cooldown |
| 137 | + |
| 138 | +# Bypass cooldown |
| 139 | +client.governance.run(force=True) |
| 140 | +``` |
| 141 | + |
| 142 | +## Error Handling |
| 143 | + |
| 144 | +```python |
| 145 | +from memoria import ( |
| 146 | + MemoriaConnectionError, # network unreachable / timeout |
| 147 | + MemoriaAPIError, # base class for all HTTP error responses |
| 148 | + MemoriaAuthError, # 401 — invalid key or rate-limit exceeded |
| 149 | + MemoriaForbiddenError, # 403 — e.g. write to main in multi-member group mode |
| 150 | + MemoriaNotFoundError, # 404 |
| 151 | + MemoriaUnprocessableError,# 422 — server-side validation (empty content, bad type, etc.) |
| 152 | + MemoriaServerError, # 5xx |
| 153 | + MemoriaValidationError, # local validation (request not sent) |
| 154 | +) |
| 155 | + |
| 156 | +try: |
| 157 | + mem = client.memories.store(content="") |
| 158 | +except MemoriaUnprocessableError as e: |
| 159 | + print(f"Validation failed: {e.detail}") |
| 160 | +except MemoriaAuthError: |
| 161 | + print("Check your API key, or you may have hit the rate limit") |
| 162 | +``` |
| 163 | + |
| 164 | +`ping()` raises `MemoriaConnectionError` for network failures and `MemoriaAPIError` (or a |
| 165 | +subclass) for HTTP error responses — callers can distinguish the two: |
| 166 | + |
| 167 | +```python |
| 168 | +from memoria import MemoriaAPIError, MemoriaConnectionError |
| 169 | + |
| 170 | +try: |
| 171 | + client.ping() |
| 172 | +except MemoriaConnectionError: |
| 173 | + print("Cannot reach the server") |
| 174 | +except MemoriaAPIError as e: |
| 175 | + print(f"Server returned HTTP {e.status_code}: {e.detail}") |
| 176 | +``` |
| 177 | + |
| 178 | +## Compatibility Matrix |
| 179 | + |
| 180 | +| SDK version | Memoria API | Python | |
| 181 | +|-------------|-------------|--------| |
| 182 | +| 1.0.x | >= 0.2.3 | >= 3.10 | |
| 183 | + |
| 184 | +## Development |
| 185 | + |
| 186 | +```bash |
| 187 | +cd sdk/python |
| 188 | +pip install -e ".[dev]" |
| 189 | +pytest tests/unit/ -v # unit tests (no API needed) |
| 190 | +make python-sdk-test # full integration tests (needs make up) |
| 191 | +``` |
0 commit comments