Skip to content

Get files in and out of a sandbox

There is no file API. No sb.files.write(), no upload endpoint, no download URL. The sandbox has exactly one door — exec — and every file you move goes through it or through the sandbox’s own network connection.

This page shows the patterns that work today. Read the size limits first: they decide which pattern you need.

Direction Practical limit What happens past it
Writing a file into the sandbox a few hundred KB per run() The request body grows; splitting into chunks gets slow
Reading via sb.run() / a job 32 MiB per stream Cut, with truncated: true
Either, at any size unlimited Let the sandbox fetch/push it over the network

A quoted heredoc. <<'EOF' with the marker in quotes stops the shell expanding $variables and backticks inside your content, which matters when the content is code an LLM wrote.

from sistemo import Sandbox
SCRIPT = """
import json, sys
print(json.dumps({"argv": sys.argv[1:]}))
"""
with Sandbox(stack="python") as sb:
sb.run(f"cat > /tmp/main.py <<'SISTEMO_EOF'\n{SCRIPT}\nSISTEMO_EOF")
print(sb.run("python3 /tmp/main.py a b").stdout)

Pattern 2: base64 for anything you did not write

Section titled “Pattern 2: base64 for anything you did not write”

The only safe way to move arbitrary bytes through a shell. It costs ~33% more bytes and it never breaks on quotes, newlines, markers or binary.

import base64
from sistemo import Sandbox
data = open("model.bin", "rb").read() # any bytes at all
b64 = base64.b64encode(data).decode()
with Sandbox() as sb:
sb.run(f"echo '{b64}' | base64 -d > /tmp/model.bin")
print(sb.run("ls -l /tmp/model.bin").stdout)

base64 -w0 matters: without it base64 wraps at 76 columns and you have to strip the newlines before decoding.

Pattern 3: let the sandbox move it over the network

Section titled “Pattern 3: let the sandbox move it over the network”

For anything above roughly a megabyte, do not push bytes through exec at all. Sandboxes have outbound internet access (that is how pip install works), so the fastest path in and out is the sandbox talking to your own storage directly.

from sistemo import Sandbox
with Sandbox(stack="python") as sb:
# In: the sandbox pulls it.
sb.run("curl -fsSL -o /tmp/input.csv 'https://example.com/presigned-get-url'", timeout=120)
sb.run("python3 -c \"import csv;print(sum(1 for _ in open('/tmp/input.csv')))\"")
# Out: the sandbox pushes it.
sb.run("curl -fsS -T /tmp/result.parquet 'https://example.com/presigned-put-url'", timeout=120)

Use presigned URLs with a short expiry. A long-lived credential passed into a sandbox is a credential you have handed to the code you did not trust enough to run on your own machine — the whole reason you are here.

Pattern 4: keep the files instead of moving them

Section titled “Pattern 4: keep the files instead of moving them”

If the same data is needed by run after run, stop copying it. Attach a volume once and it survives the sandbox being destroyed and recreated.

A volume appears in the guest as the next drive — /dev/vdb for the first data disk — and nothing mounts it for you. Every run that wants it mounts it first:

A fresh Sandbox() cannot take a volume — attach needs a stopped machine. Create the machine over the REST API with "volumes": ["<id>"] instead: it is placed on the volume’s host and boots with the disk attached. Persist data with volumes walks through it, and Volumes has the endpoints.

Mount, write and flush in one command:

Terminal window
mkdir -p /mnt/data && mount /dev/vdb /mnt/data && echo 'run 1' >> /mnt/data/log.txt && sync

⚠ Chain them with &&. As two separate commands, a failed mount is followed by a write that succeeds — onto the machine’s own root disk under /mnt/data, which is thrown away with the machine. Nothing errors, and the data is gone.

⚠ End with && sync. Deleting a machine flushes its disk first, but best-effort — a wedged or out-of-memory guest cannot answer, and an unflushed write is then lost silently. Delete the machine, create a new one with the same volumes, mount again — log.txt is still there.

Your situation Use
Writing a script you control, < 100 KB Heredoc (pattern 1)
Writing anything an LLM produced Base64 (pattern 2)
Reading a result you know is small cat, after checking stat -c %s
Anything ≥ 1 MB, either direction Presigned URL (pattern 3)
The same data on every run A volume (pattern 4)