Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions sandboxes/create-sandbox.mdx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
---
title: Create sandboxes
description: Learn how to create Serverless Sandboxes.
keywords: ["Sandbox.run", "SandboxDefaults", "Session"]
---

<Warning>
Serverless Sandboxes is in public preview.
</Warning>

Create sandboxes with W&B. Each sandbox runs in its own container with its own [filesystem](/sandboxes/file-access), network, and process space.
Create W&B Serverless Sandboxes to run code in isolated environments. Start a single sandbox, run commands in it, and manage multiple sandboxes with a session. Each sandbox runs in its own container, with its own [file system](/sandboxes/file-access), network, and process space.

<Info>
By default, sandboxes use `python:3.11` as the base image. To use a different image, pass `container_image` to [`Sandbox.run()`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/sandbox#run) or [`SandboxDefaults`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/sandbox-defaults). W&B supports public container images only.

The following code snippet creates a sandbox with the `python:3.15` image and runs `python --version` inside it.
The following code snippet creates a sandbox with the `python:3.15` image and runs `python --version` inside it.

```python
from wandb.sandbox import Sandbox
Expand All @@ -27,7 +28,7 @@ with Sandbox.run(container_image="python:3.15") as sandbox:
Use [`Sandbox.run()`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/sandbox#run) to create and start a sandbox. This method returns a `Sandbox` object that you can use to interact with the environment.

<Tip>
W&B recommends using a context manager (`with` statement) to ensure that the sandbox is stopped automatically when it exits the block, even if an error occurs.
W&B recommends using a context manager (`with` statement) to stop the sandbox automatically when the block exits, even if an error occurs.
</Tip>

The following example creates a sandbox with the default container image (`python:3.11`):
Expand Down Expand Up @@ -56,7 +57,7 @@ This pattern is useful for interactive and multi-step workflows. To learn how to

### Start a sandbox with a main command

You can also pass a command to `Sandbox.run()`. Use this pattern when the sandbox is meant to run a single job from start to finish. When the main process exits, the sandbox enters a terminal state such as [COMPLETED or FAILED](/sandboxes/lifecycle).
You can also pass a command to `Sandbox.run()`. Use this pattern when the sandbox runs a single job from start to finish. When the main process exits, the sandbox enters a terminal state such as [COMPLETED or FAILED](/sandboxes/lifecycle).

The command you provide to `Sandbox.run()` starts as the sandbox's main process.

Expand All @@ -77,16 +78,17 @@ sandbox = Sandbox.run("python", "train.py")
sandbox.wait_until_complete().result()
```


## Create multiple sandboxes with a session

Use [`Session`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/session) to create and manage multiple sandboxes. When the session closes (for example, when exiting a `with` block), all sandboxes created by that session are stopped automatically.
When you need more than one sandbox at a time, or want to share configuration across several sandboxes, use a session.

Use [`Session`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/session) to create and manage multiple sandboxes. When the session closes (for example, when you exit a `with` block), it automatically stops all sandboxes it created.

You can optionally pass a [`SandboxDefaults`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/sandbox-defaults) object to a session to define reusable default configuration for all sandboxes created by that session. For example, you can specify a default container image, network configuration, or maximum lifetime for all sandboxes in the session.

`session.sandbox()` returns an unstarted sandbox. It auto-starts when you call `Sandbox.exec()`, `Sandbox.read_file()`, or any other operation.

The following code snippet creates a session that creates two sandboxes that use a default configuration (`SandboxDefaults`):
The following code snippet creates a session with two sandboxes that share a default configuration (`SandboxDefaults`):

```python
from wandb.sandbox import Session, SandboxDefaults
Expand Down
5 changes: 3 additions & 2 deletions sandboxes/file-access.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: File operations
description: Learn how to read, write, and mount files in Serverless Sandboxes.
keywords: ["Sandbox.write_file", "Sandbox.read_file", "file mount", "upload to sandbox"]
---

<Warning>
Expand All @@ -25,7 +26,7 @@ Read and write files when you want to transfer smaller files between your local
{/* creating, updating, modifying, or saving data */}
{/* transfers a file into the sandbox. Use it to upload inputs your sandbox code needs (scripts, configs, data files). */}

Transfer a file from your local environment to the sandbox using the [`Sandbox.write_file()`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/sandbox#write_file) method.
Write a file to the sandbox when you need to upload inputs your sandbox code uses, such as scripts, configs, or data files. Transfer a file from your local environment to the sandbox using the [`Sandbox.write_file()`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/sandbox#write_file) method.


```python
Expand Down Expand Up @@ -70,7 +71,7 @@ Use mounted files to provide local files to the sandbox at creation time. Unlike
Mounted files are read-only in the sandbox. If you need to modify files in the sandbox, use `Sandbox.write_file()` instead.
</Info>

In the following code snippet, local files `train.py` and `requirements.txt` are mounted to the sandbox root directory. The sandbox installs dependencies from `requirements.txt` and then runs `train.py`.
The following code snippet mounts the local files `train.py` and `requirements.txt` to the sandbox root directory. The sandbox installs dependencies from `requirements.txt` and then runs `train.py`.

```python
from pathlib import Path
Expand Down
19 changes: 12 additions & 7 deletions sandboxes/invoke-agent-sandbox-tutorial.mdx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
title: "Tutorial: Invoke an agent in a sandbox"
description: "Learn how to invoke OpenAI agent within a Serverless Sandbox environment"
description: "Learn how to invoke an OpenAI agent within a Serverless Sandbox environment"
keywords: ["tutorial", "invoke agent in sandbox"]
---

<Warning>
Serverless Sandboxes is in public preview.
</Warning>

In this tutorial, you will invoke an agent within a Serverless Sandbox environment. To do this, you will start a sandbox with the appropriate environment variables, install the necessary dependencies, and run a Python script that creates and invokes a simple OpenAI agent that uses tool calls to get the weather for a location and respond with a punny weather forecast.
In this tutorial, you invoke an agent in a W&B Serverless Sandbox. You start a sandbox with the required environment variables, install dependencies, and run a Python script that creates and invokes a simple OpenAI agent. The agent uses tool calls to get weather for a location and return a punny forecast.

<Note>
This tutorial uses OpenAI as the language model for the agent, which requires an OpenAI API key.
Expand All @@ -17,6 +18,8 @@ This tutorial uses OpenAI as the language model for the agent, which requires an

## Prerequisites

Before you begin, install the W&B Python SDK, authenticate with W&B, and store your OpenAI API key as a secret so the sandbox can use it.

{/* ### (Recommended) Create a Python virtual environment

Create and activate a Python virtual environment to keep your dependencies organized and avoid conflicts with other projects. Run the following commands in your terminal:
Expand All @@ -28,9 +31,9 @@ source venv/bin/activate # On Windows, use `venv\Scripts\activate`

### Install W&B Python SDK

Install the W&B Python SDK. You can do this using `pip`:
Install the W&B Python SDK using `pip`:

```bash
```bash
pip install wandb
```

Expand All @@ -44,15 +47,17 @@ wandb login

### Store API keys in Secret Manager

Store your OpenAI API key in the [W&B Secret Manager](/platform/secrets) as `OPENAI_API_KEY`. See [Secrets](/sandboxes/secrets) for more information about using secrets in sandboxes.
Store your OpenAI API key in the [W&B Secret Manager](/platform/secrets) as `OPENAI_API_KEY`. For more information about using secrets in sandboxes, see [Secrets](/sandboxes/secrets).

This allows you to securely access the API key in the sandbox without hardcoding it in your code or sandbox configuration.
This lets you securely access the API key in the sandbox without hardcoding it in your code or sandbox configuration.

## Copy agent code

With your prerequisites in place, save the agent code locally so you can run it inside a sandbox. The script defines an OpenAI agent with two tools and a structured response format.

<Accordion title="OpenAI agent code">

Copy and paste the following code into a file named `demo.py` in the same directory as this tutorial, then run the above code snippet to see how to invoke an OpenAI agent within a sandbox environment.
Copy the following code into a file named `demo.py` in the same directory as this tutorial. Then, run the previous code snippet to invoke an OpenAI agent in a sandbox.

```python title="demo.py"
import json
Expand Down
37 changes: 20 additions & 17 deletions sandboxes/lifecycle.mdx
Original file line number Diff line number Diff line change
@@ -1,53 +1,56 @@
---
title: Sandbox lifecycle
description: Learn about the lifecycle of a Serverless Sandbox, including its states, how to wait for state changes, and how to stop a sandbox.
keywords: ["SandboxState", "wait_until_complete", "wait_until_ready", "sandbox timeout", "resource cleanup"]
---

<Warning>
Serverless Sandboxes is in public preview.
</Warning>

A Serverless Sandbox goes through several states during its lifecycle. The sandbox's state determines which operations are available.
This page describes the lifecycle of a Serverless Sandbox so that you can choose the right method for waiting on state changes, stopping a sandbox, and structuring code around its lifetime. Understanding the lifecycle helps you avoid race conditions, distinguish startup failures from runtime errors, and clean up resources predictably.

In most cases, a sandbox starts in `PENDING`, moves to `CREATING` while the container is provisioned, and then enters `RUNNING` when it is ready for use.
A W&B Serverless Sandbox goes through several states during its lifecycle. The sandbox state determines which operations are available.

If you start a sandbox with a main command, that command becomes the sandbox's main process. When the main process exits, the sandbox enters a terminal state such as `COMPLETED` (exit code `0`) or `FAILED` (an error during startup or execution). A sandbox can also enter `TERMINATED` if it is stopped externally or exceeds its maximum lifetime.
Usually, a sandbox starts in `PENDING`, moves to `CREATING` while the container is provisioned, and then enters `RUNNING` when it's ready for use.

If you start a sandbox with a main command, that command becomes the sandbox's main process. When the main process exits, the sandbox enters a terminal state such as `COMPLETED` (exit code `0`) or `FAILED` (an error during startup or execution). A sandbox can also enter `TERMINATED` if it's stopped externally or exceeds its maximum lifetime.

```text
PENDING -> CREATING -> RUNNING -> COMPLETED
-> FAILED
-> TERMINATED
```

The next sections describe sandbox states, how to wait for readiness or completion, and how to stop a sandbox. For more information about creating sandboxes and running commands, see [Create sandboxes](/sandboxes/create-sandbox) and [Run commands](/sandboxes/run-commands).
The following sections describe sandbox states, how to wait for readiness or completion, and how to stop a sandbox. For more information about creating sandboxes and running commands, see [Create sandboxes](/sandboxes/create-sandbox) and [Run commands](/sandboxes/run-commands).

## Sandbox states

The following table summarizes the states of a sandbox:

| State | Description |
|-------------|-------------------------------------|
| PENDING | The sandbox request was accepted and is waiting to be scheduled. |
| CREATING | The container is being provisioned. |
| RUNNING | The sandbox is ready for operations. |
| COMPLETED | The main process exited successfully with code 0. |
| FAILED | The sandbox or its main process encountered an error during startup or execution. |
| TERMINATED | The sandbox was stopped externally or ended because it exceeded its maximum lifetime. |
| `PENDING` | The sandbox request was accepted and is waiting to be scheduled. |
| `CREATING` | The container is provisioning. |
| `RUNNING` | The sandbox is ready for operations. |
| `COMPLETED` | The main process exited successfully with code `0`. |
| `FAILED` | The sandbox or its main process encountered an error during startup or execution. |
| `TERMINATED` | An external process stopped the sandbox, or it ended because it exceeded its maximum lifetime. |

Most operations require the sandbox to be in the RUNNING state. However, many operations automatically wait until the sandbox is ready before proceeding.
Most operations require the sandbox to be in the `RUNNING` state. However, many operations wait until the sandbox is ready before proceeding.

After a sandbox enters a terminal state, it is no longer available for additional work.
After a sandbox enters a terminal state, it's no longer available for additional work.

## Wait for readiness or completion

Use different wait methods depending on what you need:
The following sections describe the two methods for waiting on sandbox state changes and when to use each. Use different wait methods depending on what you need:

* Use `Sandbox.wait()` to [wait until the sandbox is ready for use](/sandboxes/lifecycle#wait-for-a-sandbox-to-start).
* Use `Sandbox.wait_until_complete()` to [wait until the sandbox's main process finishes](/sandboxes/lifecycle#wait-for-a-sandbox-to-complete).

### Wait for a sandbox to start

Use `Sandbox.wait()` to explicitly wait for the sandbox to reach the RUNNING state. This is useful for debugging startup problems or when you want to distinguish startup failures from errors in later commands.
Use `Sandbox.wait()` to explicitly wait for the sandbox to reach the `RUNNING` state. This method is useful for debugging startup problems or when you want to distinguish startup failures from errors in later commands.

For example, the following code creates a sandbox, waits for it to become ready, and then runs a command:

Expand Down Expand Up @@ -75,7 +78,7 @@ print(f"Exit code: {sandbox.returncode}")

This pattern is useful when you start a sandbox with a main command such as `Sandbox.run("python", "train.py")` and want the sandbox lifecycle to match that command's execution.

When the main process exits, the sandbox enters a terminal state. A successful run typically ends in COMPLETED. If the process fails, the sandbox may enter FAILED.
When the main process exits, the sandbox enters a terminal state. A successful run typically ends in `COMPLETED`. If the process fails, the sandbox may enter `FAILED`.

If you need to run commands interactively instead, use a context manager with `Sandbox.exec()`:

Expand All @@ -92,7 +95,7 @@ with Sandbox.run() as sandbox:

Use methods such as `Sandbox.wait()` and `Sandbox.stop()` when you need explicit control over the sandbox lifecycle.

In most cases, you do not need to call these methods directly. Operations such as `Sandbox.exec()`, `Sandbox.read_file()`, and `Sandbox.write_file()` automatically wait for readiness, and the context manager (`with Sandbox.run() as sandbox:`) automatically stops the sandbox when the block exits.
Usually, you don't need to call these methods directly. Operations such as `Sandbox.exec()`, `Sandbox.read_file()`, and `Sandbox.write_file()` wait for readiness, and the context manager (`with Sandbox.run() as sandbox:`) stops the sandbox when the block exits.

### Stop a sandbox

Expand All @@ -116,6 +119,6 @@ from wandb.sandbox import Sandbox
with Sandbox.run("sleep", "infinity") as sandbox:
sandbox.stop(
graceful_shutdown_seconds=30.0, # Wait before force-killing the sandbox.
missing_ok=True, # Do not raise an error if it is already stopped.
missing_ok=True, # Don't raise an error if it's already stopped.
).result()
```
Loading
Loading