Skip to main content
Templates define the base filesystem image used when creating sandboxes. The default default template includes Python 3, Node.js, Go, and common utilities. Custom templates let you pre-install packages, copy in project files, and configure the environment once so every sandbox starts ready to run.

Default template

Unless you specify a template parameter, sandboxes use the default template which provides:
  • Python 3 with pip
  • Node.js with npm
  • Go toolchain
  • Common shell utilities (curl, wget, git, tar, jq)

Build a custom template

from declaw import Template

template = Template.build(
    template="""
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y python3 python3-pip
RUN pip3 install numpy pandas scikit-learn matplotlib
WORKDIR /workspace
""",
    alias="data-science",
)
print(template.template_id)  # tpl-abc123

Build with options

template = Template.build(
    template="""
FROM ubuntu:22.04
RUN pip3 install anthropic openai langchain
COPY ./prompts /workspace/prompts
""",
    alias="llm-agent",
    cpu_count=2,
    memory_mb=1024,
    on_build_logs=lambda line: print(line),  # stream build output
)

TemplateBase model

TemplateBase describes a template configuration before it is built.
FieldTypeDescription
templatestrDockerfile content for the template image
aliasstr | NoneHuman-readable name for the template
cpu_countintDefault vCPUs for sandboxes from this template (1–8)
memory_mbintDefault RAM for sandboxes from this template (128–8192)
copy_fileslist[CopyItem]Files to copy into the template at build time

Copy files into a template

Use CopyItem to embed files from the host into the template image at build time.
from declaw import Template, CopyItem

template = Template.build(
    template="""
FROM ubuntu:22.04
RUN pip3 install -r /workspace/requirements.txt
""",
    copy_files=[
        CopyItem(
            source_path="./requirements.txt",
            dest_path="/workspace/requirements.txt",
        ),
        CopyItem(
            source_path="./scripts/",
            dest_path="/workspace/scripts/",
        ),
    ],
    alias="my-project",
)

CopyItem model

FieldTypeDescription
source_pathstrPath on the host machine
dest_pathstrDestination path inside the template image

Start background build

Use build_in_background() to start a build without waiting, then poll the status.
build_id = Template.build_in_background(
    template="""
FROM ubuntu:22.04
RUN apt-get install -y heavy-dependency
""",
    alias="slow-build",
)
print(f"Build started: {build_id}")

Check build status

from declaw import TemplateBuildStatus
import time

while True:
    status = Template.get_build_status(build_id)
    print(status.status, status.progress)

    if status.status == TemplateBuildStatus.done:
        print(f"Template ready: {status.template_id}")
        break
    elif status.status == TemplateBuildStatus.error:
        print(f"Build failed: {status.error}")
        break

    time.sleep(2)

BuildInfo model

FieldTypeDescription
build_idstrUnique build identifier
template_idstr | NoneSet once build completes successfully
statusTemplateBuildStatuspending, building, done, or error
progressfloatBuild progress 0.0–1.0
errorstr | NoneError message if status is error
logslist[str]Build log lines streamed during build

TemplateBuildStatus enum

ValueDescription
pendingBuild queued, not yet started
buildingBuild is in progress
doneBuild succeeded — template_id is set
errorBuild failed — error contains the reason

Use a custom template

from declaw import Sandbox

sbx = Sandbox.create(template="data-science")
result = sbx.commands.run("python3 -c 'import pandas; print(pandas.__version__)'")
print(result.stdout)  # 2.1.0
Or by template ID:
sbx = Sandbox.create(template="tpl-abc123")

Async template operations

from declaw import AsyncTemplate

template = await AsyncTemplate.build(
    template="""
FROM ubuntu:22.04
RUN pip3 install torch transformers
""",
    alias="ml-inference",
)
Template builds run on the server. Build time depends on the packages being installed and typically takes 1–5 minutes. Use build_in_background() for large builds to avoid blocking your process.