Python SDK for MemoryGraph - the graph-based memory layer for AI agents.
pip install memorygraphsdkWith framework integrations:
# LlamaIndex integration
pip install memorygraphsdk[llamaindex]
# LangChain integration
pip install memorygraphsdk[langchain]
# CrewAI integration
pip install memorygraphsdk[crewai]
# AutoGen integration
pip install memorygraphsdk[autogen]
# All integrations
pip install memorygraphsdk[all]from memorygraphsdk import MemoryGraphClient
# Initialize client (api_key can also be set via MEMORYGRAPH_API_KEY env var)
client = MemoryGraphClient(api_key="mgraph_your_key_here")
# Create a memory
memory = client.create_memory(
type="solution",
title="Fixed Redis timeout issue",
content="Used exponential backoff with max 5 retries. Key was setting proper timeout values.",
tags=["redis", "timeout", "solution"],
importance=0.8
)
print(f"Created memory: {memory.id}")
# Search memories
results = client.search_memories(query="redis timeout", limit=10)
for mem in results:
print(f"- {mem.title}: {mem.content[:100]}...")
# Create relationships
client.create_relationship(
from_memory_id=solution_id,
to_memory_id=problem_id,
relationship_type="SOLVES"
)
# Get related memories
related = client.get_related_memories(
memory_id=problem_id,
relationship_types=["SOLVES"]
)from memorygraphsdk import AsyncMemoryGraphClient
async with AsyncMemoryGraphClient(api_key="mgraph_...") as client:
memory = await client.create_memory(
type="solution",
title="Async solution",
content="..."
)
memories = await client.search_memories(query="async")from memorygraphsdk.integrations.llamaindex import MemoryGraphChatMemory
from llama_index.core.chat_engine import SimpleChatEngine
memory = MemoryGraphChatMemory(api_key="mgraph_...")
chat_engine = SimpleChatEngine.from_defaults(memory=memory)
response = chat_engine.chat("I'm working on a Redis timeout issue")from memorygraphsdk.integrations.langchain import MemoryGraphMemory
from langchain.chains import ConversationChain
memory = MemoryGraphMemory(api_key="mgraph_...")
chain = ConversationChain(memory=memory, llm=llm)
response = chain.run("What Redis issues have we seen?")from memorygraphsdk.integrations.crewai import MemoryGraphCrewMemory
from crewai import Agent
memory = MemoryGraphCrewMemory(api_key="mgraph_...")
agent = Agent(
role="Developer",
goal="Solve technical problems",
memory=memory
)| Method | Description |
|---|---|
create_memory(...) |
Create a new memory |
get_memory(id) |
Get memory by ID |
update_memory(id, ...) |
Update a memory |
delete_memory(id) |
Delete a memory |
search_memories(...) |
Search for memories |
recall_memories(query) |
Natural language recall |
create_relationship(...) |
Create relationship |
get_related_memories(id) |
Get related memories |
task- Tasks and todossolution- Solutions to problemsproblem- Problems encounterederror- Errors and exceptionsfix- Bug fixescode_pattern- Code patternsworkflow- Workflows and processesconversation- Chat/conversation messages (used by integrations)general- General information
SOLVES- Solution → ProblemCAUSES- Cause → EffectTRIGGERS- Trigger → EventREQUIRES- Dependent → DependencyRELATED_TO- General association
Environment variables:
export MEMORYGRAPH_API_KEY="mgraph_..."
export MEMORYGRAPH_API_URL="https://api.memorygraph.dev" # optionalOr in code:
client = MemoryGraphClient(
api_key="mgraph_...",
api_url="https://api.memorygraph.dev",
timeout=30.0
)from memorygraphsdk import (
MemoryGraphClient,
AuthenticationError,
RateLimitError,
NotFoundError
)
try:
memory = client.get_memory("invalid-id")
except AuthenticationError:
print("Invalid API key")
except NotFoundError:
print("Memory not found")
except RateLimitError:
print("Rate limit exceeded, please retry")The SDK models are kept in sync with the core memorygraph models. Key types:
task, code_pattern, problem, solution, project, technology, error, fix, command, file_context, workflow, general, conversation
All relationship types from core are available in SDK, organized into categories:
- Causal: CAUSES, TRIGGERS, LEADS_TO, PREVENTS, BREAKS
- Solution: SOLVES, ADDRESSES, ALTERNATIVE_TO, IMPROVES, REPLACES
- Context: OCCURS_IN, APPLIES_TO, WORKS_WITH, REQUIRES, USED_IN
- Learning: BUILDS_ON, CONTRADICTS, CONFIRMS, GENERALIZES, SPECIALIZES
- Similarity: SIMILAR_TO, VARIANT_OF, RELATED_TO, ANALOGY_TO, OPPOSITE_OF
- Workflow: FOLLOWS, DEPENDS_ON, ENABLES, BLOCKS, PARALLEL_TO
- Quality: EFFECTIVE_FOR, INEFFECTIVE_FOR, PREFERRED_OVER, DEPRECATED_BY, VALIDATED_BY
The SDK Relationship model includes optional bi-temporal tracking fields:
valid_from: When the relationship became validvalid_until: When the relationship expiresrecorded_at: When the relationship was recordedinvalidated_by: ID of memory that invalidated this relationship
See tests/test_model_sync.py for automated sync verification.
- API Reference - Complete API documentation
- LlamaIndex Integration Guide - Using MemoryGraph with LlamaIndex
- LangChain Integration Guide - Using MemoryGraph with LangChain
- CrewAI Integration Guide - Using MemoryGraph with CrewAI
- AutoGen Integration Guide - Using MemoryGraph with AutoGen
- Examples - Working code examples
- Synchronous and Async - Full async/await support for high-performance applications
- Type-Safe - Complete type hints and Pydantic models
- Framework Integrations - Native support for LlamaIndex, LangChain, CrewAI, and AutoGen
- Graph-Based - Relationships between memories for complex reasoning
- Search & Recall - Powerful search with natural language queries
- Error Handling - Comprehensive exception handling with specific error types
Sign up at memorygraph.dev to get your API key.
- GitHub Issues: https://github.com/gregorydickson/claude-code-memory/issues
- Documentation: https://memorygraph.dev/docs
- Email: support@memorygraph.dev
Apache 2.0