- Creating multiple sandboxes with shared configuration
- Executing Python functions in sandboxes
- You want automatic cleanup of orphaned sandboxes
Properties
sandbox_count
Methods
log_metrics
step(int | None): Training step to associate with metrics. If provided, metrics are logged at this step number in wandb.reset(bool): If True (default), reset accumulated metrics after a successful log. Metrics are preserved if log() fails (no active wandb run). Set to False to keep accumulating regardless.
bool: True if metrics were logged, False if no reporter configuredbool: or no active wandb run.
get_metrics
dict[str, Any]: Dictionary with cwsandbox/* prefixed metric names and values.dict[str, Any]: Empty dict if no reporter is configured.
close
OperationRef[None]: OperationRef[None]: Use .result() to block until all sandboxes stopped.
SandboxError: If one or more running sandboxes failed to stop.
sandbox
command(str | None): Command to run in sandboxargs(list[str] | None): Arguments for the commandcontainer_image(str | None): Container image to usetags(list[str] | None): Tags for the sandbox (merged with session defaults, including whentemplate_idis set, solist()/adoptcan find the sandbox). Environment variables and annotations do not merge on the template path (template-owned spec).profile_ids: Removed in 1.x; passing a value raisesTypeError.profile_names: Removed in 1.x; passing a value raisesTypeError.runner_ids(list[str] | None): Optional CKS runner pin (incompatible with serverless and withplacement_spillover='serverless_then_cks')resources(ResourceOptions | dict[str, Any] | None): Resource configuration. Accepts ResourceOptions for separate requests/limits, or a flat dict for backward-compatible Guaranteed QoS.mounted_files(list[dict[str, Any]] | None): Files to mount into the sandbox at startup. Each dict should havemount_path(str) andfile_content(bytes). Note: Mounted files are read-only at runtime. To modify a file, usesandbox.write_file()after the sandbox is running.s3_mount: Removed in 1.x; passing a value raisesTypeError.ports: Removed in 1.x; useservices=[Service(...)]instead.network(NetworkOptions | dict[str, Any] | None):NetworkOptions(or dict) with deny flags and optional create-time hostname grants. Port exposure usesservices=.file_system_snapshot(FileSystemSnapshotOptions | dict[str, Any] | None): Convenience single-mount FSS options (FileSystemSnapshotOptionsor dict). Prefervolumes=for multi-volume setups.max_timeout_seconds: Removed in 1.x; userequest_timeout_seconds.request_timeout_seconds(float | None): Client-side HTTP timeout for sandbox RPCs. Defaults to the session’sSandboxDefaults.request_timeout_seconds.data_plane_mode(DataPlaneMode | str | None): Override the session’s data-plane transport policy.auth(AuthConfig | None): Authentication strategy, resolved headers, or provider. Overrides the session default for this sandbox.environment_variables(dict[str, str] | None): Environment variables to inject into the sandbox. Merges with and overrides matching keys from the session defaults. Use for non-sensitive config only.annotations(dict[str, str] | None): Kubernetes pod annotations for the sandbox. Merges with and overrides matching keys from the session defaults. Use for non-sensitive metadata only.secrets(Sequence[Secret | dict[str, Any]] | None): Secrets to inject as environment variables. Merged with session defaults (defaults first, then this list).containers(Sequence[Container | Mapping[str, Any]] | None): Multi-container spec. Mutually exclusive with single-container kwargs. Replaces sessiondefaults.secrets,environment_variables,security_context, andworking_dir; put those on eachContainer. Not used by@session.function().
Sandbox: An unstarted Sandbox registered with the session.
SandboxError: If the session has been closed.
list
show_terminated=True to widen the search to include terminal
sandboxes (completed, failed, terminated).
A terminal status filter (e.g. status="completed") also widens
the search automatically.
Parameters
tags(list[str] | None): Additional tags to filter by (merged with session’s default tags)status(str | None): Filter by statusprofile_ids(list[str] | None): Removed in 1.x; passing a value raisesTypeError.profile_names(list[str] | None): Removed in 1.x; passing a value raisesTypeError.runner_ids(list[str] | None): Filter by runner IDs (defaults to session’s runner_ids if set)volume_ids(list[str] | tuple[str, ...] | None): Filter to sandboxes attached to these registered Volume IDsshow_terminated(bool): If True, include terminal sandboxes (completed, failed, terminated). Defaults to False.adopt(bool): If True, register discovered sandboxes with this session so they are stopped when the session closes
OperationRef[list[Sandbox]]: OperationRef[list[Sandbox]]: Use .result() to block for results,OperationRef[list[Sandbox]]: or await directly in async contexts.
from_id
sandbox_id(str): The ID of the existing sandboxadopt(bool): If True (default), register the sandbox with this session
OperationRef[Sandbox]: OperationRef[Sandbox]: Use .result() to block for the Sandbox instance,OperationRef[Sandbox]: or await directly in async contexts.
adopt
sandbox(Sandbox): A Sandbox instance to track
SandboxError: If the session is closedValueError: If the sandbox has no sandbox_id
function
container_image(str | None): Override session’s default image for this functiontemp_dir(str | None): Override temp directory for payload/result files in sandbox. Defaults to session default. Created if missing.profile_ids: Removed in 1.x; passing a value raisesTypeError.profile_names: Removed in 1.x; passing a value raisesTypeError.runner_ids(list[str] | None): Optional CKS runner pin (incompatible with serverless and withplacement_spillover='serverless_then_cks')resources(ResourceOptions | dict[str, Any] | None): Resource configuration. Accepts ResourceOptions for separate requests/limits, or a flat dict for backward-compatible Guaranteed QoS.mounted_files(Sequence[dict[str, Any]] | None): Files to mount into the sandbox at startup. Each dict should havemount_path(str) andfile_content(bytes). Note: Mounted files are read-only at runtime. To modify a file, usesandbox.write_file()after the sandbox is running.s3_mount: Removed in 1.x; passing a value raisesTypeError.ports: Removed in 1.x; useservices=[Service(...)]instead.network(NetworkOptions | dict[str, Any] | None):NetworkOptions(or dict) with deny flags and optional create-time hostname grants. Port exposure usesservices=.file_system_snapshot(FileSystemSnapshotOptions | dict[str, Any] | None): Convenience single-mount FSS options (FileSystemSnapshotOptionsor dict). Prefervolumes=for multi-volume setups.max_timeout_seconds: Removed in 1.x; userequest_timeout_seconds.request_timeout_seconds(float | None): Client-side HTTP timeout for sandbox RPCs. Defaults to the session’sSandboxDefaults.request_timeout_seconds.data_plane_mode(DataPlaneMode | str | None): Override the session’s data-plane transport policy.environment_variables(dict[str, str] | None): Environment variables to inject into the sandbox. Merges with and overrides matching keys from the session defaults. Use for non-sensitive config only.annotations(dict[str, str] | None): Kubernetes pod annotations for the sandbox. Merges with and overrides matching keys from the session defaults. Use for non-sensitive metadata only.
Callable[[Callable[P, R]], RemoteFunction[P, R]]: A decorator that wraps a function as a RemoteFunction