Skip to content

stateless code sandbox#658

Merged
cosmicBboy merged 19 commits intomainfrom
add-sandbox
Feb 26, 2026
Merged

stateless code sandbox#658
cosmicBboy merged 19 commits intomainfrom
add-sandbox

Conversation

@samhita-alla
Copy link
Contributor

@samhita-alla samhita-alla commented Feb 12, 2026

Add flyte.sandbox.create() for containerized execution

This PR introduces a lightweight sandbox runtime for executing arbitrary code inside an ephemeral stateless container, along with improvements to file handling.

Sandbox execution API

flyte.sandbox.create() runs arbitrary Python code or shell commands inside a one-off Docker container.

The container is built on demand from the declared packages / system_packages, executed once, and discarded.

Supported execution modes

Auto-IO mode (default): minimal boilerplate

  • Triggered when code is provided and auto_io=True
  • Flyte auto-generates an argparse preamble
  • Declared inputs become local variables
  • Declared scalar outputs are written automatically to /var/outputs/
  • No manual I/O handling required
sandbox = flyte.sandbox.create(
    name="double",
    code="result = x * 2",
    inputs={"x": int},
    outputs={"result": int},
)

result = await sandbox.run.aio(x=21)  # -> 42

Verbatim mode: full script control

  • Triggered when code is provided and auto_io=False
  • Script runs exactly as written
  • No CLI args injected
  • Script handles all I/O manually via /var/inputs/ and /var/outputs/
sandbox = flyte.sandbox.create(
    name="etl",
    code="""
import json, pathlib

data = json.loads(pathlib.Path("/var/inputs/payload").read_text())
pathlib.Path("/var/outputs/total").write_text(
    str(sum(data["values"]))
)
""",
    inputs={"payload": File},
    outputs={"total": int},
    auto_io=False,
)

Command mode: run arbitrary commands

  • Triggered when command is provided
  • Executes any binary, shell pipeline, or test runner
sandbox = flyte.sandbox.create(
    name="test-runner",
    command=["/bin/bash", "-c", pytest_cmd],
    arguments=["_", "/var/inputs/solution.py", "/var/inputs/tests.py"],
    inputs={"solution.py": File, "tests.py": File},
    outputs={"exit_code": str},
)

Execution

Call .run() on the returned sandbox to build the image and execute.

Container improvements (_container.py)

  • Adds support for parsing File input from kwargs

Deterministic remote file paths (_file.py) - File.named_remote(name)

Creates a File reference whose remote path is derived deterministically from name. Unlike File.new_remote(), which generates a random path on each call, this method produces the same path for the same name within a task execution, making it safe across retries.

  • First attempt uploads to the path
  • Retries resolve to the identical location
  • Avoids duplicate uploads

If extraction fails, it falls back to the run base directory.

file = File.named_remote("report.csv")

async with file.open("wb") as f:
    await f.write(b"...")

New error type

InvalidPackageError: Introduced to signal invalid or unavailable packages during sandbox image build.

Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
@samhita-alla samhita-alla changed the title sandbox code sandbox Feb 18, 2026
samhita-alla and others added 8 commits February 24, 2026 10:35
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Resolved conflicts in convert.py and _container.py
Removed _sandbox.py file

Co-Authored-By: Warp <agent@warp.dev>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
@samhita-alla samhita-alla marked this pull request as ready for review February 24, 2026 08:15
@samhita-alla samhita-alla changed the title code sandbox stateless code sandbox Feb 24, 2026
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
@samhita-alla samhita-alla marked this pull request as draft February 24, 2026 12:45
@samhita-alla samhita-alla marked this pull request as ready for review February 24, 2026 16:02
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Signed-off-by: Samhita Alla <aallasamhita@gmail.com>
Copy link
Contributor

@cosmicBboy cosmicBboy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@cosmicBboy cosmicBboy merged commit e58cc5b into main Feb 26, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants