ultralytics/yolov5 · error · ValueError

Could not resolve hostname '{hostname}': {e}

Error message

Could not resolve hostname '{hostname}': {e}

What it means

_validate_ssrf_url raises ValueError when socket.getaddrinfo fails for the URL's hostname, i.e. DNS cannot resolve the name at all. This guard runs before every fetch in _request_ssrf_url (including each redirect hop) for remote weight/dataset downloads; a gaierror means the hostname is typo'd, the DNS server is unreachable, or the host genuinely does not exist. The wrapper turns what would be a socket error into ValueError with the hostname spelled out.

Source

Thrown at models/common.py:823

        triton = not any(types) and all([any(s in url.scheme for s in ["http", "grpc"]), url.netloc])
        return [*types, triton]

    @staticmethod
    def _load_metadata(f=Path("path/to/meta.yaml")):
        """Loads metadata from a YAML file, returning strides and names if the file exists, otherwise `None`."""
        if f.exists():
            d = yaml_load(f)
            return d["stride"], d["names"]  # assign stride, names
        return None, None


def _validate_ssrf_url(url: str) -> None:
    """Raise ValueError if url resolves to any private/internal address."""
    hostname = urlparse(url).hostname or ""
    try:
        results = socket.getaddrinfo(hostname, None)
    except socket.gaierror as e:
        raise ValueError(f"Could not resolve hostname '{hostname}': {e}") from e
    for _family, _type, _proto, _canonname, sockaddr in results:
        addr = ipaddress.ip_address(sockaddr[0])
        if addr.is_private or addr.is_loopback or addr.is_link_local or addr.is_reserved or addr.is_multicast:
            raise ValueError(f"Blocked request to internal address: {addr}")


def _request_ssrf_url(url: str, max_redirects: int = 5):
    """Fetch a URL after validating each resolved redirect target."""
    session = requests.Session()
    for _ in range(max_redirects + 1):
        _validate_ssrf_url(url)
        response = session.get(url, stream=True, allow_redirects=False)
        if not response.is_redirect:
            return response
        url = urljoin(response.url, response.headers["location"])
        response.close()
    raise ValueError(f"Too many redirects while fetching {url}")

View on GitHub (pinned to 20d1d78a08)

Solutions

  1. Check the URL string for typos and confirm the host resolves: python -c "import socket; print(socket.getaddrinfo('example.com', None))".
  2. If offline, download the file on a connected machine and pass the local path instead of the URL.
  3. Fix container/host DNS (resolv.conf, corporate proxy settings) if unrelated hostnames also fail.
  4. If the failure is on a redirect hop, inspect the server's Location header — the final host may be misspelled or dead.

Example fix

# before
attempt_download('https://githib.com/ultralytics/yolov5/releases/download/v7.0/yolov5s.pt')

# after
attempt_download('https://github.com/ultralytics/yolov5/releases/download/v7.0/yolov5s.pt')
Defensive patterns

Strategy: validation

Validate before calling

import socket
from urllib.parse import urlparse

def hostname_resolves(url: str) -> bool:
    host = urlparse(url).hostname or ''
    try:
        socket.getaddrinfo(host, None)
        return True
    except socket.gaierror:
        return False

Try / catch

try:
    attempt_download(url)
except ValueError as e:
    if 'Could not resolve hostname' in str(e):
        raise SystemExit(f'Check URL/DNS for {url}') from e

Prevention

When it happens

Trigger: attempt_download of a weights URL with a typo'd host (e.g. 'https://githib.com/...'); running fully offline with no DNS; a download URL whose domain expired; a redirect Location header pointing at a dead host.

Common situations: Air-gapped or proxy-only environments where DNS for public hosts fails; copy-pasted URLs with transcription errors; container images with broken /etc/resolv.conf.

Understand the failure class

Related errors


AI-assisted analysis of ultralytics/yolov5@20d1d78a08 (2026-08-15). Data as JSON: /api/errors/8d1abbf48fe6a1e5. Report an issue: GitHub.