From d7eac20805aca37f6c789529d6735f6da2bf7401 Mon Sep 17 00:00:00 2001 From: George Panchuk Date: Fri, 25 Sep 2026 01:20:55 +0700 Subject: [PATCH] fix: extend the pool of network errors caught during download --- fastembed/common/model_management.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/fastembed/common/model_management.py b/fastembed/common/model_management.py index d044559d..164efbcd 100644 --- a/fastembed/common/model_management.py +++ b/fastembed/common/model_management.py @@ -27,6 +27,27 @@ _DOWNLOAD_CHUNK_SIZE = 256 * 1024 +def _hf_transport_errors() -> tuple[type[Exception], ...]: + """Network errors of huggingface_hub's HTTP library that aren't OSError. + + A refused connection, a DNS failure or a timeout raises an OSError in huggingface_hub 0.x, + which is built on requests, but a TransportError in 1.x (httpx) and 2.x (httpx2). + """ + try: + # huggingface_hub>=1.30 re-exports whichever of httpx and httpx2 it's built on. + from huggingface_hub.utils import httpx + except ImportError: + try: + import httpx # huggingface_hub 1.0 to 1.29 + except ImportError: # huggingface_hub 0.x + return () + return (httpx.TransportError,) + + +# Errors from an HF download that download_model handles by falling back to url and retrying. +_HF_DOWNLOAD_ERRORS = (OSError, RepositoryNotFoundError, ValueError) + _hf_transport_errors() + + class ModelManagement(Generic[T]): METADATA_FILE = "files_metadata.json" @@ -502,7 +523,7 @@ def download_model(cls, model: T, cache_dir: str, retries: int = 3, **kwargs: An **kwargs, ) ) - except (EnvironmentError, RepositoryNotFoundError, ValueError) as e: + except _HF_DOWNLOAD_ERRORS as e: if not local_files_only: logger.error( f"Could not download model from HuggingFace: {e} "