How it works
A sandbox is a single isolated compute environment. You create it, run commands inside it, and stop it when it is done. Each sandbox runs in its own container with its own filesystem, network, and process space. W&B authenticates your identity when you create and manage sandboxes. To use W&B or Weave inside a sandbox, pass your API key using the W&B Secrets Manager or environment variables. See Secrets for more information about using secrets in sandboxes. A sandbox goes through several states in its lifecycle. When a container is running, you can execute commands inside it. Read, write, and mount read-only files to and from the sandbox. Common examples include reading in a Python script to execute, writing out logs or results, or mounting a directory of data read-only for the sandbox to access. Use a session to manage multiple sandboxes that share configuration. When a session closes, all of its sandboxes are stopped automatically. See Manage multiple sandboxes for more details.Basic usage
Follow these steps to create a sandbox and run a command inside it:-
Install the W&B Python SDK (
wandb) and the serverless sandbox dependencies with the following command: -
Log in to W&B with the
wandb loginCLI command. When prompted, provide your API key to authenticate your identity and access your W&B account: -
Copy and paste the following code snippet into a Python file and run it. The code snippet accomplishes the following:
- Create a sandbox with
Sandbox.run(). - Run the command
echo "Hello from Serverless Sandboxes!"inside the sandbox using theSandbox.exec()method. - Print the output to the console using the
Processobject returned bySandbox.exec().
You should see the outputhello_sandbox.pyHello from Serverless Sandboxes!printed to the console. The sandbox automatically stops when the context manager (thewithblock) exits. For more information on sandbox lifecycle and states, see Lifecycle of sandboxes. - Create a sandbox with
Migrate to cwsandbox
wandb.sandbox is deprecated and will be removed in a future release of the W&B Python SDK. The cwsandbox package replaces it and accepts either a W&B API key or a CoreWeave API access token. Its reference documentation lives in the CoreWeave sandbox docs.
To migrate, change how you install the package, how you import it, and how you select your credential:
-
Install
cwsandboxwith the W&B extra. The extra pulls in thewandblibrary, whichcwsandboxuses to resolve W&B credentials: -
Import from
cwsandboxinstead ofwandb.sandbox, and passauth=AuthStrategy.WANDBwhen you create a sandbox or a session.cwsandboxreads a CoreWeave API access token fromCWSANDBOX_API_KEYby default, so W&B authentication must be selected explicitly:Before: wandb.sandboxAfter: cwsandbox
wandb login session, the WANDB_API_KEY environment variable, or the api.wandb.ai entry in ~/.netrc. To bill sandboxes to a CoreWeave account instead, omit auth and set CWSANDBOX_API_KEY to a CoreWeave API access token. For a comparison of the two credentials, see Choose a credential.
Behavior that changes after you migrate
wandb.sandbox wraps cwsandbox and applies W&B Serverless defaults that the cwsandbox package doesn’t apply on its own. Account for these differences when you migrate:
Serverless Sandboxes tutorials
For more in-depth examples, see:- Invoke an agent in a Serverless Sandbox tutorial
- Train a PyTorch model in a Serverless Sandbox tutorial