# Chalk Compute Reference: Volume
source: https://docs.chalk.ai/compute-docs#section-volume

Persistent named volumes backed by object storage.

[`VolumeClient`](https://docs.chalk.ai/compute-docs#VolumeClient) manages volume lifecycle; [`Volume`](https://docs.chalk.ai/compute-docs#Volume) provides file operations (read, write, list, delete, batch upload).

## `VolumeClient`
source: https://docs.chalk.ai/compute-docs#VolumeClient

Class

Client for the Chalk Volume service.

### `VolumeClient.__init__(endpoint, token, env_id, commit_author, extra_headers, chalk_server, credentials, metadata, options)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.__init__

Initialize the typed volume control plane and native data plane.

**Parameters**

- `endpoint`: `str` = `'http://localhost:50051'`
  Volume service endpoint. Values without a scheme are converted to `http://` or `https://` depending on `credentials`.
- `token`: `str` = `''`
  Optional bearer token used for service authentication.
- `env_id`: `str | None` = `None`
  Chalk environment id to send with requests.
- `commit_author`: `str | None` = `None`
  Optional commit author URN. If omitted, the native client derives one from the request credentials.
- `extra_headers`: `builtins.list[tuple[str, str]] | None` = `None`
  Additional request headers to send with every call.
- `chalk_server`: `str` = `'go-api'`
  Target Chalk server name.
- `credentials`: `object | None` = `None`
  Optional TLS/SSL credentials marker. If set, schemeless endpoints use `https://`.
- `metadata`: `object | None` = `None`
  Optional default metadata. `authorization`, `x-chalk-env-id`, and `x-chalk-server` are translated into native client settings.
- `options`: `object | None` = `None`
  Accepted for compatibility with the gRPC-backed constructor and ignored by the native client.

### `VolumeClient.from_env(chalk_client)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.from_env

Create a volume client using environment authentication.

**Parameters**

- `chalk_client`: `Any | None` = `None`
  Chalk client supplying authentication. Defaults to environment auth.

**Returns**

`VolumeClient`

Client that owns its connection and closes it on context exit or an explicit `close` call.

### `VolumeClient.from_connect(connect)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.from_connect

Create a client from an authenticated [`ConnectClient`](https://docs.chalk.ai/compute-docs#ConnectClient).

**Parameters**

- `connect`: `ConnectClient`
  An authenticated [`ConnectClient`](https://docs.chalk.ai/compute-docs#ConnectClient). Auth is ensured automatically if not already done.

**Returns**

`VolumeClient`

A new client with endpoint, token, and environment settings derived from the [`ConnectClient`](https://docs.chalk.ai/compute-docs#ConnectClient).

### `VolumeClient.close()`
source: https://docs.chalk.ai/compute-docs#VolumeClient.close

Close owned RPC resources and reject subsequent operations.

### `VolumeClient.create(name, volume_type)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.create

Create a new named volume.

**Parameters**

- `name`: `str`
  Name for the new volume.
- `volume_type`: `str | None` = `None`
  Optional volume kind, e.g. `"source_code"` for volumes that hold deployed function source files.

**Returns**

`Volume`

A handle to the newly created volume.

### `VolumeClient.from_name(name)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.from_name

Return a handle by name without validating that it exists.

**Parameters**

- `name`: `str`
  Name of the existing volume.

**Returns**

`Volume`

A handle that fetches metadata lazily on first use.

### `VolumeClient.lookup(name)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.lookup

Look up a volume by name, verifying it exists.

**Parameters**

- `name`: `str`
  Name of the volume.

**Returns**

`Volume`

A handle with metadata pre-fetched.

**Raises**

- `VolumeNotFoundError`: If the volume does not exist.

### `VolumeClient.exists(name)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.exists

Return whether a volume with `name` exists.

Probes `GetVolume` with the default version selector. A not-found response maps to [`False`](https://docs.python.org/3/library/constants.html#False); any other client error propagates so a transient failure is never mistaken for "absent".

### `VolumeClient.upload_lazy_files(image, volume_prefix)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.upload_lazy_files

Upload an image's lazy local files into volumes.

Creates one volume per destination directory and commits files in batches. Returned mounts are pinned to the uploaded version, so writes under them are discarded with the mount.

**Parameters**

- `image`: `Image`
  Image whose lazy local files should be uploaded.
- `volume_prefix`: `str` = `'files'`
  Prefix used when naming the generated volumes.

**Returns**

`builtins.list[VolumeMount]`

One [`VolumeMount`](https://docs.chalk.ai/compute-docs#VolumeMount) per created volume.

### `VolumeClient.stage_lazy_files(image, volume_name)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.stage_lazy_files

Stage an image's lazy local files into source-code volumes.

Reuses (or creates, `source_code`-kind) one stable volume per destination directory — named from `volume_name` — and uploads the file bytes, but does **not** commit. Each staged upload is returned as a typed `chalk.volume.v2.CommitIntent` for `ExternalFunctionVersionSpec.volume_commits`: the server commits the intent and stamps the resulting version_id onto the spec volume with the matching name, so every replica is pinned to that immutable snapshot.

On reused volumes the intent carries a recursive remove of root, which the server applies as "replace the base tree": the committed version is exactly the staged files, so redeploys never leak stale files into new versions.

**Parameters**

- `image`: `Image`
  Image whose lazy local files should be staged.
- `volume_name`: `str`
  Stable name for the volume, typically derived from the function or class name so redeploys append versions to the same volume. A per-directory suffix is added when the image spans multiple destination directories.

**Returns**

`tuple[builtins.list[VolumeMount], builtins.list[pb_v2.CommitIntent]]`

One mount and one commit-intent dict per volume.

### `VolumeClient.delete_volume(name)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.delete_volume

Delete a volume by name.

**Parameters**

- `name`: `str`
  Volume to delete.

### `VolumeClient.list(limit)`
source: https://docs.chalk.ai/compute-docs#VolumeClient.list

List all volumes the caller has access to.

**Parameters**

- `limit`: `int` = `100`
  Page size used when traversing the server-side cursor.

**Returns**

`builtins.list[VolumeInfo]`

One entry per volume.

## `Volume`
source: https://docs.chalk.ai/compute-docs#Volume

Class

A handle to a volume using the v2 volume service.

Volumes expose immutable snapshots behind mutable refs. The Python API supports direct file writes, reads, listing, and version lookup. Each write produces a new immutable version; the `main` ref (or a caller-provided ref) advances to point at the latest committed version.

**Examples**

Write a file, advance the ref, and read the result back:

```python
from chalkcompute import Volume
with Volume("models") as vol:
    vol.put_file("config.yaml", b"lr: 0.01\n")
    vol.read_file("config.yaml")
```

```python
b'lr: 0.01\n'
```

Pin a handle to a previous version and read from it:

```python
previous = vol.at_version(vol.versions()[-2].id)
_ = previous.read_file("config.yaml")
```

**Attributes**

- `name`: `str`
  Volume name.
- `version_id`: `int | None`
  Pinned version id, or [`None`](https://docs.python.org/3/library/constants.html#None) for a ref-tracking handle.
- `ref`: `str`
  Mutable ref tracked by this handle.
- `info`: `VolumeInfo`
  Cached volume metadata, fetched on first access.

  **Returns**

  VolumeInfo Metadata for this volume.
- `version`: `VersionInfo`
  Cached version metadata for the current ref or pinned version.

  **Returns**

  VersionInfo The version this handle currently points at.

  **Raises**

  VolumeError If the volume has no version yet (newly created, no commits).

### `Volume.__init__(name, client, version_id, ref, create_if_missing)`
source: https://docs.chalk.ai/compute-docs#Volume.__init__

Initialize a volume handle.

**Parameters**

- `name`: `str`
  Volume name.
- `client`: `VolumeClient | None` = `None`
  Optional [`VolumeClient`](https://docs.chalk.ai/compute-docs#VolumeClient). If [`None`](https://docs.python.org/3/library/constants.html#None), one is constructed from a default [`ConnectClient`](https://docs.chalk.ai/compute-docs#ConnectClient).
- `version_id`: `int | None` = `None`
  If set, pin this handle to a specific immutable version. Such handles are read-only; mutating operations raise [`VolumeError`](https://docs.chalk.ai/compute-docs#VolumeError).
- `ref`: `str` = `'main'`
  Mutable ref to read/write through (defaults to `"main"`).
- `create_if_missing`: `bool` = `True`
  If [`True`](https://docs.python.org/3/library/constants.html#True) (the default), the volume is created on first access if it does not already exist. Ignored when `version_id` is set.

### `Volume.reload()`
source: https://docs.chalk.ai/compute-docs#Volume.reload

Refresh volume and version metadata from the server.

**Returns**

`VolumeInfo`

Fresh metadata for this volume.

### `Volume.read_file(path)`
source: https://docs.chalk.ai/compute-docs#Volume.read_file

Read a file's contents at the current version.

**Parameters**

- `path`: `str`
  File path within the volume.

**Returns**

`bytes`

The raw file bytes.

### `Volume.put_file(path, data)`
source: https://docs.chalk.ai/compute-docs#Volume.put_file

Write a file, committing a new version on the current ref.

Small payloads are committed inline; large payloads are chunked and uploaded to object storage before the commit completes.

**Parameters**

- `path`: `str`
  Destination path within the volume.
- `data`: `bytes | str`
  File contents ([`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes) or [`str`](https://docs.python.org/3/library/stdtypes.html#str)).

**Returns**

`Volume`

`self`, to allow chaining.

### `Volume.put_file_from_path(path, local_path)`
source: https://docs.chalk.ai/compute-docs#Volume.put_file_from_path

Upload a local file into the volume.

**Parameters**

- `path`: `str`
  Destination path within the volume.
- `local_path`: `str | Path`
  Local file to read and upload.

**Returns**

`Volume`

`self`, to allow chaining.

### `Volume.put_dir(local_path, remote_path)`
source: https://docs.chalk.ai/compute-docs#Volume.put_dir

**Upload a directory, overwriting files without deleting other content.Notes**

Empty directories are omitted. Uploads may make multiple commits. A failed upload can leave earlier commits visible.

**Parameters**

- `local_path`: `str | Path`
  Local directory to upload recursively.
- `remote_path`: `str` = `''`
  Relative destination within the volume. Defaults to its root.

**Returns**

`Volume`

`self`, with its cached version updated after commits complete.

**Raises**

- `ValueError`: If paths are invalid or the directory contains symlinks or special files.
- `VolumeError`: If the volume is closed, pinned, or an upload fails.

### `Volume.load_git_repo(url, dest_prefix, revision)`
source: https://docs.chalk.ai/compute-docs#Volume.load_git_repo

Clone a Git repository and upload its checkout to this volume.

Requires the `git` executable on the local machine. The checkout includes Git metadata so it can be used as a working repository after mounting the volume.

**Parameters**

- `url`: `str`
  Repository URL accepted by `git clone`.
- `dest_prefix`: `str` = `''`
  Destination directory within the volume.
- `revision`: `str` = `'main'`
  Branch, tag, or commit SHA to check out.

### `Volume.remove_file(path)`
source: https://docs.chalk.ai/compute-docs#Volume.remove_file

Remove a file, committing a new version on the current ref.

**Parameters**

- `path`: `str`
  File path within the volume.

**Returns**

`Volume`

`self`, to allow chaining.

### `Volume.listdir(prefix, recursive)`
source: https://docs.chalk.ai/compute-docs#Volume.listdir

List files at the current version.

**Parameters**

- `prefix`: `str` = `''`
  Only return entries under this directory prefix.
- `recursive`: `bool` = `True`
  If [`True`](https://docs.python.org/3/library/constants.html#True) (the default), descend into subdirectories.

**Returns**

`list[FileInfo]`

One entry per matching file or directory.

### `Volume.iterdir(prefix)`
source: https://docs.chalk.ai/compute-docs#Volume.iterdir

Iterate over files at the current version.

**Parameters**

- `prefix`: `str` = `''`
  Only return entries under this directory prefix.

### `Volume.batch_upload(max_files_per_commit)`
source: https://docs.chalk.ai/compute-docs#Volume.batch_upload

Context manager that commits staged writes as one version.

Files staged within the block are bundled into a single commit when the block exits (or when `max_files_per_commit` is reached).

**Parameters**

- `max_files_per_commit`: `int` = `_BATCH_MAX_FILES`
  Maximum files per commit before flushing automatically.

**Examples**

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

### `Volume.load_dataset(source, dest_prefix, timeout, max_attempts, on_progress)`
source: https://docs.chalk.ai/compute-docs#Volume.load_dataset

Copy a Chalk Dataset revision into a volume.

Only the revision's output parquet files are ingested with matching partitioning.

**Parameters**

- `source`: `str | object`
  Revision id or an object with a `revision_id` attribute.
- `dest_prefix`: `str` = `''`
  Optional destination directory within the volume.
- `timeout`: `float` = `600.0`
  Deadline in seconds for each dataset download-URI stream.
- `max_attempts`: `int` = `3`
  Number of full stream passes after staging failures. Each new pass obtains fresh signed URLs and skips files already staged.
- `on_progress`: `Callable[[int, int], None] | None` = `None`
  Optional callback receiving cumulative staged file and byte counts.

**Returns**

`VersionInfo`

The newly committed volume version.

### `Volume.versions(limit)`
source: https://docs.chalk.ai/compute-docs#Volume.versions

List all versions of this volume.

**Parameters**

- `limit`: `int` = `100`
  Page size used when traversing the server-side cursor.

**Returns**

`list[VersionInfo]`

One entry per immutable version, ordered by the server.

### `Volume.latest_version()`
source: https://docs.chalk.ai/compute-docs#Volume.latest_version

Fetch the latest committed version on this handle's ref.

**Returns**

`VersionInfo`

The version currently pointed at by the ref.

**Raises**

- `VolumeError`: If the volume has no committed versions yet.

### `Volume.at_version(version_id)`
source: https://docs.chalk.ai/compute-docs#Volume.at_version

Return a read-only handle pinned to a specific version.

Mutating operations on the returned handle raise [`VolumeError`](https://docs.chalk.ai/compute-docs#VolumeError).

**Parameters**

- `version_id`: `int`
  The immutable version to pin to.

**Returns**

`Volume`

A new handle pointing at `version_id`.

### `Volume.at_ref(name)`
source: https://docs.chalk.ai/compute-docs#Volume.at_ref

Return a mutable handle tracking an existing ref.

**Parameters**

- `name`: `str`
  Name of the ref to open.

**Returns**

`Volume`

A mutable handle tracking `name`.

### `Volume.fork(name)`
source: https://docs.chalk.ai/compute-docs#Volume.fork

Create an independent ref at this handle's current version.

The returned handle reads and writes through the new ref. Forking a version-pinned handle starts exactly at that immutable version.

**Parameters**

- `name`: `str`
  Name for the new ref.

**Returns**

`Volume`

A mutable handle tracking the new ref.

### `Volume.forks()`
source: https://docs.chalk.ai/compute-docs#Volume.forks

List the forks for this volume.

### `Volume.delete_fork()`
source: https://docs.chalk.ai/compute-docs#Volume.delete_fork

Delete this handle's fork without deleting its versions.

**Raises**

- `VolumeError`: If this handle tracks the main ref or is pinned to a version.

### `Volume.mount(mount_path)`
source: https://docs.chalk.ai/compute-docs#Volume.mount

Create a mount specification for this handle.

Fork handles mount their mutable ref. Version-pinned handles mount an immutable snapshot. The default path is `/volumes/<volume-name>`.

### `Volume.delete()`
source: https://docs.chalk.ai/compute-docs#Volume.delete

Delete this volume and all of its versions.

**Raises**

- `VolumeError`: If called on a version-pinned (read-only) handle.

### `Volume.download_to_file(remote_path, local_path)`
source: https://docs.chalk.ai/compute-docs#Volume.download_to_file

Download a file from the current version to a local path.

**Parameters**

- `remote_path`: `str`
  File path within the volume.
- `local_path`: `str | Path`
  Local destination path to write.

### `Volume.download_to_directory(local_dir)`
source: https://docs.chalk.ai/compute-docs#Volume.download_to_directory

Download the current version into a local directory.

**Parameters**

- `local_dir`: `str | Path`
  Local destination directory.

### `Volume.close()`
source: https://docs.chalk.ai/compute-docs#Volume.close

Close the underlying client used by this Volume.

Safe to call multiple times.

### `Volume.__enter__()`
source: https://docs.chalk.ai/compute-docs#Volume.__enter__

Enter a context that closes the volume on exit.

### `Volume.__exit__(_args)`
source: https://docs.chalk.ai/compute-docs#Volume.__exit__

Close the volume on context exit.

## `VolumeInfo`
source: https://docs.chalk.ai/compute-docs#VolumeInfo

Class

Metadata about a volume.

**Attributes**

name Name of the volume. created_at Timestamp when the volume was created.

**Attributes**

- `name`: `str`
- `created_at`: `datetime.datetime | None`
- `volume_id`: `str | None`
- `access_mode`: `str | None`
- `ref`: `str | None`

## `FileInfo`
source: https://docs.chalk.ai/compute-docs#FileInfo

Class

Metadata about a file inside a volume.

**Attributes**

path File path within the volume. size File size in bytes. updated_at Timestamp when the file was last updated.

**Attributes**

- `path`: `str`
- `size`: `int`
- `updated_at`: `datetime.datetime | None`
- `kind`: `str | None`
- `hash`: `str | None`
- `mode`: `int | None`
- `uid`: `int | None`
- `gid`: `int | None`
- `nlink`: `int | None`

## `VolumeError`
source: https://docs.chalk.ai/compute-docs#VolumeError

Exception (bases: `Exception`)

Base exception for volume errors.

## `VolumeNotFoundError`
source: https://docs.chalk.ai/compute-docs#VolumeNotFoundError

Class (bases: `VolumeError`)

Raised when a volume is not found.
