Python SDK
Install the SDK with pip install rsibox and sign in via the quickstart. RsiboxClient delegates to the installed CLI and shares its login, API key, and selected project.
To run Harbor tasks, see Run tasks with Harbor.
from rsibox import RsiboxAPIError, RsiboxClient
client = RsiboxClient(timeout_s=30)timeout_s sets the default API request timeout. Long-polling calls extend it by wait_s, while exec(..., timeout_s=...) limits guest command runtime and file methods accept their own transfer timeout_s.
Create, wait, execute, and clean up
Section titled “Create, wait, execute, and clean up”Save this as job.py and run RSIBOX_PROJECT=default python job.py. It creates a CPU sandbox, uploads an input file, runs a command, downloads the verified result, and stops the sandbox in finally.
import sysimport timeimport uuidfrom pathlib import Path
from rsibox import RsiboxClient
spec = { "runtime": "sandbox-vm", "cpu": {"vcpus": 2, "memory_mb": 4096}, "disk_mb": 20480, "network": "none", "life": "10m",}create_key = uuid.uuid4().hex # Persist this before submitting in a durable job.
with RsiboxClient(timeout_s=30) as client: box = client.create_sandbox(spec, name="python-job", idempotency_key=create_key) sandbox_id = box["id"] try: deadline = time.monotonic() + 300 while box["state"] != "running": if box["state"] in {"failed", "terminated", "terminating", "powered_off"}: raise RuntimeError(f"Sandbox did not start: {box['state']}") remaining = deadline - time.monotonic() if remaining <= 0: raise TimeoutError("Sandbox startup exceeded the local wait budget") box = client.wait_sandbox( sandbox_id, while_state=box["state"], wait_s=min(25, remaining) )
client.put_file( sandbox_id, "tmp/input.txt", b"hello from rsibox\n", 0o600, timeout_s=60 ) exec_id = client.exec( sandbox_id, ["/bin/sh", "-c", "cp /tmp/input.txt /tmp/result.txt && cat /tmp/result.txt"], timeout_s=60, idempotency_key=uuid.uuid4().hex, ) stdout_since = stderr_since = 0 deadline = time.monotonic() + 90 while True: remaining = deadline - time.monotonic() if remaining <= 0: raise TimeoutError("Execution output exceeded the local wait budget") page = client.exec_output( exec_id, stdout_since=stdout_since, stderr_since=stderr_since, wait_s=min(25, remaining), ) for segment in page["stdout"]["segments"]: print(segment["data"], end="", flush=True) for segment in page["stderr"]["segments"]: print(segment["data"], end="", file=sys.stderr, flush=True) stdout_since = page["stdout"]["next_since"] stderr_since = page["stderr"]["next_since"] if page["output_complete"]: if page["exec"]["exit_code"] != 0: raise RuntimeError(f"Command failed: {page['exec']['exit_code']}") break
data = client.get_file(sandbox_id, "tmp/result.txt", timeout_s=60) Path("result.txt").write_bytes(data) finally: client.stop_sandbox(sandbox_id)Key behaviors to keep in mind:
- Explicit waiting: Unlike the CLI,
create_sandboxreturns right after admission and does not wait forrunning. Eachwait_sandboxcall waits for a single state change (up towait_s), so loop on the latest state with an overall deadline. - Streaming output:
execreturns anexec_id, not command output. Pollexec_output, advance bothstdout_sinceandstderr_sincecursors, and stop only whenoutput_completeis true—a terminal execution state alone does not guarantee all output has arrived. Passcmdas an argument list; invoke a shell explicitly when needed. - Explicit cleanup: The
with RsiboxClient(...)context manager only releases client resources; it does not stop sandboxes. Keepstop_sandboxinfinallyand retainsandbox_idso you can retry if stopping fails.
Supported operations
Section titled “Supported operations”| Method | Purpose |
|---|---|
create_sandbox(spec, *, name=None, idempotency_key=None) |
Create a CPU or GPU sandbox and return its record |
get_sandbox(sandbox_id) |
Inspect current sandbox state |
wait_sandbox(sandbox_id, while_state, wait_s=25) |
Wait for a state transition, up to 25 seconds per call |
get_sandbox_gpu_status(sandbox_id) |
Read live per-device GPU status and readings |
get_sandbox_events(sandbox_id) |
Read lifecycle event history, oldest first |
stop_sandbox(sandbox_id) |
Request shutdown and release resources |
exec(sandbox_id, cmd, ..., timeout_s=None, idempotency_key=None) |
Submit a command; supports optional cwd, env, and user |
exec_output(exec_id, *, stdout_since=0, stderr_since=0, wait_s=0) |
Read stdout, stderr, and completion status |
put_file(sandbox_id, path, data, mode=None, *, timeout_s=None) |
Upload bytes with an optional integer file mode |
get_file(sandbox_id, path, *, timeout_s=None) |
Download verified file bytes into memory |
set_sandbox_network(sandbox_id, network, wait_s=0, allowed_hosts=None) |
Change egress; wait_s > 0 waits for node confirmation |
File paths are relative to the guest root (tmp/result.txt, not /tmp/result.txt). get_file verifies size, SHA-256, and transfer completion before returning, and holds the file in memory—use CLI downloads for large files. File methods have no default transfer timeout, so pass timeout_s when you need a bound.
To request GPUs, add "accelerator": {"model": "rtx5080", "count": 1} to spec using the short catalog name.
Change network access
Section titled “Change network access”For a running sandbox:
client.set_sandbox_network(sandbox_id, "none", wait_s=25, allowed_hosts=[])client.set_sandbox_network(sandbox_id, "gateway", wait_s=25, allowed_hosts=[])Passing wait_s > 0 waits for confirmation that the policy was applied; the default wait_s=0 only submits the request. A 504 means confirmation timed out—do not assume the policy is active. With gateway, an empty allowed_hosts list allows public internet egress; pass public DNS names to restrict it.
Retry writes with the same key
Section titled “Retry writes with the same key”Use one durable idempotency_key per intended create_sandbox or exec call. When retrying, keep the project, key, and request identical—reusing a key with a different request is rejected, and generating a new key starts a new operation.
# These values belong to one job attempt; save them before the first request.request_key = "training-job-104-create"request_spec = {"cpu": {"vcpus": 2, "memory_mb": 4096}, "life": "10m"}
# If the outcome is uncertain, repeat this same call, then inspect or stop its ID.box = client.create_sandbox(request_spec, idempotency_key=request_key)If you omit idempotency_key, the SDK generates one. On transport failures (TimeoutError, urllib.error.URLError), HTTP 5xx errors, or malformed JSON replies, the exception carries error.idempotency_key so you can safely retry. Always inspect RsiboxAPIError (status, code, detail, request_id) rather than retrying permanent errors automatically, and preserve request_id (from X-Request-Id) when reporting failed requests.
A local timeout does not mean the operation failed. For interrupted creates, the gateway resubmits a pending reservation only within its 10-minute admission window; after that, replaying the key recovers an accepted sandbox or returns 409 create_failed. Once the backend no longer retains the record, replay returns 410. See compute retry rules.