Skip to content

inspect_evals_utils.datasets

Load JSON and CSV datasets, caching remote files and retrying transient download errors.

Remote files are downloaded once into the shared cache directory (see :mod:inspect_evals_utils.cache) and read from there on later runs.

DatasetLoader module-attribute

DatasetLoader = Callable[..., Dataset]

SUPPORTED_REMOTE_URL_PROTOCOLS module-attribute

SUPPORTED_REMOTE_URL_PROTOCOLS = {
    "https",
    "http",
    "s3",
    "gs",
    "gcs",
    "az",
    "abfs",
}

LOCAL_PROTOCOL module-attribute

LOCAL_PROTOCOL = {'', 'c', 'd', 'e', 'file'}

TRANSIENT_ERRORS module-attribute

TRANSIENT_ERRORS = {408, 429, 500, 502, 503, 504}

is_remote_url

is_remote_url(file_path: str) -> bool

get_cached_fname

get_cached_fname(url: str, tag: str = '') -> str

Generate cache filename with optional tag prefix and URL hash.

Parameters:

Name Type Description Default
url str

The URL to hash

required
tag str

Optional human-readable tag prefix (empty string for no tag)

''

Returns:

Type Description
str

Filename in format: {tag}_{hash}.{ext} or {hash}.{ext}

is_transient_error

is_transient_error(e: Exception) -> bool

Check if error is transient and should be retried.

Handles both HTTP errors from requests and network errors from fsspec.

cache_dataset_loader

cache_dataset_loader() -> Callable[
    [Callable[Concatenate[str, P], Dataset]],
    Callable[Concatenate[str, P], Dataset],
]

Decorator factory that adds caching functionality to dataset loader functions.

The cache directory is inferred from the format_extension parameter.

Returns:

Type Description
Callable[[Callable[Concatenate[str, P], Dataset]], Callable[Concatenate[str, P], Dataset]]

A decorator that wraps dataset loaders with caching logic

load_json_dataset

load_json_dataset(
    file_path: str,
    eval_name: str,
    sample_fields: Any = None,
    auto_id: bool = False,
    shuffle: bool = False,
    seed: int | None = None,
    limit: int | bool | None = None,
    epoch: int | None = None,
    name: str = "",
    fs_options: dict[str, Any] | None = None,
    *,
    encoding: str = "utf-8",
    refresh: bool = False,
    cache_tag: str | None = None,
    **reader_kwargs: Any,
) -> Dataset

Load a JSON or JSONL dataset with caching support for remote URLs.

Parameters:

Name Type Description Default
file_path str

Local path or remote URL to the JSON/JSONL file (extension auto-detected)

required
eval_name str

Evaluation name (organizes cache directory)

required
sample_fields Any

Field specification or converter function for samples

None
auto_id bool

Automatically generate sample IDs

False
shuffle bool

Shuffle the dataset

False
seed int | None

Random seed for shuffling

None
limit int | bool | None

Maximum number of samples to load

None
epoch int | None

Epoch number for sampling

None
name str

Dataset name

''
fs_options dict[str, Any] | None

Filesystem options for remote URLs

None
encoding str

Text encoding of the source file. Remote files are cached as UTF-8.

'utf-8'
refresh bool

Force re-download of cached remote files

False
cache_tag str | None

Optional human-readable tag for cache filename

None
reader_kwargs Any

Optional JSON reader options.

{}

Returns:

Type Description
Dataset

Dataset object

load_csv_dataset

load_csv_dataset(
    file_path: str,
    eval_name: str,
    sample_fields: Any = None,
    auto_id: bool = False,
    shuffle: bool = False,
    seed: int | None = None,
    limit: int | bool | None = None,
    epoch: int | None = None,
    name: str = "",
    fieldnames: list[str] | None = None,
    dialect: str = "unix",
    delimiter: str | None = None,
    encoding: str = "utf-8",
    fs_options: dict[str, Any] | None = None,
    shuffle_choices: bool | int = False,
    *,
    refresh: bool = False,
    cache_tag: str | None = None,
) -> Dataset

Load a CSV dataset with caching support for remote URLs.

Parameters:

Name Type Description Default
file_path str

Local path or remote URL to the CSV file (extension auto-detected)

required
eval_name str

Evaluation name (organizes cache directory)

required
sample_fields Any

Field specification or converter function for samples

None
auto_id bool

Automatically generate sample IDs

False
shuffle bool

Shuffle the dataset

False
seed int | None

Random seed for shuffling

None
limit int | bool | None

Maximum number of samples to load

None
epoch int | None

Epoch number for sampling

None
name str

Dataset name

''
fieldnames list[str] | None

CSV field names

None
dialect str

CSV dialect

'unix'
delimiter str | None

CSV delimiter character

None
encoding str

Text encoding of the source file. Remote files are cached as UTF-8.

'utf-8'
fs_options dict[str, Any] | None

Filesystem options for remote URLs

None
shuffle_choices bool | int

Shuffle multiple choice options. An int is used as the shuffle seed (as in inspect_ai's csv_dataset); True shuffles unseeded.

False
refresh bool

Force re-download of cached remote files

False
cache_tag str | None

Optional human-readable tag for cache filename

None

Returns:

Type Description
Dataset

Dataset object