Function supports defining custom sandboxes that can be used to reconstruct your Python environment before compiling your function.

Sandboxes are very much experimental, and will likely see major changes, additions, and revisions in the near future.

Defining a Sandbox

To compile in a sandbox, create one and provide it to the @compile decorator:

predictor.py
from fxn import compile, Sandbox
from PIL import Image

@compile(
    tag="@dream-co/generate-image",
    sandbox=Sandbox()
)
def generate_image (prompt: str) -> Image:
    ...

Installing Packages

Sandboxes support installing both Python and Debian packages in the compiler sandbox:

Installing Python Packages

Use the Sandbox.pip_install method to install Python packages from the PyPi registry:

predictor.py
from fxn import compile, Sandbox

# Install numpy and sklearn
sandbox = (Sandbox()
  .pip_install("numpy", "scikit-learn")
)

# Compile your function with the sandbox
@compile(..., sandbox=sandbox)
def predict () -> np.ndarray:
    ...

Installing Debian Packages

Use the Sandbox.apt_install method to install Debian system packages:

predictor.py
from fxn import compile, Sandbox

# Install git and wget
sandbox = (Sandbox()
  .apt_install("git", "wget")
)

# Compile your function with the sandbox
@compile(..., sandbox=sandbox)
def predict () -> BytesIO:
    ...

Uploading Files

You can upload files and directories to your sandbox, useful for prediction functions that require resources like model weights.

Uploading a File

Use the Sandbox.upload_file method to upload a file to a path in the sandbox:

predictor.py
from fxn import compile, Sandbox

# Upload a model weight to the sandbox
sandbox = (Sandbox()
  .upload_file("DeepSeek-R1.gguf", "/Deepseek-R1.gguf")
)

# Compile your function with the sandbox
@compile(..., sandbox=sandbox)
def predict (prompt: str) -> str:
    ...

Uploading a Directory

Use the Sandbox.upload_directory method to upload a directory and all its contents to a path in the sandbox:

predictor.py
from fxn import compile, Sandbox

# Upload a directory to the sandbox
sandbox = (Sandbox()
  .upload_file("resources/", "/resources")
)

# Compile your function with the sandbox
@compile(..., sandbox=sandbox)
def predict (prompt: str) -> str:
    ...

Defining Environment Variables

Use the Sandbox.env method to define plaintext environment variables:

predictor.py
from fxn import compile, Sandbox

# Define an environment variable
sandbox = (Sandbox()
  .env({ "FXN_WEBSITE": "https://fxn.ai" })
)

# Compile your function with the sandbox
@compile(..., sandbox=sandbox)
def predict (prompt: str) -> str:
    ...

Function does not yet support defining secrets. Do not provide secrets using sandbox environment variables as they are not designed for storing secrets.