Skip to main content

Filesystems

Filesystems with fsspec

Datasets, prompt templates, and evaluation logs can be stored using either the local filesystem or a remote filesystem. Inspect uses the fsspec package to read and write files, which provides support for a wide variety of filesystems, including:

Support for Amazon S3 is built in to Inspect via the s3fs package. Hugging Face Storage Buckets are supported via the optional huggingface_hub filesystem integration. Other filesystems may require installation of additional packages. See the list of built in filesystems and other known implementations for all supported storage back ends.

See Custom Filesystems below for details on implementing your own fsspec compatible filesystem as a storage back-end.

Filesystem Functions

The following Inspect API functions use fsspec:

For example, to use S3 you would prefix your paths with s3://:

# read a prompt template from s3
prompt_template("s3://inspect-prompts/ctf.txt")

# read a dataset from S3
csv_dataset("s3://inspect-datasets/ctf-12.csv")

# read eval logs from S3
list_eval_logs("s3://my-s3-inspect-log-bucket")

# read eval logs from a Hugging Face Storage Bucket
list_eval_logs("hf://buckets/my-org/inspect-logs")

Custom Filesystems

See the fsspec developer documentation for details on implementing a custom filesystem. Note that if your implementation is only for use with Inspect, you need to implement only the subset of the fsspec API used by Inspect. The properties and methods used by Inspect include:

  • sep
  • open()
  • makedirs()
  • info()
  • created()
  • exists()
  • ls()
  • walk()
  • unstrip_protocol()
  • invalidate_cache()

As with Model APIs and Sandbox Environments, fsspec filesystems should be registered using a setuptools entry point. For example, if your package is named evaltools and you have implemented a myfs:// filesystem using the MyFs class exported from the root of the package, you would register it like this in pyproject.toml:

[project.entry-points."fsspec.specs"]
myfs = "evaltools:MyFs"
[project.entry-points."fsspec.specs"]
myfs = "evaltools:MyFs"
[tool.poetry.plugins."fsspec.specs"]
myfs = "evaltools:MyFs"

Once this package is installed, you’ll be able to use myfs:// with Inspect without any further registration.

Streaming Large Files

Some callers read part of a file as a stream, for example a member of a zipped eval log that is parsed as it arrives. On an async filesystem (one that derives from fsspec’s AsyncFileSystem), Inspect serves such a read by calling _cat_file() once for the whole range and holding the result in memory.

A filesystem can have Inspect fetch the range in 8 MiB pieces instead, as the caller consumes them, by defining one optional method that binds a reader to a single version of the file:

async def _pinned_range_reader(
    self, path: str
) -> tuple[int, Callable[[int, int], Coroutine[Any, Any, bytes]]]:
    info = await self._info(path)
    return info["size"], functools.partial(self._read_version, path, info["etag"])

The method returns the size of the version it bound, and an async function read(start, end) that:

  • Returns exactly the bytes [start, end) of that version, where 0 <= start < end <= size. Inspect raises OSError for any other length.
  • Has the storage service enforce the version on every request, the first included. A metadata check followed by an unconditional request is not enough.
  • Raises if that version is no longer available. An OSError subclass that names the file is recommended; Inspect passes the error on unchanged.

Inspect also reads through the reader when it downloads a whole log to a temporary file, so the download is of one version. Reads that Inspect needs whole, such as a ZIP directory or a member read into memory, still call _cat_file() once.

Inspect calls the method with the path stripped of its protocol, and runs both it and read on the filesystem’s own event loop, one call at a time per reader. It may repeat a range after a cancelled read, may hold a reader for a long time, and never closes one. A missing file should raise FileNotFoundError from the method.

The method is not consulted for s3://, http://, https:// or reference:// paths, nor for a filesystem wrapped in dir://.