Skip to content

Dicts

A Dict is a key-value store in an environment. Functions, Servers, Sandboxes, Python code and your Workers read and write the same entries, so it’s where they share small results and state.

Keys and values are JSON, so Python reads them with json.loads. An entry expires 7 days after its last write:

Results · Modal Dictget("last-run")
keyvalue
last-run{ ok: true }
a1
b2
Each entry is one JSON key and one JSON value. get reads one entry by its key.
src/Storage.ts
import * as Modal from "@alchemy.run/modal";
export const Results = Modal.Dict("Results", {});
const results = yield* Modal.ReadWriteDict(Results);

Provide Modal.ReadWriteDictHttp. Use ReadDict or WriteDict for one direction.

yield* results.put("last-run", { ok: true });
yield* results.update([["a", 1], ["b", 2]]);
const last = yield* results.get<{ ok: boolean }>("last-run");
const all = yield* results.entries();
const popped = yield* results.pop("a");
yield* results.clear();

pop returns the value it removed.