Developer docs / AI Fabric tutorials
Run Node.js / Ruby in a sandbox
- Sandbox
- Image DSL
Sometimes the code you want to run isn't Python, and sometimes you don't fully trust it, because it came from a user, an LLM, or a tool you're orchestrating. Sandboxes are AI Fabric's answer: each one is a throwaway, isolated container with its own filesystem and no access to the host. In this tutorial you'll run Node.js and Ruby snippets in separate sandboxes from a single Python app. It's the building block for multi-language agent tooling.
The shape is always the same: define an image with the runtime you need, bladekit.Sandbox.create(...) it, write your script in, exec it, and read the output back. No GPU and no heavy stack: the point is isolation.
Before you start
You need the AI Fabric CLI installed and authenticated: run blade setup once. Everything here is CPU-only. The script is self-contained: it ships its own Node and Ruby snippets, so blade run works as-is.
Define one image per language
Each sandbox needs a runtime. Build two thin images from debian_slim (one with Node.js, one with Ruby), installed straight from apt:
import bladekit
app = bladekit.app("sandbox-node-ruby")
node_image = (
bladekit.Image.debian_slim(python_version="3.12")
.apt_install("nodejs", "npm")
)
ruby_image = (
bladekit.Image.debian_slim(python_version="3.12")
.apt_install("ruby")
)Run a Node.js script in a sandbox
Inside an AI Fabric function, open a sandbox with a context manager so it's cleaned up automatically. Write the script to a path with sb.write_file, run it with sb.exec, wait for it, and read stdout/stderr/exit code back out:
@app.function(timeout=120)
def run_node(script: str) -> dict:
with bladekit.Sandbox.create(image=node_image, timeout=30) as sb:
sb.write_file("/work/script.js", script.encode())
proc = sb.exec("node", "/work/script.js")
proc.wait()
return {
"exit_code": proc.returncode,
"stdout": proc.stdout.read().decode(),
"stderr": proc.stderr.read().decode(),
}Run a Ruby script the same way
Ruby is identical: only the image and the interpreter command change. Any language that installs from apt drops straight into this pattern:
@app.function(timeout=120)
def run_ruby(script: str) -> dict:
with bladekit.Sandbox.create(image=ruby_image, timeout=30) as sb:
sb.write_file("/work/script.rb", script.encode())
proc = sb.exec("ruby", "/work/script.rb")
proc.wait()
return {
"exit_code": proc.returncode,
"stdout": proc.stdout.read().decode(),
"stderr": proc.stderr.read().decode(),
}Drive both from the entrypoint
The local_entrypoint sends a small script to each runtime and prints what comes back:
@app.local_entrypoint()
def main():
node_result = run_node.remote("console.log('Hello from Node!'); console.log(1 + 2);")
print("Node:", node_result["stdout"])
ruby_result = run_ruby.remote("puts 'Hello from Ruby!'; puts (1..5).sum")
print("Ruby:", ruby_result["stdout"])Run it
blade run sandbox_node_ruby.pyTake it for a spin
Everything above in one file, ready to run.
import bladekit
app = bladekit.app("sandbox-node-ruby")
node_image = (
bladekit.Image.debian_slim(python_version="3.12")
.apt_install("nodejs", "npm")
)
ruby_image = (
bladekit.Image.debian_slim(python_version="3.12")
.apt_install("ruby")
)
@app.function(timeout=120)
def run_node(script: str) -> dict:
with bladekit.Sandbox.create(image=node_image, timeout=30) as sb:
sb.write_file("/work/script.js", script.encode())
proc = sb.exec("node", "/work/script.js")
proc.wait()
return {
"exit_code": proc.returncode,
"stdout": proc.stdout.read().decode(),
"stderr": proc.stderr.read().decode(),
}
@app.function(timeout=120)
def run_ruby(script: str) -> dict:
with bladekit.Sandbox.create(image=ruby_image, timeout=30) as sb:
sb.write_file("/work/script.rb", script.encode())
proc = sb.exec("ruby", "/work/script.rb")
proc.wait()
return {
"exit_code": proc.returncode,
"stdout": proc.stdout.read().decode(),
"stderr": proc.stderr.read().decode(),
}
@app.local_entrypoint()
def main():
node_result = run_node.remote("console.log('Hello from Node!'); console.log(1 + 2);")
print("Node:", node_result["stdout"])
ruby_result = run_ruby.remote("puts 'Hello from Ruby!'; puts (1..5).sum")
print("Ruby:", ruby_result["stdout"])Concepts used: Cold start, Run