Skip to content

Share a session and model resources

Sharing a pipeline session, choosing node instance scope, and reusing a model in a worker solve different problems. Keep conversation and participant state separate from reusable model weights.

Scope What is shared What to keep separate
Shared FFI session One running graph addressed by a shared session key Participant ID, role, track and modality
Node instance_scope One node for the session, or one instance per participant Per-speaker accumulation and histories when using per_participant
Shared Python worker/model Compatible model resource and process Each attached session's node instance and mutable conversation state
Scaling room Runner-hosted session with admitted members Member identity and lifecycle under Entry

Join and leave a local session

Using the MS2 native profile:

python examples/python/shared_session.py

The complete example opens Alice and Bob with the same unique shared_session_key and manifest. Both receive attributed events from the same session. Alice leaves; Bob continues. After Bob leaves, a new join under the same key creates a new session ID. Every handle is closed in finally.

The native entry point accepts a manifest JSON string, shared session key, participant ID and role, with optional track ID, modality and display name. Read outputs with recv_event() to retain the participant field alongside kind and data. A receive strategy consumes queues: do not also drain those same outputs with recv_output() in another task.

Choose node instance scope

The manifest's node-level instance_scope can be shared or per_participant; a node can also declare a default. Choose per-participant state for a speech buffer or speaker-specific history. Choose shared state when participants intentionally interact through one conversation component. A participant handle alone does not make every node private.

Reuse a model safely

Python nodes can declare @shareable(key=("model", "device")) and acquire a resource with self.shared_resource(key, factory, on_switch=...). The sharing key should include every parameter that changes model compatibility. Voice choice or per-session history need not be part of a model key when the model supports those controls per call.

The returned handle owns access and release, and serializes model calls by default. Use the handle as a synchronous context manager on its own executor when model work would block the event loop. on_switch is where a stateful model can reset internal caches between sessions. Outside a shared worker, each handle owns a private resource.

These model-sharing semantics were checked with the captured SDK's 15 shared-resource tests and its real IPC shared-worker test: two sessions maintained independent counters, loaded one model, survived one session leaving, and shut down with exit code zero. This is separate evidence from the FFI participant example; it does not establish multi-machine model deduplication or device performance.

Scaling room ownership

The reviewed Scaling room host persists until Entry ends its partition. Do not apply the local FFI example's last-member teardown rule to a Scaling room. The deploy/join harness explicitly deletes the session and pipeline, then stops only its owned local servers.

Next

Cancellation controls partial output and pending inputs within a generator call; it does not mean every member left a shared session.