Skip to main content
A session is a local, in-process index you read and write in real time, with no cloud round trip on any operation. Sessions are how the Python SDK does indexing during a live interaction: indexing transcript turns mid-call, building a per-user working set, or accumulating context that is handed off between agents. A session is a 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

Because session() 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_id reports the snapshot’s model after a load. After a custom-model snapshot loads, add_docs and query need embeddings.
  • A missing or damaged snapshot raises MossError with code DESERIALIZATION and 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_disk rebuilds 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 raises MossError with code MODEL_UNAVAILABLE and changes nothing. Pass rebuild_legacy=False to load such a snapshot as a custom-model session.
  • A relative cache_path resolves 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 with load_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_id on session() (default "moss-minilm"; also "moss-mediumlm" or "custom"). When resuming an existing cloud index, omit model_id to adopt the stored model - passing a different one raises a ValueError. All participants resuming the same index must use the same model.
  • session.model_id is 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 .embedding and every query must pass QueryOptions.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, and async with await client.session(...) as session: calls it for you. client.close() also closes every session the client opened. Requires moss >= 1.15.0. See Releasing resources.

SessionIndex reference

Every session method and property.

Metadata filtering

Filter inside a session query.