How to Solve "requests.exceptions.InvalidSchema: No connection adapters were found"
The requests.exceptions.InvalidSchema: No connection adapters were found for
error in Python, when using the requests
library, indicates a problem with the URL you're trying to access.
This guide explains the common causes of this error, focusing on:
- Incorrect URL format (missing scheme, special characters).
- Passing the wrong data type to
requests.get()
. - Accidental tuple creation.
We'll provide clear solutions for each cause.
Understanding the Error
The requests
library is designed to make HTTP requests. It needs a valid URL to know where to send the request and how to connect. The InvalidSchema
error means the URL you provided doesn't have a recognizable protocol scheme (like http://
or https://
). requests
doesn't know how to handle the request without this information.
Common Causes and Solutions
Incorrect URL Type
The requests.get()
function (and other requests
methods like post()
, put()
, etc.) expects a string as the URL argument. Passing anything else (like a list, tuple, or dictionary) will cause an error.
import requests
# ⛔️ INCORRECT: Passing a list
# url = ['https://jsonplaceholder.typicode.com/posts']
# res = requests.get(url, timeout=10) # Raises InvalidSchema
# ✅ CORRECT: Passing a string
url = 'https://jsonplaceholder.typicode.com/posts'
res = requests.get(url, timeout=10)
parsed = res.json()
print(parsed)
Always ensure your URL is a string. If it's stored in another data structure, extract it before passing it to requests.get()
:
import requests
# Correct example
url = ['https://jsonplaceholder.typicode.com/posts']
res = requests.get(url[0], timeout=10) # Access the first element to pass a string.
parsed = res.json()
print(parsed)
Missing URL Scheme (http:// or https://)
The most common cause of InvalidSchema
is omitting the protocol scheme (http:// or https://):
import requests
# ⛔️ INCORRECT: Missing "https://"
# res = requests.get('jsonplaceholder.typicode.com/posts', timeout=10)
# ✅ CORRECT: Include the scheme
res = requests.get('https://jsonplaceholder.typicode.com/posts', timeout=10)
parsed = res.json()
print(parsed)
- Always include
http://
orhttps://
(or another valid protocol) at the beginning of your URL.requests
needs this to determine how to connect. - Also, make sure that the protocol is spelled in lowercase letters.
Invalid Characters in the URL
URLs can't contain certain characters directly. Newlines, spaces (in some contexts), and quotes are common culprits:
import requests
# ⛔️ INCORRECT: Newline characters in the URL
# url = """
# https://jsonplaceholder.typicode.com/posts
# """
# res = requests.get(url, timeout=10)
# ✅ CORRECT: Remove newlines and extra spaces
url = "https://jsonplaceholder.typicode.com/posts"
res = requests.get(url, timeout=10)
parsed = res.json()
print(parsed)
Solution: Ensure your URL string is properly formatted:
- No leading/trailing whitespace. Use
url.strip()
if necessary. - No newline characters (
\n
) or other control characters within the URL. - No unescaped spaces or other special characters within the URL's path or query parameters. Use
urllib.parse.quote()
orurllib.parse.urlencode()
to properly encode these if necessary.
Accidental Tuple Creation
An easy-to-make mistake is accidentally creating a tuple instead of a string:
url = 'https://jsonplaceholder.typicode.com/posts', # Note the trailing comma!
# ('https://jsonplaceholder.typicode.com/posts',) # This is a tuple!
print(url)
print(type(url)) # Output: <class 'tuple'>
# ⛔️ requests.exceptions.InvalidSchema (because it's a tuple)
# requests.get(url, timeout=10)
#✅ CORRECT: Remove the trailing comma.
print(url[0])
url = '.../posts',
: The trailing comma creates a single-element tuple, not a string.- Solution: Remove the trailing comma, or unpack the tuple value before using the url in a request:
requests.get(url[0], ...)
Conclusion
The requests.exceptions.InvalidSchema: No connection adapters were found for
error is almost always due to an improperly formatted or typed URL. Always double-check that:
- You're passing a string to
requests.get()
(and otherrequests
methods). - The URL string includes the protocol scheme (
http://
orhttps://
). - The URL string is correctly formatted and doesn't contain invalid characters.
- You haven't accidentally created a tuple instead of a string.
By carefully inspecting your URL and its type, you can quickly resolve this common error.