Skip to main content

Python SDK

The official Python client for SharedMemory.

pip install sharedmemory-ai

Requires Python 3.8+.

Authenticate with Authorization: Bearer sm_…. Prefer sm_org_rw_… / sm_proj_rw_… from Settings → API Keys, or sm_agent_…, sm_live_… (sm login).

Quick Start

from sharedmemory import SharedMemory

memory = SharedMemory(
api_key="sm_org_rw_...",
volume_id="your-project-id",
)

# Store a memory
result = memory.add("User prefers dark mode and compact layout")

# Query memories
results = memory.query("user preferences")
for source in results["sources"]:
print(source["content"], source["score"])

Configuration

OptionTypeDefaultDescription
api_keystrRequired. Bearer secret (sm_org_rw_…, sm_proj_rw_…, sm_agent_…, etc.)
base_urlstrhttps://api.sharedmemory.aiAPI endpoint
volume_idstr"default"Default project (volume) for all operations
user_idstrScope memories to a specific user
agent_idstrScope memories to a specific agent
agent_namestr"python-sdk"Agent name for attribution
timeoutfloat30.0Request timeout in seconds
app_idstrApp identifier for scoping
session_idstrDefault session ID

Methods

Memory

MethodDescription
remember(content, **opts)Store a memory
query(query, **opts)Query memories (semantic search)
chat(query, **opts)Ask a question — LLM answers using your memories
get(memory_id)Get a single memory by ID
update(memory_id, content)Update a memory
delete(memory_id)Delete a memory
remember_many(items)Store multiple memories in batch
delete_many(memory_ids)Delete multiple memories by ID
update_many(updates)Update multiple memories in batch
feedback(memory_id, feedback, reason?)Submit feedback on a memory
history(memory_id)Get memory audit trail

Sessions

MethodDescription
start_session(session_id, **opts)Start a conversation session
end_session(session_id, auto_summarize?)End session with optional summarization
get_session(session_id)Get session details
list_sessions(**opts)List sessions with optional filters

Webhooks

MethodDescription
webhook_subscribe(url, events, secret?)Register a webhook endpoint
webhook_unsubscribe(url)Remove a webhook subscription

Export / Import

MethodDescription
export_memories(**opts)Export memories from a volume
import_memories(memories, **opts)Import memories into a volume

Extraction

MethodDescription
extract(text, schema_id, **opts)Extract structured data from text

Graph

MethodDescription
get_entity(name)Get entity details and relationships
search_entities(query, **opts)Search entities in the knowledge graph by name
get_graph()Retrieve the full knowledge graph
list_volumes()List all volumes accessible by this API key

Context

MethodDescription
get_context(template_id?)Get optimized context for LLM prompting

Agent Management

MethodDescription
create_agent(org_id, project_id, name, **opts)Create a new agent profile
list_agents(org_id, project_id?)List agents in an organization
get_agent(agent_id)Get agent details
update_agent(agent_id, **opts)Update agent settings
delete_agent(agent_id)Delete an agent
rotate_agent_key(agent_id)Rotate an agent's API key

Organization Management

MethodDescription
list_orgs()List organizations you belong to
get_org(org_id)Get organization details
list_org_members(org_id)List members of an organization
apply_promo(org_id, code)Apply a promo code

Entity Scoping

Scope memories to specific users, agents, or sessions:

memory = SharedMemory(
api_key="sm_org_rw_...",
volume_id="vol-uuid",
user_id="user-123",
agent_id="chatbot-1",
)

# All operations are automatically scoped
memory.remember("User asked about pricing", session_id="sess-abc")
results = memory.query("pricing", session_id="sess-abc")
# With reranking
results = memory.query(
"project deadlines",
rerank=True,
rerank_method="llm",
include_context=True,
)

# With metadata filters
results = memory.query("preferences", filters={
"AND": [
{"field": "memory_class", "op": "eq", "value": "preference"},
{"field": "score", "op": "gte", "value": 0.5},
]
})

Batch Operations

# Delete multiple memories
memory.delete_many(["mem-id-1", "mem-id-2", "mem-id-3"])

# Update multiple memories (each item must have memory_id AND content)
memory.update_many([
{"memory_id": "mem-id-1", "content": "Updated content"},
{"memory_id": "mem-id-2", "content": "Reviewed and updated"},
])

Webhooks

# Subscribe to memory events
memory.webhook_subscribe(
url="https://my-server.com/webhook",
events=["memory.approved", "memory.flagged"],
secret="my-signing-secret", # optional HMAC-SHA256
)

# Unsubscribe
memory.webhook_unsubscribe(url="https://my-server.com/webhook")

Deliveries are retried up to 3 times with exponential backoff. Webhooks are auto-disabled after 10 consecutive failures.

Export / Import

# Export all memories
data = memory.export_memories()
print(len(data)) # list of memory dicts

# Import memories
memory.import_memories([
{"content": "Alice is VP of Engineering", "memory_type": "factual"},
{"content": "Bob manages frontend", "memory_type": "factual"},
])

Structured Extraction

extracted = memory.extract(
"John Smith is the CTO of Acme Corp",
schema_id="schema-uuid",
)

Agent Management

# Create an agent
agent = memory.create_agent("org-id", "project-id", "support-bot", description="CS assistant")

# List agents
agents = memory.list_agents("org-id")

# Rotate an agent key
new_key = memory.rotate_agent_key(agent["id"])

Organization Management

orgs = memory.list_orgs()
members = memory.list_org_members(orgs[0]["id"])
memory.apply_promo(orgs[0]["id"], "PROMO-CODE")

Async Client

For async/await usage:

from sharedmemory.async_client import AsyncSharedMemory

memory = AsyncSharedMemory(
api_key="sm_org_rw_...",
volume_id="your-project-id",
)

result = await memory.add("User prefers dark mode")
results = await memory.search("preferences")

PyPI

https://pypi.org/project/sharedmemory-ai/

License

MIT