Skip to content

strands.storage.in_memory_storage

In-memory storage implementation.

class InMemoryStorage()

Defined in: src/strands/storage/in_memory_storage.py:18

Map-backed storage for testing and short-lived processes.

Content does not survive process restarts. The store is unbounded — consumers manage eviction themselves.

Example:

from strands.storage import InMemoryStorage
storage = InMemoryStorage()
await storage.write("sessions/abc/state.json", b'\{"messages": []}')
data = await storage.read("sessions/abc/state.json")
def __init__(
*,
search_strategy: SearchStrategy[InMemoryStorage] | None = None
) -> None

Defined in: src/strands/storage/in_memory_storage.py:36

Initialize an empty in-memory store.

Arguments:

  • search_strategy - Optional search strategy. When set, write() automatically indexes entries and search() delegates to the strategy instead of the default keyword scan.
async def write(key: str, data: bytes) -> None

Defined in: src/strands/storage/in_memory_storage.py:48

Store data under key, overwriting any existing value.

Arguments:

  • key - Opaque string key identifying the value.
  • data - Raw bytes to persist.

Raises:

  • StorageError - If the key is invalid.
async def read(key: str) -> bytes | None

Defined in: src/strands/storage/in_memory_storage.py:68

Retrieve the bytes previously stored under key.

Arguments:

  • key - The key to read.

Returns:

The stored bytes, or None if no value exists for key.

Raises:

  • StorageError - If the key is invalid.
async def delete(key: str) -> None

Defined in: src/strands/storage/in_memory_storage.py:85

Delete the value stored under key. A no-op if the key does not exist.

Arguments:

  • key - The key to delete.

Raises:

  • StorageError - If the key is invalid.
async def list(query: str = "") -> builtins.list[str]

Defined in: src/strands/storage/in_memory_storage.py:98

List keys matching the given prefix.

Arguments:

  • query - A prefix string to filter keys. Empty string matches all.

Returns:

Matching keys sorted ascending.

Raises:

  • StorageError - If the prefix is invalid.
async def search(query: str) -> builtins.list[StorageSearchResult]

Defined in: src/strands/storage/in_memory_storage.py:115

Search stored content using the configured strategy.

Delegates to the search strategy when one is set, otherwise falls back to keyword token-overlap scoring.

Arguments:

  • query - Natural-language search query.

Returns:

All matches with relevance scores, ranked best-first.

def namespace(prefix: str) -> _NamespacedStorage

Defined in: src/strands/storage/in_memory_storage.py:131

Return a view of this storage with all keys prefixed.

Arguments:

  • prefix - Prefix to prepend to all keys.

Returns:

A namespaced storage view.

def clear() -> None

Defined in: src/strands/storage/in_memory_storage.py:142

Remove all stored entries.