Skip to content

inspect_evals_utils.huggingface

Hugging Face wrappers that retry on rate limits and transient errors, and require a pinned revision=.

Needs the huggingface extra: uv add "inspect-evals-utils[huggingface]".

Jitterer module-attribute

Jitterer = Callable[[float], float]

HF_RATE_LIMIT_WINDOW_MINS module-attribute

HF_RATE_LIMIT_WINDOW_MINS = 5

STANDARD_BACKOFF_CONFIG module-attribute

STANDARD_BACKOFF_CONFIG: BackoffConfig = {
    "max_tries": 3,
    "initial_wait_mins": 1,
    "max_wait_mins": HF_RATE_LIMIT_WINDOW_MINS,
    "jitter": backoff.full_jitter,
}

CI_BACKOFF_CONFIG module-attribute

CI_BACKOFF_CONFIG: BackoffConfig = {
    "max_tries": 5,
    "initial_wait_mins": 1,
    "max_wait_mins": HF_RATE_LIMIT_WINDOW_MINS,
    "jitter": backoff.full_jitter,
}

BACKOFF_CONFIG module-attribute

BACKOFF_CONFIG = _default_backoff_config()

TRANSIENT_ERROR_CODES module-attribute

TRANSIENT_ERROR_CODES = {
    429: "Too many requests... likely the HF rate limit policy has been violated",
    502: "Bad gateway... likely a transient server-side / network issue",
    503: "Service unavailable... likely a transient server overload or maintenance",
    504: "Gateway time-out... likely a transient upstream server delay",
}

BackoffConfig

Bases: TypedDict

max_tries instance-attribute

max_tries: int

initial_wait_mins instance-attribute

initial_wait_mins: int

max_wait_mins instance-attribute

max_wait_mins: int

jitter instance-attribute

jitter: Jitterer | None

set_backoff_config

set_backoff_config(config: BackoffConfig | None) -> None

Set the retry policy every wrapper uses from its next call on.

Pass None to go back to the default: CI_BACKOFF_CONFIG when the CI environment variable is set, otherwise STANDARD_BACKOFF_CONFIG.

get_backoff_config

get_backoff_config() -> BackoffConfig

The retry policy the wrappers will use for their next call.

set_hf_call_recorder

set_hf_call_recorder(
    recorder: Callable[[str], None] | None,
) -> None

Register a callback told the wrapper name before each Hugging Face call.

Test sessions use this to count calls per test. Pass None to remove it.

set_backoff_recorder

set_backoff_recorder(
    recorder: Callable[[Details], None] | None,
) -> None

Register a callback given the backoff details dict on each retry wait.

The dict has the keys backoff.on_exception passes to on_backoff, such as wait (seconds) and tries. Pass None to remove it.

record_hf_call

record_hf_call(func_name: str) -> None

hf_backoff_policy

hf_backoff_policy(
    max_tries: int,
    initial_wait_mins: int,
    max_wait_mins: int,
    jitter: Jitterer | None,
) -> Callable[[Callable[P, R]], Callable[P, R]]

Add retries to functions that interact with HF servers.

See HF rate limits: https://huggingface.co/docs/hub/rate-limits#rate-limit-tiers

See Adding Jitter: https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/

should_retry

should_retry(err: Exception) -> bool

Check if the exception should be retried.

huggingface_hub.errors.HfHubHTTPError could signal that the rate limit policy has been violated, or that there is a temporary server side issue for 429, 502, 503, and 504 responses respectively.

huggingface_hub.errors.LocalEntryNotFoundError should be retried because once the rate limit is applied, HF function(s) attempt to check in the local cache when the internet is not working (because of the rate limit).

FileNotFoundError should be retried if it relates to internet being down (due to rate limit) and not finding in local cache. See: https://github.com/huggingface/datasets/blob/0ec4d87dfe0a3545308a8c69459b5ed9aff440fb/src/datasets/load.py#L1032

In rare circumstances, ValueError can be raised from a non-huggingface module, which does not get caught and handled by any huggingface modules. So it should also be retried.

requests.exceptions.ReadTimeout should be retried as it can be raised when the huggingface servers are temporarily unavailable.

httpx.TimeoutException and httpx.HTTPStatusError are the equivalent failure modes for the httpx-based HTTP layer that huggingface_hub 1.x and datasets use internally for downloads. These slip past the requests.exceptions.ReadTimeout / HfHubHTTPError checks because they come from a different exception hierarchy.

get_dataset_split_names

get_dataset_split_names(
    *args: Any, **kwargs: Any
) -> list[str]

Call datasets.get_dataset_split_names with exponential backoff and retry

hf_dataset

hf_dataset(*args: Any, **kwargs: Any) -> Dataset

Call inspect_ai.dataset.hf_dataset with exponential backoff and retry

load_dataset

load_dataset(*args: Any, **kwargs: Any) -> Dataset

Call datasets.load_dataset with exponential backoff and retry

snapshot_download

snapshot_download(*args: Any, **kwargs: Any) -> Any

Call huggingface_hub.snapshot_download with exponential backoff and retry

hf_hub_download

hf_hub_download(*args: Any, **kwargs: Any) -> Any

Call huggingface_hub.hf_hub_download with exponential backoff and retry

sentence_transformer

sentence_transformer(*args: Any, **kwargs: Any) -> Any

Call sentence_transformers.SentenceTransformer with exponential backoff and retry.

transformers_pipeline

transformers_pipeline(*args: Any, **kwargs: Any) -> Any

Call transformers.pipeline with exponential backoff and retry.

auto_model_for_sequence_classification

auto_model_for_sequence_classification(
    *args: Any, **kwargs: Any
) -> Any

Call transformers.AutoModelForSequenceClassification.from_pretrained with exponential backoff and retry.

auto_tokenizer

auto_tokenizer(*args: Any, **kwargs: Any) -> Any

Call transformers.AutoTokenizer.from_pretrained with exponential backoff and retry.