Dict is a distributed dictionary for storage in Modal apps. Dict contents can be essentially any object that can be serialized by cloudpickle, including other Modal objects.
Key concepts
Serialization: If writing and reading in different environments (e.g., writing locally and reading remotely), you must have the library defining the data type installed, with compatible versions, on both sides. Key recommendations: Cloudpickle serialization is not guaranteed to be deterministic, so it’s generally recommended to use primitive types for keys. Entry lifetime: An individual Dict entry will expire after 7 days of inactivity (no reads or writes). Dict entries are written to durable storage.Legacy Dicts (created before 2025-05-20) have entries that expire 30 days after being last added, and contents are stored in memory on Modal servers. These legacy Dicts will eventually be sunset.
Basic usage
Creating dicts
Reference by name
Create or reference a named dict that persists across runs:Ephemeral dicts
Create a temporary dict that exists only within a context manager:Create from ID
Reference a dict by its object ID:Dictionary operations
Get and set values
Check if key exists
Update multiple values
Put with conditional creation
Remove values
Get dict length
The
len() operation is expensive and will return at most 100,000.Iterating over entries
Iterate over keys, values, or items:Unlike Python dicts, these return simple iterators and results are unordered.
Async usage
TheDict class offers methods with .aio suffix for async contexts. These are safe to call without blocking the event loop:
Managing dicts
TheDict.objects namespace provides methods for managing named dicts.
Create a dict
List dicts
List all dicts in the active environment:Delete a dict
API reference
Dict methods
Get the value associated with a key. Returns
default if key does not exist.Parameters:key(Any): The key to look updefault(Any, optional): Value to return if key is not found
Add a specific key-value pair to the Dict.Parameters:
key(Any): The keyvalue(Any): The valueskip_if_exists(bool): If True, only set if key doesn’t exist. Default: False
Check if a key is present in the Dict.Parameters:
key(Any): The key to check
Update the Dict with additional items.Parameters:
other(Mapping, optional): Mapping to update from**kwargs: Additional key-value pairs
Remove a key from the Dict, returning the value if it exists.Parameters:
key(Any): The key to removedefault(Any, optional): Value to return if key not found. If not provided, raises KeyError.
Remove all items from the Dict.
Return the number of items in the Dict. This is an expensive operation and will return at most 100,000.Returns: int
Return an iterator over the keys in this Dict. Results are unordered.Returns: AsyncIterator[Any]
Return an iterator over the values in this Dict. Results are unordered.Returns: AsyncIterator[Any]
Return an iterator over the (key, value) tuples in this Dict. Results are unordered.Returns: AsyncIterator[tuple[Any, Any]]
Return information about the Dict object. Returns a
DictInfo dataclass with name, created_at, and created_by fields.Returns: DictInfoError handling
Request size errors
Deserialization errors
Best practices
- Use primitive types for keys (strings, integers) to ensure deterministic serialization
- Set expiration expectations: Remember that entries expire after 7 days of inactivity
- Use
.aiomethods in async contexts to avoid blocking the event loop - Handle missing keys: Use
get()with defaults orcontains()to check existence - Batch updates: Use
update()for multiple key-value pairs instead of individualput()calls