Chalk Compute Reference

This reference documents the chalkcompute Python SDK for building and deploying containers, sandboxes, volumes, remote functions, and scaling groups on Chalk's managed compute infrastructure. Use it to spin up isolated environments, stash files in persistent volumes, and deploy versioned Python functions and classes that scale on demand.

New to Chalk Compute? Start with the Compute overview for a conceptual tour of sandboxes, containers, scaling groups, and the deployment model. Then dive into Images, Functions, Volumes, and Sandbox design for task-oriented guides. The sections below document every public class and function in the SDK.

Sandbox quickstart

A full end-to-end sandbox: build an image with your code baked in, create the sandbox, exec a command inside it, read the output, and tear it down.

from chalkcompute import SandboxClient, Image

client = SandboxClient(target="sandboxes.example.com:443")

img = (
    Image.debian_slim("3.12")
    .pip_install(["pandas", "requests"])
    .add_local_file(
        "./analyze.py",
        "/app/analyze.py",
    )
    .workdir("/app")
)

sandbox = client.create(
    image=img,
    name="analysis-scratchpad",
    cpu="2",
    memory="4Gi",
)

result = sandbox.exec(
    "python",
    "/app/analyze.py",
    "--input", "/data/in.csv",
)
print(result.stdout_text)

sandbox.terminate()

Declarative, composable container image builder using a fluent API.

Each builder method returns a new Image instance so intermediate images can be shared and extended. Use class methods like Image.debian_slim() or Image.base() to start, then chain pip_install, run_commands, add_local_file, etc.

Image

Class

Declarative, composable container image builder.

Uses a fluent (method-chaining) API. Each method returns a new Image instance so that intermediate images can be shared and extended.

Examples

from chalkcompute import Image
img = (
    Image.debian_slim("3.12")
    .pip_install(["requests", "pandas"])
    .workdir("/home/user/app")
)
Attributes
lazy_local_files
set[LazyLocalFile]

Local files deferred for volume-mounting at sandbox start.

Returns

set[LazyLocalFile] Copy of the set of lazy local files tracked by this image.

Functions

Create an image from an arbitrary base image reference.

Parameters

Full image reference (e.g. "python:3.12-slim-bookworm", "ghcr.io/org/image:tag").

Returns
type:

A new Image based on the given reference.

from chalkcompute import Image
img = Image.base("ubuntu:22.04")
img = Image.base("ghcr.io/my-org/my-image:latest")

Create an image based on python:<version>-slim-bookworm.

Parameters

Python version tag.

Returns
type:

A new Image based on the selected Python slim image.

from chalkcompute import Image
img = Image.debian_slim()
img = Image.debian_slim("3.11")

Create an image from an existing Dockerfile.

The Dockerfile contents are transmitted as a dockerfile_commands step so that additional builder methods can be chained on top.

Parameters

Path to the Dockerfile.

Returns
type:

A new Image whose build steps mirror the Dockerfile.

from chalkcompute import Image
img = Image.from_dockerfile("./Dockerfile")
img = (
    Image.from_dockerfile("./Dockerfile")
    .pip_install(["rich"])
)

Run shell commands during the image build.

Each string is executed as a separate RUN instruction.

Returns
type:

A new Image with the run steps appended.

from chalkcompute import Image
img = (
    Image.debian_slim()
    .run_commands(
        "apt-get update",
        "apt-get install -y curl git",
    )
)

Install Python packages via pip.

Parameters

List of pip requirement specifiers (e.g. ["requests>=2.28", "pandas"]).

Returns
type:

A new Image with the pip install step appended.

from chalkcompute import Image
img = (
    Image.debian_slim()
    .pip_install(["requests>=2.28", "pandas"])
)

Install packages from a requirements.txt file.

The file is read locally and its contents are inlined into the image spec so that the file does not need to be accessible at build time.

Parameters

Path to requirements.txt.

Returns
type:

A new Image with the pip install step appended.

from chalkcompute import Image
img = (
    Image.debian_slim()
    .pip_install_from_requirements("./requirements.txt")
)

Install Python packages via uv pip install.

Uses the uv resolver instead of pip for significantly faster installs. Requires a base image that has uv available.

Parameters

List of pip requirement specifiers (e.g. ["requests>=2.28", "pandas"]).

Returns
type:

A new Image with the uv pip install step appended.

from chalkcompute import Image
img = (
    Image.base("ghcr.io/astral-sh/uv:python3.12-bookworm-slim")
    .uv_pip_install(["requests", "polars"])
)

Add a local file into the image.

Parameters

Local file path.

Absolute destination path in the image.

mode: = None

Optional file permission mode (e.g. 0o755).

strategy:
'copy' | 'volume'
= 'volume'

"copy" inlines the file contents into the image spec. "volume" (default) defers the file as a lazy reference to be mounted at sandbox start.

Returns
type:

A new Image with the file registered for inclusion.

from chalkcompute import Image
img = (
    Image.debian_slim()
    .add_local_file(
        "./config.yaml",
        "/app/config.yaml",
    )
    .add_local_file(
        "./entrypoint.sh",
        "/app/entrypoint.sh",
        mode=0o755,
    )
)

Add an entire local directory into the image.

Automatically respects .gitignore and .chalkignore files found at any level within src (each scoped to its directory subtree, matching go-git's behavior). The .git/ directory is always excluded. Negation patterns (!) are supported.

Parameters

Local directory path.

Absolute destination directory in the image.

strategy:
'copy' | 'volume'
= 'volume'

"copy" inlines all file contents into the image spec. "volume" (default) defers each file as a lazy reference to be mounted at sandbox start.

exclude: = None

Additional glob patterns to skip (matched against relative paths). These are applied on top of patterns loaded from ignore files, e.g. ["data/**", "*.csv"].

archive: = False

When True (volume strategy only), tar the directory into a single file for upload instead of individual files. The caller is responsible for extracting at the destination.

Returns
type:

A new Image with the directory registered for inclusion.

from chalkcompute import Image
img = (
    Image.debian_slim()
    .add_local_dir(
        "./src",
        "/app/src",
        exclude=["*.pyc", "__pycache__"],
    )
)

Add raw Dockerfile instructions.

These are injected verbatim into the generated Dockerfile.

Parameters

List of Dockerfile instructions (e.g. ["RUN echo hello", "EXPOSE 8080"]).

Returns
type:

A new Image with the Dockerfile commands appended.

from chalkcompute import Image
img = (
    Image.debian_slim()
    .dockerfile_commands([
        "USER root",
        "EXPOSE 8080",
    ])
)

Set environment variables in the image.

Parameters

Mapping of variable names to values.

Returns
type:

A new Image with the environment variables applied.

from chalkcompute import Image
img = (
    Image.debian_slim()
    .env({
        "LOG_LEVEL": "DEBUG",
        "PORT": "8080",
    })
)

Set the working directory in the image.

Parameters

Absolute path for the working directory.

Returns
type:

A new Image with the working directory set.

from chalkcompute import Image
img = (
    Image.debian_slim()
    .workdir("/home/user/app")
)

Set the container entrypoint.

Parameters

Entrypoint as exec-form list (e.g. ["/bin/bash"]).

Returns
type:

A new Image with the entrypoint set.

from chalkcompute import Image
img = (
    Image.debian_slim()
    .entrypoint(["python", "-m", "my_app"])
)

Set the container CMD.

The CMD is the default argument list passed to the container's entrypoint when the container starts. Unlike entrypoint — which sets the executable that always runs — CMD defines the default arguments that can be overridden at run time (for example, by a Container.run(command=[...]) call or a Kubernetes pod spec).

If no entrypoint is set, the first element of CMD is treated as the executable. If an entrypoint is set, CMD is appended to it.

Parameters

CMD as exec-form list (e.g. ["/bin/bash", "-l"]).

Returns
type:

A new Image with the CMD set.

Run python -m my_app by default, but allow the caller to override the module at run time:

from chalkcompute import Image
img = (
    Image.debian_slim()
    .entrypoint(["python"])
    .cmd(["-m", "my_app"])
)

Validate lazy local file destinations for volume mounting.

Volume-backed lazy files are grouped by parent directory and mounted at that directory. Root-level destinations would require mounting at /, which is not supported by container runtimes.

Raises
error:

If a lazy local file would require mounting at the root directory.

Return lazy local files grouped by mount directory.

Files from add_local_dir carry a mount_root that keeps all files from one call in a single volume. Files from add_local_file (mount_root is None) fall back to grouping by parent directory.

Returns
type:
dict[str, list[LazyLocalFile]]

Mapping from mount directory to the files that should be placed there.

Serialize this image definition to a protobuf ImageSpec.

Returns
type:
pb.ImageSpec

The protobuf representation of this image.

High-level container abstraction backed by the Chalk ContainerService.

Orchestrates image building, local file upload via volumes, and container lifecycle in a single API. Use Container(image=...).run() to start, .exec() to run commands, and .stop() to clean up.

A managed container backed by the Chalk ContainerService.

Orchestrates image building, local file upload via volumes, and container lifecycle in a single high-level API.

Examples

Build an image, run a container, and exec a command inside it:

from chalkcompute import Container, Image
img = (
    Image.debian_slim()
    .pip_install(["requests"])
    .add_local_file("./script.py", "/app/script.py")
)
c = Container(
    image=img,
    name="hello-container",
    cpu="1",
    memory="2Gi",
    env={"LOG_LEVEL": "INFO"},
).run()
result = c.exec("python", "/app/script.py")
result.stdout_text
'hello\n'
c.stop()
Attributes

Opaque container identifier assigned by the service.

None until run (or from_id / from_name) has resolved the container against the service.

The most recently fetched ContainerInfo snapshot.

None until the container has been started or attached to. Populated by run, from_id, and from_name. Call refresh_info to fetch the latest state from the service.

The fully qualified registry URI of the image the container uses.

Set after run finishes the image build (or, if the container was constructed with a string image reference, equal to that reference). None before the container has been started.

Functions

Attach to an existing container by ID.

Parameters

Opaque container identifier assigned by the service.

Returns
type:

A Container handle bound to the existing container.

Raises

If the container cannot be found.

Attach to an existing container by name.

Parameters

DNS-safe container name.

Returns
type:

A Container handle bound to the existing container.

Raises

If the container cannot be found.

Build the image, upload local files, and start the container.

Walks through three phases and blocks until the container is ready (or raises): (1) build the image via CustomImageService, (2) if the Image has add_local_file / add_local_dir entries registered with strategy="volume", upload them to a managed volume and attach it, (3) start the container and poll until it reaches the Running state.

Parameters

Seconds between status polls for both build and startup.

Maximum seconds to wait for the image build to complete.

Maximum seconds to wait for the container to reach Running.

Returns
type:

self, to allow chaining.

Raises

If the container fails to start or times out.

If the image build fails.

Build an image from scratch, start a container, inspect it, and tear it down:

from chalkcompute import Container, Image
img = (
    Image.debian_slim("3.12")
    .pip_install(["requests"])
    .add_local_file(
        "./app.py",
        "/app/app.py",
    )
    .workdir("/app")
)
c = Container(
    image=img,
    name="hello-container",
    cpu="1",
    memory="2Gi",
    env={"LOG_LEVEL": "INFO"},
)
c.run()
c.info.status
'Running'
c.stop()

Execute a command in the running container.

The command runs as a one-shot process inside the already-started container (equivalent to docker exec). Output is captured in full and returned at once; use exec if you need a streaming interface.

Parameters

Optional execution timeout in seconds.

Returns

The stdout, stderr, and exit code of the executed command.

Raises

If the container is not running or the exec fails.

Build an image, start a container, run a few commands against it, and clean up:

from chalkcompute import Container, Image
img = (
    Image.debian_slim("3.12")
    .pip_install(["requests"])
    .add_local_file(
        "./fetch.py",
        "/app/fetch.py",
    )
)
c = Container(
    image=img,
    name="fetcher",
    cpu="1",
    memory="1Gi",
).run()
hello = c.exec("python", "/app/fetch.py", "https://example.com")
hello.exit_code
0
hello.stdout_text.splitlines()[0]
'<!doctype html>'
err = c.exec("ls", "/does-not-exist")
err.exit_code
2
err.stderr_text
"ls: cannot access '/does-not-exist': No such file or directory\n"
c.stop()

Stop the container and clean up temporary volumes and secrets.

Parameters

Optional grace period before forceful termination.

Fetch latest container status from the server.

Returns

The latest metadata for the container.

Raises

If no container ID is set (run() was never called).

Metadata about a running container.

Attributes

id Opaque container identifier assigned by the service. name DNS-safe container name. status Lifecycle state reported by the service (e.g. "Running"). pod_name Underlying Kubernetes pod name. web_url Optional URL exposing the container's port, if one is published.

Result of a completed command execution.

Attributes

exit_code Exit code of the process, if it exited normally. signal Signal number that terminated the process, if any. pid PID of the process, if known. error Error string if execution failed.

Attributes
stdout
Iterator[str]

Iterate over stdout lines.

stderr
Iterator[str]

Iterate over stderr lines.

Base exception for Container errors.

Raised when the custom image build fails.

Low-level sandbox management backed by the Chalk SandboxService (gRPC).

Provides bidirectional streaming exec, interactive stdin/signal control, and fine-grained process lifecycle management.

Client for the Sandbox gRPC Service.

Functions

Initialize the gRPC client.

Parameters
target: = 'localhost:50051'

gRPC server address (e.g. "localhost:50051" or "sandbox.example.com:443").

credentials:
grpc.ChannelCredentials | None
= None

Optional TLS/SSL channel credentials. If None, uses insecure channel.

metadata:
builtins.list[tuple[str, str]] | None
= None

Optional default metadata to send with every RPC call.

Close the gRPC channel.

Create a new sandbox.

When image is an Image spec, the server builds it asynchronously and returns a sandbox in the "building" state. If wait is True (the default), this method polls GetSandbox until the sandbox reaches "ready" (or "error").

Parameters

Container image to use — either a string reference (e.g. "ubuntu:latest") or a declarative Image builder.

name: = None

Optional name for the sandbox.

cpu: = None

Optional CPU limit (e.g. "1", "500m").

memory: = None

Optional memory limit (e.g. "512Mi", "1Gi").

env: = None

Optional environment variables.

volumes:
builtins.list[pb.VolumeMount] | None
= None

Optional list of VolumeMount protos to attach.

wait: = True

If True, poll until the sandbox is ready (default True).

Seconds between GetSandbox polls (default 5).

timeout: = 600.0

Maximum seconds to wait for the sandbox to become ready (default 600).

Returns
type:

A sandbox handle.

Get a sandbox handle by ID.

Does not validate the sandbox exists until you call a method on it.

Parameters

Sandbox identifier.

Returns
type:

A sandbox handle.

List all sandboxes.

Returns
type:
builtins.list[SandboxInfo]

One entry per sandbox.

Sandbox

Class

A handle to a sandbox instance.

Obtain instances from create or get. Use exec to run commands and terminate to shut the sandbox down when you're done.

Examples

from chalkcompute import SandboxClient, Image
sandbox = SandboxClient().create(
    image=Image.debian_slim().pip_install(["numpy"]),
)
result = sandbox.exec(
    "python",
    "-c",
    "import numpy; print(numpy.__version__)",
)
result.stdout_text.strip()
'1.26.4'
sandbox.terminate()
Attributes

Get sandbox info, fetching from server if needed.

Functions

Force-refresh sandbox info from the server.

Execute a command and wait for it to complete.

Parameters

Command binary or script to execute.

args: = ()

Optional timeout in seconds.

workdir: = None

Optional working directory inside the sandbox.

env: = None

Optional environment variables to set for the command.

Returns

Result with stdout/stderr and exit code.

Execute a command and stream events as they arrive.

Parameters

Command binary or script to execute.

args: = ()

Optional timeout in seconds.

workdir: = None

Optional working directory inside the sandbox.

env: = None

Optional environment variables to set for the command.

Start a command and return an ExecProcess handle for interactive use.

The caller can write to stdin, send signals, and iterate output.

Parameters

Command binary or script to execute.

args: = ()

Optional timeout in seconds.

workdir: = None

Optional working directory inside the sandbox.

env: = None

Optional environment variables to set for the command.

Returns

Handle for interacting with the running process.

Terminate this sandbox.

Parameters

Optional grace period to allow running processes to shut down.

Result of a completed command execution.

Attributes

exit_code Exit code of the process, if it exited normally. signal Signal number that terminated the process, if any. pid PID of the process, if known. error Error string if execution failed.

Attributes
stdout
Iterator[str]

Iterate over stdout lines.

stderr
Iterator[str]

Iterate over stderr lines.

A single event from a streaming exec call.

Attributes

stdout Stdout text chunk, if this event carries stdout output. stderr Stderr text chunk, if this event carries stderr output. pid PID of the started process, set on the process-started event. exit_code Exit code of the process, set on the process-exited event. exit_signal Signal number that terminated the process, if any. error_code Error code string, set on error events. error_message Error message string, set on error events.

Handle to a running exec stream, allowing stdin writes and signal sends.

Attributes

pid PID of the running process once the server reports it.

Attributes
Functions

Send data to the process's stdin.

Parameters

Bytes or string to write to stdin. Strings are UTF-8 encoded.

Signal EOF on stdin.

Send a signal to the process.

Parameters
sig: = signal.SIGTERM

Signal number to send. Defaults to SIGTERM.

Iterate over output events from the process.

Consume all output and return the final result.

Returns

Aggregated stdout, stderr, exit code, and signal information.

SandboxError

Exception

Base exception for sandbox errors.

Raised when a sandbox is not found.

Raised when a sandbox has terminated.

Raised when a sandbox image build fails.

Persistent named volumes backed by object storage.

VolumeClient manages volume lifecycle; Volume provides file operations (read, write, list, delete, batch upload).

Client for the Volume gRPC Service.

Functions

Initialize the gRPC client.

Parameters
target: = 'localhost:50051'

gRPC server address (e.g. "localhost:50051" or "volumes.example.com:443").

credentials:
grpc.ChannelCredentials | None
= None

Optional TLS/SSL channel credentials. If None, uses insecure channel.

metadata:
builtins.list[tuple[str, str]] | None
= None

Optional default metadata to send with every RPC call.

Close the gRPC channel.

Create a new named volume.

If the volume already exists on the server, behavior depends on the server implementation (may return the existing volume or error).

Parameters

Name for the volume.

Returns
type:

A volume handle.

Get a Volume handle by name.

Does not validate the volume exists until you call a method on it. Use this when you know the volume already exists.

Parameters

Name of the existing volume.

Returns
type:

A volume handle.

Look up a volume by name, verifying it exists.

Parameters

Name of the volume.

Returns
type:

A volume handle with info pre-fetched.

Raises

If the volume does not exist.

List all volumes.

Returns
type:
builtins.list[VolumeInfo]

One entry per volume.

Volume

Class

A handle to a named volume, providing file operations.

Volumes are persistent storage backed by object storage. Obtain an instance via create or get.

Examples

Create a volume, upload files, and read them back:

from chalkcompute import VolumeClient
client = VolumeClient(target="volumes.example.com:443")
vol = client.create("models")
vol.put("config.yaml", b"learning_rate: 0.01\n")
vol.get("config.yaml")
b'learning_rate: 0.01\n'
[f.path for f in vol.list()]
['config.yaml']

Batch-upload multiple files atomically:

with vol.batch_upload() as batch:
    batch.put("a.txt", b"hello")
    batch.put("b.txt", b"world")
Attributes

Get volume info, fetching from server if needed.

Functions

Force-refresh volume info from the server.

Read a file's contents from the volume.

Parameters

File path within the volume.

Returns
type:

The raw file bytes.

Write a file into the volume.

Parameters

Destination path within the volume.

File contents (bytes or str).

Returns
type:

self, to allow chaining.

Upload a local file into the volume.

Parameters

Destination path within the volume.

Local file to read and upload.

Returns
type:

self, to allow chaining.

Remove a file from the volume.

Parameters

File path within the volume.

Returns
type:

self, to allow chaining.

List files in the volume.

Parameters

Only return files whose paths start with this prefix.

Currently always recursive (server-side behavior).

Returns
type:

One entry per matching file.

Iterate over files in the volume.

Same as listdir() but returns an iterator for lazy consumption.

Parameters

Only return files whose paths start with this prefix.

Context manager for batching multiple file uploads.

All staged files are uploaded when the context manager exits.

with vol.batch_upload() as batch:
    batch.put("a.txt", b"hello")
    batch.put("b.txt", b"world")

Delete this volume and all its contents.

Attributes
created_at
_timestamp_pb2.Timestamp
Attributes
updated_at
_timestamp_pb2.Timestamp

VolumeError

Exception

Base exception for volume errors.

Raised when a volume is not found.

Deploy Python functions as versioned, autoscaling remote functions.

Remote functions turn an ordinary Python callable into a managed service. Chalk packages the function into a container image, deploys it as a ScalingGroup, routes incoming calls over gRPC, and handles replica autoscaling, warm-pool management, batching, and retries. Input and output Arrow schemas are inferred from the function's type hints so callers can invoke it with native Python values and the transport layer stays hidden.

The decorator form, function, is the common entrypoint: annotate any callable with @chalkcompute.function(image=..., cpu=..., memory=...) and call .deploy() to publish a new version. For the imperative API — useful when wrapping functions defined elsewhere or deploying programmatically — construct a RemoteFunction directly.

Use RetryPolicy to configure automatic retries, and catch the FunctionError / FunctionBuildError hierarchy for deploy-time and runtime failures.

Decorator that deploys a function as a versioned remote function.

Locally (at decoration time):

  1. Derives Arrow schemas from the function's type hints
  2. Builds a deploy image with a handler shim
  3. Registers the function version via ExternalFunctionCatalogService
  4. If CHALK_FUNCTION_LOCAL_SDK_COPY=1, includes local SDK source in the deploy image for unreleased feature iteration.

Remotely (inside the container): The decorator is a no-op — returns the raw function unchanged.

Parameters
image:
_Image | None
= None

Container image to deploy in. Defaults to Image.debian_slim().

cpu: = None

CPU resource request (e.g. "1", "500m").

memory: = None

Memory resource request (e.g. "1Gi", "512Mi").

gpu: = None

GPU resource request (e.g. "nvidia-l4", "2:nvidia-a100"). Format is "<count>:<type>" or just "<type>" for a single GPU.

name: = None

Custom function name. Defaults to the function's __name__.

env: = None

Environment variables to pass to the function.

Minimum number of replicas.

Maximum number of replicas.

List of (name, mount_path) tuples for persistent storage.

secrets:
list[_Secret] | None
= None

List of Secret references to inject.

Wire format for arguments and results. Currently only "pyarrow" is supported.

Time in seconds until containers shut down after the last request.

options: = None

Arbitrary key-value options forwarded to the Chalk platform.

Maximum time in milliseconds to buffer incoming items before invoking the handler. Defaults to 1000 ms when batching is enabled. When batching is enabled, handler args are lists or a single pyarrow table.

Maximum number of items to accumulate before invoking the handler. Defaults to 10 when batching is enabled.

Retry policy for handler invocations. Pass an int for simple max-attempts with default exponential backoff, or a RetryPolicy for full control.

Name of an externally defined rate limit policy. Used to control outbound concurrency from multiple sources.

import chalkcompute
@chalkcompute.function()
def add(x: int, y: int) -> int:
    return x + y
add(1, 2)

A versioned remote function backed by ExternalFunctionCatalogService.

Deploys a Python function as a ScalingGroup and registers it with input/output Arrow schemas derived from the function's type hints.

Most users should prefer the function decorator; use RemoteFunction directly when you need the imperative API (for example, when wrapping a function defined elsewhere, or when constructing deployments programmatically).

Examples

Decorator form — the most common entrypoint:

import chalkcompute
from chalkcompute import Image
img = (
    Image.debian_slim()
    .pip_install(["numpy"])
)
@chalkcompute.function(
    image=img,
    cpu="1",
    memory="2Gi",
    min_replicas=1,
    max_replicas=4,
)
def embed(text: str) -> list[float]:
    import numpy as np
    return np.ones(8).tolist()
embed.deploy()
embed("hello world")
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

Imperative form, for constructing a RemoteFunction from an existing callable:

from chalkcompute import RemoteFunction, Image
def square(x: int) -> int:
    return x * x
fn = RemoteFunction(
    square,
    image=Image.debian_slim(),
    name="square",
    min_replicas=1,
    max_replicas=2,
)
fn.deploy()
fn(7)
49
Functions

Attach to an existing function version by ID.

Parameters

Identifier of the function version.

Returns

A handle bound to the existing version.

Attach to an existing function version by name.

Parameters

Name of the function.

Returns

A handle bound to the existing version.

Build the image, register the function version, and wait until running.

Parameters

Seconds between status polls.

Maximum seconds to wait for the image build.

Maximum seconds to wait for the scaling group to become ready.

Returns

self for chaining.

Wait until the backing scaling group has at least one ready replica.

Parameters
timeout: = 120.0

Maximum seconds to wait.

Seconds between status polls.

Returns

self for chaining.

Raises

If the function has not been deployed or never becomes ready.

Call the remote function via gRPC or the API server.

Returns
type:

Scalar result for single-row calls, or a list of results otherwise.

Delete this function version.

Fetch latest function version info from the server.

Returns

Updated version info.

Raises

If the function has not been deployed or the RPC fails.

Metadata about a deployed function version.

Attributes

id Unique identifier of the function version. function_name Registered function name. version Monotonic version number for this function. scaling_group_name Name of the scaling group backing this version.

Retry configuration for handler invocations.

Construct via the named constructors rather than instantiating directly. With exponential backoff, wait times grow geometrically (initial, initial * multiplier, initial * multiplier^2, ...) capped at max_wait. With linear, wait times are constant between retries. A plain int passed to function(retries=3) is shorthand for RetryPolicy.exponential(attempts=3). By default all Exception subclasses trigger a retry; pass a tuple of exception classes to retry_on to narrow the set.

Attributes

max_retries Maximum number of retry attempts (not counting the initial call). initial_wait_seconds Base wait time in seconds before the first retry. backoff_multiplier Multiplier applied to wait time on each successive retry. Use 2.0 for exponential backoff (the default) or 1.0 for fixed delay. max_wait_seconds Upper bound on wait time between retries. jitter If True, add random jitter to wait times to avoid thundering herd. retry_on Tuple of exception types that trigger a retry. Non-matching exceptions propagate immediately without consuming retry budget.

Examples

RetryPolicy.exponential(attempts=5, initial=1.0, max_wait=30.0)
RetryPolicy.linear(attempts=3, delay=2.0)
RetryPolicy.exponential(
    attempts=3,
    retry_on=(ConnectionError, TimeoutError, OSError),
)
Functions

Create a policy with exponential backoff.

Parameters

Maximum number of retry attempts.

Base wait time in seconds before the first retry.

Multiplier applied to wait time on each successive retry.

Upper bound on wait time between retries.

jitter: = True

If True, add random jitter to wait times.

retry_on:
tuple[type[BaseException], ...]
= (Exception)

Exception types that trigger a retry.

Returns

A configured RetryPolicy.

RetryPolicy.exponential(attempts=5, initial=0.5, max_wait=30.0)
RetryPolicy.exponential(attempts=3, retry_on=(TimeoutError,))

Create a policy with fixed delay between retries.

Parameters

Maximum number of retry attempts.

Fixed wait time in seconds between retries.

jitter: = False

If True, add random jitter to wait times.

retry_on:
tuple[type[BaseException], ...]
= (Exception)

Exception types that trigger a retry.

Returns

A configured RetryPolicy.

RetryPolicy.linear(attempts=3, delay=2.0)
RetryPolicy.linear(
    attempts=5,
    delay=0.5,
    retry_on=(ConnectionError, TimeoutError),
)

Serialize to a plain dict for metadata / JSON export.

The retry_on field is serialized as a list of exception class names only when it differs from the default (Exception,).

Returns
type:

A JSON-serializable dict representation of the policy.

Normalize int | RetryPolicy into a RetryPolicy.

An int is treated as RetryPolicy.exponential(attempts=value).

Parameters

Either a max-attempts integer shorthand or a pre-built policy.

Returns

The normalized policy.

FunctionError

Exception

Base exception for Function errors.

Raised when the function image build fails.

Deploy Python classes with multiple methods as versioned remote functions.

Use @chalkcompute.cls() with @chalkcompute.method() to mark methods for deployment. Lifecycle hooks @before() and @after() run startup and shutdown logic in each method's container.

Decorator that deploys a class as versioned remote functions.

Locally (at decoration time):

  1. Scans the class for @method(), @before(), @after()
  2. Builds a deploy image with per-method handler shims
  3. Registers each method via ExternalFunctionCatalogService
  4. If CHALK_FUNCTION_LOCAL_SDK_COPY=1, includes local SDK source in the deploy image for unreleased feature iteration.

Remotely (inside the container): The decorator is a no-op — returns an instance of the class.

Parameters
image:
_Image | None
= None

Container image to deploy in. Defaults to Image.debian_slim().

cpu: = None

CPU resource request (e.g. "1", "500m").

memory: = None

Memory resource request (e.g. "1Gi", "512Mi").

gpu: = None

GPU resource request (e.g. "nvidia-l4", "2:nvidia-a100").

name: = None

Custom class name. Defaults to the class's __name__.

env: = None

Environment variables to pass to the class containers.

Minimum number of replicas per method.

Maximum number of replicas per method.

List of (name, mount_path) tuples for persistent storage.

secrets:
list[_Secret] | None
= None

List of Secret references to inject.

Wire format for arguments and results. Currently only "pyarrow" is supported.

Time in seconds until containers shut down after the last request.

options: = None

Arbitrary key-value options forwarded to the Chalk platform.

Maximum time in milliseconds to buffer incoming items before invoking the handler.

Maximum number of items to accumulate before invoking the handler.

Retry policy for handler invocations. Pass an int for simple max-attempts with default exponential backoff, or a RetryPolicy for full control.

Name of an externally defined rate limit policy. Used to control outbound concurrency from multiple sources.

Mark a class method for remote deployment.

Mark a method as the startup hook (called before the server starts).

Mark a method as the shutdown hook (called when the process exits).

A managed class backed by ExternalFunctionCatalogService.

Deploys each @method() as a separate function version with its own scaling group, sharing a single container image. Lifecycle hooks (@before() / @after()) run in every method's container.

Most users should prefer the cls decorator.

Examples

Deploy a stateful class with an expensive warm-up step, then call individual methods remotely:

import chalkcompute
from chalkcompute import Image
img = (
    Image.debian_slim()
    .pip_install(["sentence-transformers"])
)
@chalkcompute.cls(
    image=img,
    cpu="2",
    memory="4Gi",
    min_replicas=1,
    max_replicas=2,
)
class Embedder:
    @chalkcompute.before()
    def load(self):
        from sentence_transformers import SentenceTransformer
        self.model = SentenceTransformer("all-MiniLM-L6-v2")
    @chalkcompute.method()
    def embed(self, text: str) -> list[float]:
        return self.model.encode(text).tolist()
    @chalkcompute.method()
    def embed_batch(self, texts: list[str]) -> list[list[float]]:
        return self.model.encode(texts).tolist()
    @chalkcompute.after()
    def shutdown(self):
        del self.model
Embedder.deploy()
e = Embedder()
vec = e.embed("hello world")
batch = e.embed_batch(["a", "b", "c"])
Functions

Attach to an existing remote class by name.

Discovers all function versions whose functionName starts with {name}. and rebuilds a RemoteClass with callable methods.

Parameters

Class name previously used at deploy time.

Returns

A handle bound to the existing versions.

Raises
error:

If no function versions are found for the given name.

Build the image and deploy all methods as function versions.

Parameters

Seconds between status polls.

Maximum seconds to wait for the image build.

Maximum seconds to wait for scaling groups to become ready.

Returns

self for chaining.

Wait until all backing scaling groups have ready replicas.

Parameters
timeout: = 120.0

Maximum seconds to wait per method.

Seconds between status polls.

Returns

self for chaining.

Delete all function versions and clean up volumes.

Fetch latest info for all methods from the server.

Returns

Mapping of method name to updated FunctionVersionInfo.

ClassError

Exception

Base exception for RemoteClass errors.

Raised when the class image build fails.

Managed scaling groups for deploying HTTP/gRPC services.

Orchestrates image building, local file upload, and scaling group lifecycle. Use .call() to make HTTP requests to the deployed service.

A managed scaling group backed by ScalingGroupManagerService.

Orchestrates image building, local file upload via volumes, and scaling-group lifecycle in a single high-level API.

Examples

from chalkcompute import ScalingGroup, Image
img = (
    Image.debian_slim()
    .pip_install(["flask"])
    .add_local_file("./app.py", "/app/app.py")
    .entrypoint(["python", "/app/app.py"])
)
sg = ScalingGroup(image=img, port=8080).deploy().wait_ready()
resp = sg.call("/health", method="GET")
sg.delete()
Functions

Attach to an existing scaling group by ID.

Parameters

Opaque scaling-group identifier assigned by the service.

Returns

A ScalingGroup handle bound to the existing resource.

Raises

If the scaling group cannot be found.

Attach to an existing scaling group by name.

Parameters

DNS-safe scaling-group name.

Returns

A ScalingGroup handle bound to the existing resource.

Raises

If the scaling group cannot be found.

Build the image, upload local files, and create the scaling group.

Parameters

Seconds between status polls for both build and startup.

Maximum seconds to wait for the image build to complete.

Maximum seconds to wait for the scaling group to reach Running.

Returns

self, to allow chaining.

Raises

If deployment fails or times out.

Make an HTTP request to the scaling group's endpoint.

Parameters
path: = '/'

URL path appended to the scaling group's web_url.

method: = 'POST'

HTTP method.

json: = None

Optional JSON-serializable body.

data: = None

Optional raw bytes body (mutually exclusive with json).

headers: = None

Optional HTTP headers.

Request timeout in seconds.

Returns
type:
httpx.Response

The HTTP response.

Raises

If the scaling group has no web_url yet.

Wait until a running scaling group has at least one ready replica.

Parameters
timeout: = 120.0

Maximum seconds to wait before giving up.

Seconds between status polls.

Returns

self, to allow chaining.

Raises

If the group enters a terminal failed state or never becomes ready before the timeout elapses.

Delete the scaling group and clean up associated volumes.

Fetch latest scaling-group status from the server.

Returns

The latest metadata for the scaling group.

Raises

If no scaling-group ID is set (deploy() was never called).

Metadata about a scaling group.

Attributes

id Opaque scaling-group identifier assigned by the service. name DNS-safe scaling-group name. status Lifecycle state reported by the service (e.g. "Running"). web_url URL to which HTTP traffic should be sent, if ready. ready_replicas Number of replicas currently ready to serve traffic. available_replicas Number of replicas available behind the service endpoint.

Base exception for ScalingGroup errors.

Raised when the custom image build fails.

References to secrets available to a sandbox, container, or remote function.

Use Secret.from_name(...) to reference a secret by name; the secret value is resolved at runtime from the Chalk secret store and injected as an environment variable.

Secret

Class

A reference to a Chalk secret or integration to inject as env vars.

Construct instances via the factory methods rather than instantiating directly.

Examples

Secret.from_env("OPENAI_API_TOKEN")
Secret.from_integration("prod_postgres")
Secret.from_local_env("OPENAI_API_KEY")
Secret.from_local_env("OPENAI_API_KEY", env_var_name="API_KEY")
Secret.from_local_env_file(".env")
Attributes

Whether this secret requires local resolution before container start.

Returns

bool True if the secret is built from a local env var or env file.

Functions

Reference a standalone secret by name (e.g. "OPENAI_API_TOKEN").

The secret's value is injected as an environment variable with the same name, unless alias or prefix is given.

Parameters

Name of the Chalk secret.

alias: = None

Rename the env var exposed in the container.

prefix: = None

Optional prefix applied to the env var name.

Returns
type:

A secret reference.

Reference an integration by name (e.g. "prod_postgres").

All secrets associated with the integration are injected as env vars.

Parameters

Name of the Chalk integration.

keys: = None

Limit injection to the listed keys.

aliases: = None

Rename specific keys when exposing them as env vars.

prefix: = None

Optional prefix applied to every injected env var.

Returns
type:

A secret reference bound to the named integration.

Inject a local environment variable as a secret into the container.

Reads os.environ[local_env_var] at container start time, upserts it as a Chalk secret, and injects it as an env var in the container.

Parameters

Name of the local environment variable to read.

Name for the env var inside the container. Defaults to local_env_var.

Returns
type:

A lazy secret reference resolved at Container.run() time.

Inject all variables from a local .env file as secrets.

Reads the file at container start time, upserts each KEY=VALUE pair as a Chalk secret, and injects them as env vars in the container. Lines starting with # and blank lines are ignored.

Parameters

Path to the local .env file.

Returns
type:

A lazy secret reference resolved at Container.run() time.

Secret

Class

A reference to a Chalk secret or integration to inject as env vars.

Construct instances via the factory methods rather than instantiating directly.

Examples

Secret.from_env("OPENAI_API_TOKEN")
Secret.from_integration("prod_postgres")
Secret.from_local_env("OPENAI_API_KEY")
Secret.from_local_env("OPENAI_API_KEY", env_var_name="API_KEY")
Secret.from_local_env_file(".env")
Attributes

Whether this secret requires local resolution before container start.

Returns

bool True if the secret is built from a local env var or env file.

Functions

Reference a standalone secret by name (e.g. "OPENAI_API_TOKEN").

The secret's value is injected as an environment variable with the same name, unless alias or prefix is given.

Parameters

Name of the Chalk secret.

alias: = None

Rename the env var exposed in the container.

prefix: = None

Optional prefix applied to the env var name.

Returns
type:

A secret reference.

Reference an integration by name (e.g. "prod_postgres").

All secrets associated with the integration are injected as env vars.

Parameters

Name of the Chalk integration.

keys: = None

Limit injection to the listed keys.

aliases: = None

Rename specific keys when exposing them as env vars.

prefix: = None

Optional prefix applied to every injected env var.

Returns
type:

A secret reference bound to the named integration.

Inject a local environment variable as a secret into the container.

Reads os.environ[local_env_var] at container start time, upserts it as a Chalk secret, and injects it as an env var in the container.

Parameters

Name of the local environment variable to read.

Name for the env var inside the container. Defaults to local_env_var.

Returns
type:

A lazy secret reference resolved at Container.run() time.

Inject all variables from a local .env file as secrets.

Reads the file at container start time, upserts each KEY=VALUE pair as a Chalk secret, and injects them as env vars in the container. Lines starting with # and blank lines are ignored.

Parameters

Path to the local .env file.

Returns
type:

A lazy secret reference resolved at Container.run() time.