modal.Dict
class Dict(modal.object.Object)
Distributed dictionary for storage in Modal apps.
Dict contents can be essentially any object so long as they can be serialized by cloudpickle
. This includes other Modal objects. If writing and reading in different
environments (eg., writing locally and reading remotely), it’s necessary to have the
library defining the data type installed, with compatible versions, on both sides.
Additionally, cloudpickle serialization is not guaranteed to be deterministic, so it is
generally recommended to use primitive types for keys.
Lifetime of a Dict and its items
An individual Dict entry will expire after 7 days of inactivity (no reads or writes). The Dict entries are written to durable storage.
Legacy Dicts (created before 2025-05-20) will still have entries expire 30 days after being last added. Additionally, contents are stored in memory on the Modal server and could be lost due to unexpected server restarts. Eventually, these Dicts will be fully sunset.
Usage
from modal import Dict
my_dict = Dict.from_name("my-persisted_dict", create_if_missing=True)
my_dict["some key"] = "some value"
my_dict[123] = 456
assert my_dict["some key"] == "some value"
assert my_dict[123] == 456
The Dict
class offers a few methods for operations that are usually accomplished
in Python with operators, such as Dict.put
and Dict.contains
. The advantage of
these methods is that they can be safely called in an asynchronous context by using
the .aio
suffix on the method, whereas their operator-based analogues will always
run synchronously and block the event loop.
For more examples, see the guide.
hydrate
def hydrate(self, client: Optional[_Client] = None) -> Self:
Synchronize the local object with its identity on the Modal server.
It is rarely necessary to call this method explicitly, as most operations will lazily hydrate when needed. The main use case is when you need to access object metadata, such as its ID.
Added in v0.72.39: This method replaces the deprecated .resolve()
method.
objects
class objects(object)
Namespace with methods for managing named Dict objects.
create
@staticmethod
def create(
name: str, # Name to use for the new Dict
*,
allow_existing: bool = False, # If True, no-op when the Dict already exists
environment_name: Optional[str] = None, # Uses active environment if not specified
client: Optional[_Client] = None, # Optional client with Modal credentials
) -> None:
Create a new Dict object.
Examples:
modal.Dict.objects.create("my-dict")
Dicts will be created in the active environment, or another one can be specified:
modal.Dict.objects.create("my-dict", environment_name="dev")
By default, an error will be raised if the Dict already exists, but passing allow_existing=True
will make the creation attempt a no-op in this case.
modal.Dict.objects.create("my-dict", allow_existing=True)
Note that this method does not return a local instance of the Dict. You can use modal.Dict.from_name
to perform a lookup after creation.
Added in v1.1.2.
list
@staticmethod
def list(
*,
max_objects: Optional[int] = None, # Limit results to this size
created_before: Optional[Union[datetime, str]] = None, # Limit based on creation date
environment_name: str = "", # Uses active environment if not specified
client: Optional[_Client] = None, # Optional client with Modal credentials
) -> list["_Dict"]:
Return a list of hydrated Dict objects.
Examples:
dicts = modal.Dict.objects.list()
print([d.name for d in dicts])
Dicts will be retreived from the active environment, or another one can be specified:
dev_dicts = modal.Dict.objects.list(environment_name="dev")
By default, all named Dict are returned, newest to oldest. It’s also possible to limit the number of results and to filter by creation date:
dicts = modal.Dict.objects.list(max_objects=10, created_before="2025-01-01")
Added in v1.1.2.
delete
@staticmethod
def delete(
name: str, # Name of the Dict to delete
*,
allow_missing: bool = False, # If True, don't raise an error if the Dict doesn't exist
environment_name: Optional[str] = None, # Uses active environment if not specified
client: Optional[_Client] = None, # Optional client with Modal credentials
):
Delete a named Dict.
Warning: This deletes an entire Dict, not just a specific key. Deletion is irreversible and will affect any Apps currently using the Dict.
Examples:
await modal.Dict.objects.delete("my-dict")
Dicts will be deleted from the active environment, or another one can be specified:
await modal.Dict.objects.delete("my-dict", environment_name="dev")
Added in v1.1.2.
name
@property
def name(self) -> Optional[str]:
ephemeral
@classmethod
@contextmanager
def ephemeral(
cls: type["_Dict"],
data: Optional[dict] = None, # DEPRECATED
client: Optional[_Client] = None,
environment_name: Optional[str] = None,
) -> Iterator["_Dict"]:
Creates a new ephemeral Dict within a context manager:
Usage:
from modal import Dict
with Dict.ephemeral() as d:
d["foo"] = "bar"
async with Dict.ephemeral() as d:
await d.put.aio("foo", "bar")
from_name
@staticmethod
def from_name(
name: str,
*,
environment_name: Optional[str] = None,
create_if_missing: bool = False,
) -> "_Dict":
Reference a named Dict, creating if necessary.
This is a lazy method that defers hydrating the local object with metadata from Modal servers until the first time it is actually used.
d = modal.Dict.from_name("my-dict", create_if_missing=True)
d[123] = 456
info
@live_method
def info(self) -> DictInfo:
Return information about the Dict object.
clear
@live_method
def clear(self) -> None:
Remove all items from the Dict.
get
@live_method
def get(self, key: Any, default: Optional[Any] = None) -> Any:
Get the value associated with a key.
Returns default
if key does not exist.
contains
@live_method
def contains(self, key: Any) -> bool:
Return if a key is present.
len
@live_method
def len(self) -> int:
Return the length of the Dict.
Note: This is an expensive operation and will return at most 100,000.
update
@live_method
def update(self, other: Optional[Mapping] = None, /, **kwargs) -> None:
Update the Dict with additional items.
put
@live_method
def put(self, key: Any, value: Any, *, skip_if_exists: bool = False) -> bool:
Add a specific key-value pair to the Dict.
Returns True if the key-value pair was added and False if it wasn’t because the key already existed and skip_if_exists
was set.
pop
@live_method
def pop(self, key: Any) -> Any:
Remove a key from the Dict, returning the value if it exists.
keys
@live_method_gen
def keys(self) -> Iterator[Any]:
Return an iterator over the keys in this Dict.
Note that (unlike with Python dicts) the return value is a simple iterator, and results are unordered.
values
@live_method_gen
def values(self) -> Iterator[Any]:
Return an iterator over the values in this Dict.
Note that (unlike with Python dicts) the return value is a simple iterator, and results are unordered.
items
@live_method_gen
def items(self) -> Iterator[tuple[Any, Any]]:
Return an iterator over the (key, value) tuples in this Dict.
Note that (unlike with Python dicts) the return value is a simple iterator, and results are unordered.