LangChain and LangGraph

Use the published Python package for explicit memory calls, retriever documents, or application-defined LangGraph nodes.

#Choose the LangChain adapter

Install langchain-memorae from PyPI when your Python application already uses LangChain. MeMoraeMemory gives you explicit recall and save operations; MeMoraeRetriever returns LangChain Document objects. LangGraph uses those same operations inside nodes you define. None of these adapters replaces your application transcript or runs automatically in the background.

#Install and configure securely

Install the published 0.1.2 release with `python -m pip install langchain-memorae==0.1.2` (https://pypi.org/project/langchain-memorae/0.1.2/). Create a project API key in Dashboard → API Keys and copy its personal Subject ID from Projects → Connection details. On your backend set MEMORAE_API_KEY, MEMORAE_BASE_URL=https://memora-singapore.onrender.com, and MEMORAE_SUBJECT_ID. The package does not automatically load .env files; export the variables or load them in your application. For another namespace, set MEMORAE_NAMESPACE consistently for save and recall.

python
import os
from langchain_memorae import MeMoraeMemory

# Install first: python -m pip install langchain-memorae
# Set MEMORAE_API_KEY, MEMORAE_SUBJECT_ID and
# MEMORAE_BASE_URL=https://memora-singapore.onrender.com
memory = MeMoraeMemory(
    api_key=os.environ["MEMORAE_API_KEY"],
    base_url=os.environ["MEMORAE_BASE_URL"],
    subject_id=os.environ["MEMORAE_SUBJECT_ID"],
    namespace=os.getenv("MEMORAE_NAMESPACE", "default"),
)
memory.save_context({"input": "I like mangoes."}, {"output": "Got it."})
# Search after extraction and indexing:
print(memory.load_memory_variables({"input": "What fruit do I like?"})["history"])

#Recall before generation, save after

Call load_memory_variables with the current question before generating an answer. Pass the returned history to your prompt as supporting context. After the answer, call save_context with the turn. By default, only the user message is submitted as a source; set save_assistant=True only when assistant output should also be stored.

python
context = memory.load_memory_variables({"input": question})["history"]
answer = await answer_model(question, memory_context=context)
memory.save_context({"input": question}, {"output": answer})

#Use MeMoraeRetriever

Use MeMoraeRetriever where a LangChain retriever is expected. invoke(query) returns Document objects with memory_id and source_ids metadata. Preserve source metadata when downstream code cites or audits recalled content. Empty results are valid and should not interrupt the conversation.

python
from langchain_memorae import MeMoraeRetriever

retriever = MeMoraeRetriever(
    api_key=os.environ["MEMORAE_API_KEY"],
    base_url=os.environ["MEMORAE_BASE_URL"],
    subject_id=os.environ["MEMORAE_SUBJECT_ID"],
    namespace=os.getenv("MEMORAE_NAMESPACE", "default"),
)
documents = retriever.invoke(question)

#Use the adapter in LangGraph

Add a recall node before the model node and a save node after it. Keep the authenticated application user’s stable subject ID available to the memory instance or node. This is an explicit graph pattern, not an automatic callback.

python
def recall(state):
    history = memory.load_memory_variables(
        {"input": state["input"]}
    )["history"]
    return {"history": history}

def save(state):
    memory.save_context(
        {"input": state["input"]},
        {"output": state["output"]},
    )
    return {}

#Production behavior and troubleshooting

Ingestion and indexing are asynchronous. A successful save request means the source was accepted; extraction, activation, and search-index projection still need to complete before retrieval can find it. Keep normal chat behavior when history is empty, apply bounded retries to transient network errors, and inspect HTTP status without logging credentials or message contents. A 401 usually means an invalid or revoked project key; a 503 means retrieval or a required index service is unavailable. Keep one stable Subject ID per authenticated end user, use the same namespace for save and search, and never share one personal identity across all customers.