psf/requests · error · InvalidSchema

No connection adapters were found for

Error message

No connection adapters were found for {url!r}

What it means

Raised by Session.get_adapter() when no registered adapter prefix matches the URL scheme. Requests ships with adapters for 'http://' and 'https://' only. Any other scheme (or a malformed URL missing the scheme) triggers this InvalidSchema error.

Solutions

  1. Ensure the URL starts with 'http://' or 'https://'.
  2. Mount a custom adapter if you need a non-standard scheme: session.mount('ftp://', MyAdapter()).
  3. Validate/sanitize URLs before passing to requests: prepend 'https://' if no scheme is present.

Example fix

# before
requests.get('example.com/api')  # InvalidSchema

# after
requests.get('https://example.com/api')

# or sanitize
from urllib.parse import urlparse
if not urlparse(url).scheme:
    url = 'https://' + url
Defensive patterns

Strategy: validation

Validate before calling

from urllib.parse import urlparse
parsed = urlparse(url)
if parsed.scheme not in ('http', 'https'):
    if not parsed.scheme:
        url = 'https://' + url
    else:
        raise ValueError(f'Unsupported scheme: {parsed.scheme}')
resp = requests.get(url)

Type guard

from urllib.parse import urlparse
def has_valid_http_scheme(url: str) -> bool:
    return urlparse(url).scheme in ('http', 'https')

Try / catch

try:
    resp = requests.get(url)
except requests.exceptions.InvalidSchema as e:
    # Prepend scheme and retry
    url = 'https://' + url
    resp = requests.get(url)

Prevention

When it happens

Trigger: Requesting a URL with scheme 'ftp://', 'file://', 'ws://', or a custom protocol; passing a URL without a scheme like 'example.com/path' (no http:// prefix); passing a URL with a typo like 'htp://'.

Common situations: User input missing the 'https://' prefix; configuration files storing URLs without schemes; attempting to access non-HTTP protocols; typos in URL strings; passing relative paths instead of absolute URLs.

Related errors


AI-assisted analysis of psf/requests@8068356288 (2026-08-11). Data as JSON: /api/errors/8b7d06f4fad34a22. Report an issue: GitHub.

Appendix: source

Thrown at src/requests/sessions.py:881

        proxies = merge_setting(proxies, self.proxies)
        stream = merge_setting(stream, self.stream)
        verify = merge_setting(verify, self.verify)
        cert = merge_setting(cert, self.cert)

        return {"proxies": proxies, "stream": stream, "verify": verify, "cert": cert}

    def get_adapter(self, url: str) -> BaseAdapter:
        """
        Returns the appropriate connection adapter for the given URL.

        :rtype: requests.adapters.BaseAdapter
        """
        for prefix, adapter in self.adapters.items():
            if url.lower().startswith(prefix.lower()):
                return adapter

        # Nothing matches :-/
        raise InvalidSchema(f"No connection adapters were found for {url!r}")

    def close(self) -> None:
        """Closes all adapters and as such the session"""
        for v in self.adapters.values():
            v.close()

    def mount(self, prefix: str, adapter: BaseAdapter) -> None:
        """Registers a connection adapter to a prefix.

        Adapters are sorted in descending order by prefix length.
        """
        self.adapters[prefix] = adapter
        keys_to_move = [k for k in self.adapters if len(k) < len(prefix)]

        for key in keys_to_move:
            self.adapters[key] = self.adapters.pop(key)

    def __getstate__(self) -> dict[str, Any]:

View on GitHub (pinned to 8068356288)