MemoryClient API reference
MemoryClient connects to the configured backend and exposes its memory stores. The concrete stores and supported operations differ between bolt and NAMS.
Signatures below show types, keyword-only arguments (*), and defaults. They are reference declarations; the examples show calls to execute.
See Backend capabilities for availability and data scope.
Overview
Export the connection values for a dedicated AuraDB instance using the Aura connection setup. BoltSettings selects the direct Neo4j backend for Aura.
import asyncio
import os
from neo4j_agent_memory import BoltSettings, MemoryClient
async def main():
settings = BoltSettings(
neo4j={
"uri": os.environ["NEO4J_URI"],
"username": os.environ["NEO4J_USERNAME"],
"password": os.environ["NEO4J_PASSWORD"],
"database": os.getenv("NEO4J_DATABASE", "neo4j"),
},
)
async with MemoryClient(settings) as client:
message = await client.short_term.add_message("example", "user", "Hello")
print(message.id)
asyncio.run(main())
For hosted setup use NamsSettings and the returned conversation ID, as shown in NAMS quickstart. connect(settings) is also available as an async construction helper: it returns an already-connected BoltMemoryClient or NamsMemoryClient for the corresponding settings type. Close that client in finally. The SDK aliases improve static typing; they do not change the service.
Constructor
def MemoryClient(
settings: MemorySettings | None=None,
*,
embedder: Embedder | None=None,
extractor: EntityExtractor | None=None,
resolver: EntityResolver | None=None,
geocoder: Geocoder | None=None,
enrichment_provider: _EnrichmentProviderProtocol | None=None,
ontology: OntologyDocument | str | Path | None=None,
) -> None: ...
| Parameter | Description |
|---|---|
|
Memory settings (uses defaults if not provided) |
|
Optional embedder override (for testing) |
|
Optional extractor override (for testing) |
|
Optional resolver override (for testing). Without it, bolt builds the resolver from |
|
Optional geocoder override (for testing) |
|
Optional enrichment provider override (for testing) |
|
The ontology this client extracts and validates against: an |
Ontology precedence
On bolt, connect() resolves one ontology document before it builds the extractor, and hands it to the extractor factory, the resolver, and the short-term and long-term stores. The first source that yields a document wins:
-
MemoryClient(ontology=…) -
settings.schema_config.ontology_path -
settings.schema_config.custom_schema_path -
the active stored ontology version, when
settings.schema_config.use_active_ontologyis True and a version is activated -
settings.schema_config.modelset tocustomwithsettings.schema_config.entity_types(an ad-hoc document, one label per name) -
the built-in template named by
settings.schema_config.ontology_template(defaultpoleo), falling back to the built-in POLE+O ontology when the name is unknown
The validation mode is settings.schema_config.validation_mode when set, then the active stored version’s mode when that version supplied the document, then strict if strict_types is True, otherwise permissive.
Because resolution happens at connect time, activating a version affects the next connection, not the client that activated it. Read the result back with the ontology_document and validation_mode properties. See Graph schema configuration for the settings.
Properties
| Property | Type / backend | Description |
|---|---|---|
|
|
Conversation operations; connection required. |
|
|
Entity operations; preferences and facts are bolt-only. |
|
|
Reasoning operations; NAMS assembles conversation-level steps into compatibility traces. |
|
|
Resolved |
|
|
Whether NAMS is selected. |
|
|
Whether the backend is connected. |
|
|
Read-only |
|
|
Deprecated direct graph access; see the |
|
Graph |
Constraints/index inspection and management; see Schema objects. |
|
|
User nodes and explicit user-scoped operations; see Manage user records. |
|
|
Explicit buffered writes; see Buffered writes. |
|
|
Conversation hygiene and audit operations; see Consolidation. |
|
|
Retrieval evaluation; the operations evaluated must be supported by the selected backend. |
|
|
Versioned domain ontologies on both backends: |
|
|
The effective ontology resolved at connect time, which drives extraction, relation validation, and the write paths. |
|
|
How strictly the write paths enforce |
|
|
Key management; see Authentication. |
|
|
Errors collected by explicit buffered writes on bolt; always an empty list on NAMS. |
Store access before connection raises NotConnectedError. A property being present is not evidence of backend support. On NAMS, the bolt-only users, buffered, consolidation and schema accessors return a placeholder whose method calls raise NotSupportedError, and graph raises NotSupportedError on access. Unsupported store methods that NAMS defines raise NotSupportedError; bolt-only methods it does not define, for example long_term.get_preferences_by_category, raise AttributeError. The NAMS placeholder long_term.get_preferences_for takes keyword arguments only, so the bolt positional form get_preferences_for(user) raises TypeError instead. The NAMS long_term.supersede_preference takes one preference_id plus keyword arguments, so the bolt two-argument call raises TypeError whether it is written positionally or with the old_preference_id/new_preference_id keywords. The NAMS long_term.get_facts_about takes one positional entity_name and no keyword arguments, so a bolt call that passes subject= or limit= raises TypeError. Keep the MemorySettings object you passed to the constructor if you need to inspect configuration; there is no public client.settings property.
Methods
connect
Connect to the configured backend. Called by async with; bolt initializes schema and providers, while NAMS configures HTTP transport and optionally validates the connection.
async def connect() -> None: ...
close
Close backend resources. Called when leaving async with, including on exceptions.
async def close() -> None: ...
graph
On bolt, client.graph exposes the existing connection for domain-specific Cypher. execute_read emits a deprecation warning; prefer client.query.cypher for read-only queries. Direct write access remains bolt-only.
rows = await client.query.cypher(
"MATCH (c:Customer {id: $id}) RETURN c.name AS name",
{"id": "CUST-001"},
)
# Bolt only: writes require the direct graph client.
await client.graph.execute_write(
"CREATE (n:MyNode {name: $name})", {"name": "example"}
)
Neo4jClient also provides execute_batch and vector_search. Use graph operations only while the client is connected. NAMS does not expose direct bolt access or write Cypher.
get_context
Return a formatted string assembled from the enabled stores. There is no MemoryContext object or to_prompt() conversion.
async def get_context(
query: str,
*,
session_id: str | None=None,
include_short_term: bool=True,
include_long_term: bool=True,
include_reasoning: bool=True,
max_items: int=10,
) -> str: ...
| Parameter | Description |
|---|---|
|
The query to search for relevant context |
|
Optional session ID for short-term filtering |
|
Whether to include conversation history |
|
Whether to include matching entities and preferences on bolt (facts are not included). On NAMS, long-term context is an empty string. |
|
Whether to include similar task traces |
|
Maximum items per memory type |
session_id is passed to short-term context retrieval, which also appends global semantic matches on bolt when an embedder is configured. It does not scope long-term or reasoning retrieval. This method is not an authorization boundary. On NAMS, a conversation ID is required when short-term context is enabled; the server-provided short-term context does not use query to filter its contents.
context = await client.get_context(
"restaurant recommendations", session_id="example", max_items=5
)
prompt = f"Context from memory:\n{context}\nUser question: Suggest a restaurant."
get_stats
Bolt only. Return a flat dictionary of counts; NAMS raises NotSupportedError. Use a supported read-only counting query when hosted statistics are needed.
async def get_stats() -> dict[str, Any]: ...
stats = await client.get_stats()
print(stats["messages"], stats["entities"], stats["traces"])
The count keys are conversations, messages, entities, preferences, facts, and traces.
get_graph
Bolt only. Return a MemoryGraph for visualization, containing nodes, relationships, and metadata. The limit applies per memory type.
async def get_graph(
*,
memory_types: list[Literal['short_term', 'long_term', 'reasoning']] | None=None,
session_id: str | None=None,
since: datetime | None=None,
until: datetime | None=None,
include_embeddings: bool=False,
limit: int=1000,
) -> MemoryGraph: ...
| Parameter | Description |
|---|---|
|
Which memory types to include. Defaults to all. |
|
Filter by session ID (for short_term and reasoning) |
|
Only include data created/updated after this time |
|
Only include data created/updated before this time |
|
Whether to include embedding vectors in properties. Set to False (default) for smaller payloads. |
|
Maximum number of nodes to return per memory type |
export_graph is not a Python SDK method. For NAMS, use long_term.expand_graph or a supported read-only Cypher query.
get_locations
Bolt only; on NAMS it raises NotSupportedError. Return location dictionaries with coordinates, enrichment details, and conversation references for map rendering.
async def get_locations(
*,
session_id: str | None=None,
has_coordinates: bool=True,
limit: int=500,
) -> list[dict[str, Any]]: ...
| Parameter | Description |
|---|---|
|
Filter to locations mentioned in this conversation. When provided, only returns locations that have an |
|
Only return locations with lat/lon coordinates. Defaults to True for map visualization use cases. |
|
Maximum number of locations to return. Defaults to 500. |
Context manager and concurrency
Use async with MemoryClient(settings) to connect and close reliably. Async calls may share one client within the same event loop; create separate clients for separate threads/event loops. Flush submitted buffered work before consuming its results and inspect write_errors as described in the buffered-write guide.
Graph models
GraphNode
Fields and defaults:
| Field | Type | Default | Description |
|---|---|---|---|
|
|
|
Node identifier |
|
|
|
Node labels (e.g., ['Message'], ['Entity']) |
|
|
|
Node properties |
GraphRelationship
Fields and defaults:
| Field | Type | Default | Description |
|---|---|---|---|
|
|
|
Relationship identifier |
|
|
|
Relationship type (e.g., 'HAS_MESSAGE', 'MENTIONS') |
|
|
|
Source node ID |
|
|
|
Target node ID |
|
|
|
Relationship properties |