SessionIndex, created from
client.session(). For the full method
list, see the SDK reference.
Create or resume
client.session(index_name=...) returns a SessionIndex. If a cloud index with that name
already exists, it auto-loads into the session (no re-embedding); otherwise the session
starts empty. The workflow is identical in both cases, so you do not branch on whether the
index exists. Pass auto_load_on_init=False to skip that load and start empty, for example
to restore the session from disk.
Mutate and query locally
add_docs, delete_docs, and get_docs run in-memory; add_docs embeds locally via the
Rust core with no network round trip. query also runs entirely in-memory (~1-10 ms) and supports
the same metadata filter syntax as client.query().
Persist to the cloud
push_index() uploads the session’s documents to the cloud under the session’s name,
creating or replacing that index. For a built-in model the service re-embeds the text with
that model, so the pushed index answers text queries like one built with create_index. A
custom-model session keeps its own embeddings.
Extend an existing index
Becausesession() resumes by name, the same code extends an index across runs. Resume,
append new documents, then push back to overwrite the cloud copy with the combined set.
Save and restore on disk
save_to_disk(cache_path) writes the
session under <cache_path>/<name>/. A later session with the same name restores it with
load_from_disk(cache_path),
which returns the number of documents loaded. Open that session with
auto_load_on_init=False, so it skips the cloud load and the restore downloads no index.
(moss 1.16.0+)
- The snapshot replaces the session’s documents and model, so
session.model_idreports the snapshot’s model after a load. After a custom-model snapshot loads,add_docsandqueryneed embeddings. - A missing or damaged snapshot raises
MossErrorwith codeDESERIALIZATIONand leaves the session as it was. A failed or interrupted save leaves the last saved snapshot loadable. - A snapshot that an older core saved for a built-in model has no model artifact identity.
By default
load_from_diskrebuilds it for a session opened with the same model: it re-embeds the stored documents and saves the snapshot back in the same call, which needs the cloud for the model. If the model cannot be loaded, it raisesMossErrorwith codeMODEL_UNAVAILABLEand changes nothing. Passrebuild_legacy=Falseto load such a snapshot as a custom-model session. - A relative
cache_pathresolves against the current working directory, and~is not expanded.
Live-call context: short-term plus long-term
A session is short-term context - the working set for the current interaction. A persistent cloud index loaded withload_index()
is long-term context - durable knowledge shared across interactions. A typical live call
loads a cloud FAQ index for long-term context and opens a session for short-term context,
then queries both for each turn.
Behavior notes
- The session’s embedding model is set by
model_idonsession()(default"moss-minilm"; also"moss-mediumlm"or"custom"). When resuming an existing cloud index, omitmodel_idto adopt the stored model - passing a different one raises aValueError. All participants resuming the same index must use the same model. session.model_idis the model the session uses, or"custom". Loading a cloud index or a disk snapshot moves the session to the loaded model. (moss 1.16.0+)- With
model_id="custom", each document must carry its own.embeddingand every query must passQueryOptions.embedding. See Custom embeddings. - Project credentials are validated when the session is opened;
session()raises if they are invalid. close()sends the final usage report and frees the session’s memory, andasync with await client.session(...) as session:calls it for you.client.close()also closes every session the client opened. Requiresmoss>= 1.15.0. See Releasing resources.
Related
SessionIndex reference
Every session method and property.
Metadata filtering
Filter inside a session query.