Get gaierror when trying to access HF Token for login

Hi there. I’m trying to login from my Kaggle notebook (in web), using my Hugging Face token but I get ‘gaierror’.

I’ve looked at Stack and the other usuals, but no bueno…

My code snippets in Kaggle so far are:

import os
from getpass import getpass
from huggingface_hub import login

HF_TOKEN = getpass()
I enter my token here

login(token=HF_TOKEN)

I then get the following:
The token has not been saved to the git credentials helper. Pass add_to_git_credential=True in this function directly or --add-to-git-credential if using via huggingface-cli if you want to set the git credential as well.
---------------------------------------------------------------------------
gaierror Traceback (most recent call last)
File /opt/conda/lib/python3.10/site-packages/urllib3/connection.py:198, in HTTPConnection._new_conn(self)

  • 197 try:*
    → 198 sock = connection.create_connection(
  • 199 (self._dns_host, self.port),*
  • 200 self.timeout,*
  • 201 source_address=self.source_address,*
  • 202 socket_options=self.socket_options,*
  • 203 )*
  • 204 except socket.gaierror as e:*

File /opt/conda/lib/python3.10/site-packages/urllib3/util/connection.py:60, in create_connection(address, timeout, source_address, socket_options)

  • 58     raise LocationParseError(f"'{host}', label empty or too long") from None*
    

—> 60 for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):

  • 61     af, socktype, proto, canonname, sa = res*
    

File /opt/conda/lib/python3.10/socket.py:955, in getaddrinfo(host, port, family, type, proto, flags)

  • 954 addrlist = *
    → 955 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
  • 956 af, socktype, proto, canonname, sa = res*

gaierror: [Errno -3] Temporary failure in name resolution

The above exception was the direct cause of the following exception:

NameResolutionError Traceback (most recent call last)
*File /opt/conda/lib/python3.10/site-packages/urllib3/connectionpool.py:793, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, *response_kw)

  • 792 # Make the request on the HTTPConnection object*
    → 793 response = self._make_request(
  • 794 conn,*
  • 795 method,*
  • 796 url,*
  • 797 timeout=timeout_obj,*
  • 798 body=body,*
  • 799 headers=headers,*
  • 800 chunked=chunked,*
  • 801 retries=retries,*
  • 802 response_conn=response_conn,*
  • 803 preload_content=preload_content,*
  • 804 decode_content=decode_content,*
  • 805 *response_kw,
  • 806 )*
  • 808 # Everything went great!*

File /opt/conda/lib/python3.10/site-packages/urllib3/connectionpool.py:491, in HTTPConnectionPool._make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)

  • 490 new_e = _wrap_proxy_error(new_e, conn.proxy.scheme)*
    → 491 raise new_e
  • 493 # conn.request() calls http.client..request, not the method in
  • 494 # urllib3.request. It also calls makefile (recv) on the socket.*

File /opt/conda/lib/python3.10/site-packages/urllib3/connectionpool.py:467, in HTTPConnectionPool._make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)

  • 466 try:*
    → 467 self._validate_conn(conn)
  • 468 except (SocketTimeout, BaseSSLError) as e:*

File /opt/conda/lib/python3.10/site-packages/urllib3/connectionpool.py:1099, in HTTPSConnectionPool._validate_conn(self, conn)

File /opt/conda/lib/python3.10/site-packages/urllib3/connection.py:616, in HTTPSConnection.connect(self)

  • 615 sock: socket.socket | ssl.SSLSocket*
    → 616 self.sock = sock = self._new_conn()
  • 617 server_hostname: str = self.host*

File /opt/conda/lib/python3.10/site-packages/urllib3/connection.py:205, in HTTPConnection._new_conn(self)

  • 204 except socket.gaierror as e:*
    → 205 raise NameResolutionError(self.host, self, e) from e
  • 206 except SocketTimeout as e:*

NameResolutionError: <urllib3.connection.HTTPSConnection object at 0x7b86b8725600>: Failed to resolve ‘huggingface.co’ ([Errno -3] Temporary failure in name resolution)

The above exception was the direct cause of the following exception:

MaxRetryError Traceback (most recent call last)
File /opt/conda/lib/python3.10/site-packages/requests/adapters.py:667, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)

  • 666 try:*
    → 667 resp = conn.urlopen(
  • 668 method=request.method,*
  • 669 url=url,*
  • 670 body=request.body,*
  • 671 headers=request.headers,*
  • 672 redirect=False,*
  • 673 assert_same_host=False,*
  • 674 preload_content=False,*
  • 675 decode_content=False,*
  • 676 retries=self.max_retries,*
  • 677 timeout=timeout,*
  • 678 chunked=chunked,*
  • 679 )*
  • 681 except (ProtocolError, OSError) as err:*

*File /opt/conda/lib/python3.10/site-packages/urllib3/connectionpool.py:847, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, *response_kw)

  • 845 new_e = ProtocolError(“Connection aborted.”, new_e)*
    → 847 retries = retries.increment(
  • 848 method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2]*
  • 849 )*
  • 850 retries.sleep()*

File /opt/conda/lib/python3.10/site-packages/urllib3/util/retry.py:515, in Retry.increment(self, method, url, response, error, _pool, _stacktrace)

  • 514 reason = error or ResponseError(cause)*
    → 515 raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type]
  • 517 log.debug(“Incremented Retry for (url=‘%s’): %r”, url, new_retry)*

MaxRetryError: HTTPSConnectionPool(host=‘huggingface.co’, port=443): Max retries exceeded with url: /api/whoami-v2 (Caused by NameResolutionError(“<urllib3.connection.HTTPSConnection object at 0x7b86b8725600>: Failed to resolve ‘huggingface.co’ ([Errno -3] Temporary failure in name resolution)”))

During handling of the above exception, another exception occurred:

ConnectionError Traceback (most recent call last)
Cell In[5], line 1
----> 1 login(token=HF_TOKEN)

File /opt/conda/lib/python3.10/site-packages/huggingface_hub/_login.py:111, in login(token, add_to_git_credential, new_session, write_permission)

  • 104 if not add_to_git_credential:*
  • 105 print(*
  • 106 "The token has not been saved to the git credentials helper. Pass "*
  • 107 "add_to_git_credential=True in this function directly or "*
  • 108 "--add-to-git-credential if using via huggingface-cli if "*
  • 109 “you want to set the git credential as well.”*
  • 110 )*
    → 111 _login(token, add_to_git_credential=add_to_git_credential, write_permission=write_permission)
  • 112 elif is_notebook():*
  • 113 notebook_login(new_session=new_session, write_permission=write_permission)*

File /opt/conda/lib/python3.10/site-packages/huggingface_hub/_login.py:305, in _login(token, add_to_git_credential, write_permission)

  • 302 if token.startswith(“api_org”):*
  • 303 raise ValueError(“You must use your personal account token, not an organization token.”)*
    → 305 permission = get_token_permission(token)
  • 306 if permission is None:*
  • 307 raise ValueError(“Invalid token passed!”)*

File /opt/conda/lib/python3.10/site-packages/huggingface_hub/hf_api.py:1625, in HfApi.get_token_permission(self, token)

  • 1608 “”"*
  • 1609 Check if a given token is valid and return its permissions.*
  • 1610 *
  • (…)*
  • 1622 token passed or token is invalid.*
  • 1623 “”"*
  • 1624 try:*
    → 1625 return self.whoami(token=token)[“auth”][“accessToken”][“role”]
  • 1626 except (LocalTokenNotFoundError, HTTPError):*
  • 1627 return None*

*File /opt/conda/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py:114, in validate_hf_hub_args.._inner_fn(*args, *kwargs)

  • 111 if check_use_auth_token:*
  • 112 kwargs = smoothly_deprecate_use_auth_token(fn_name=fn.name, has_token=has_token, kwargs=kwargs)*
    *–> 114 return fn(*args, *kwargs)

File /opt/conda/lib/python3.10/site-packages/huggingface_hub/hf_api.py:1587, in HfApi.whoami(self, token)

  • 1575 @validate_hf_hub_args*
  • 1576 def whoami(self, token: Union[bool, str, None] = None) → Dict:*
  • 1577 “”"*
  • 1578 Call HF API to know “whoami”.*
  • 1579 *
  • (…)*
  • 1585 To disable authentication, pass False.*
  • 1586 “”"*
    → 1587 r = get_session().get(
  • 1588 f"{self.endpoint}/api/whoami-v2",*
  • 1589 headers=self._build_hf_headers(*
  • 1590 # If token is provided and not None, it will be used by default.*
  • 1591 # Otherwise, the token must be retrieved from cache or env variable.*
  • 1592 token=(token or self.token or True),*
  • 1593 ),*
  • 1594 )*
  • 1595 try:*
  • 1596 hf_raise_for_status(r)*

*File /opt/conda/lib/python3.10/site-packages/requests/sessions.py:602, in Session.get(self, url, *kwargs)

  • 594 r"""Sends a GET request. Returns :class:Response object.*
  • 595 *
  • 596 :param url: URL for the new :class:Request object.*
  • 597 :param **kwargs: Optional arguments that request takes.*
  • 598 :rtype: requests.Response*
  • 599 “”"*
  • 601 kwargs.setdefault(“allow_redirects”, True)*
    *–> 602 return self.request(“GET”, url, *kwargs)

File /opt/conda/lib/python3.10/site-packages/requests/sessions.py:589, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)

  • 584 send_kwargs = {*
  • 585 “timeout”: timeout,*
  • 586 “allow_redirects”: allow_redirects,*
  • 587 }*
  • 588 send_kwargs.update(settings)*
    *–> 589 resp = self.send(prep, *send_kwargs)
  • 591 return resp*

*File /opt/conda/lib/python3.10/site-packages/requests/sessions.py:703, in Session.send(self, request, *kwargs)

  • 700 start = preferred_clock()*
  • 702 # Send the request*
    *–> 703 r = adapter.send(request, *kwargs)
  • 705 # Total elapsed time of the request (approximately)*
  • 706 elapsed = preferred_clock() - start*

*File /opt/conda/lib/python3.10/site-packages/huggingface_hub/utils/_http.py:93, in UniqueRequestIdAdapter.send(self, request, *args, *kwargs)

  • 91 """Catch any RequestException to append request id to the error message for debugging."""*
    
  • 92 try:*
    

*—> 93 return super().send(request, *args, *kwargs)

  • 94 except requests.RequestException as e:*
    
  • 95     request_id = request.headers.get(X_AMZN_TRACE_ID)*
    

File /opt/conda/lib/python3.10/site-packages/requests/adapters.py:700, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)

  • 696 if isinstance(e.reason, _SSLError):*
  • 697 # This branch is for urllib3 v1.22 and later.*
  • 698 raise SSLError(e, request=request)*
    → 700 raise ConnectionError(e, request=request)
  • 702 except ClosedPoolError as e:*
  • 703 raise ConnectionError(e, request=request)*

ConnectionError: (MaxRetryError(‘HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /api/whoami-v2 (Caused by NameResolutionError(“<urllib3.connection.HTTPSConnection object at 0x7b86b8725600>: Failed to resolve 'huggingface.co' ([Errno -3] Temporary failure in name resolution)”))’), ‘(Request ID: 5e5d185c-79b7-44e5-bfa0-d74427e21610)’)

1 Like

It seems that gaierror is an error in the address information acquisition function (get_address_info), but the overall error appears to be an SSL configuration error.

1 Like

I switched to Colab notebook, and seems to work ok now. Think it might be stringent security settings on work pc…

1 Like