[GH-ISSUE #644] recommendations() requests.exceptions.HTTPError #382

Closed
opened 2026-02-27 23:22:19 +03:00 by kerem · 21 comments
Owner

Originally created by @carolynzech on GitHub (Feb 20, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/644

I have the following code:

import spotipy
from spotipy.oauth2 import SpotifyOAuth

username = 'XXX'
client_id = 'XXX'
client_secret = 'XXX'
redirect_uri = 'http://localhost:8888/callback/'
scope = 'user-top-read user-read-recently-played playlist-modify-public playlist-modify-private'

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret,
                                               redirect_uri=redirect_uri, scope=scope, username=username))

print(sp.recommendations(
        seed_tracks=['spotify:track:7cm50Lw03k6VvRauJtkyTj', 'spotify:track:3zwMVvkBe2qIKDObWgXw4N',
                     'spotify:track:0KRYCBwIpWYFNrXOmXbyUh', 'spotify:track:5BK0uqwY9DNfZ630STAEaq',
                     'spotify:track:7lPN2DXiMsVn7XUKtOW1CS', 'spotify:track:1ZycJRpLOazlLcoFGxGROO',
                     'spotify:track:0lx2cLdOt3piJbcaXIV74f', 'spotify:track:3UoULw70kMsiVXxW0L3A33',
                     'spotify:track:4pfrrhvplbJZAIsfosGWQP', 'spotify:track:385VOmGbguaPG57TgoW8Lh'],
        seed_artists=['spotify:artist:06HL4z0CvFAxyc27GXpf02', 'spotify:artist:6qqNVTkY8uBg9cP3Jd7DAH',
                      'spotify:artist:4AK6F7OLvEQ5QYCBNiQWHq', 'spotify:artist:163tK9Wjr9P9DmM0AVK7lm',
                      'spotify:artist:7n2wHs1TKAczGzO7Dd2rGr', 'spotify:artist:66CXWjxzNUsdJxJ2JdwvnR',
                      'spotify:artist:6LuN9FCkKOj5PcnpouEgny', 'spotify:artist:4dpARuHxo51G3z768sgnrY',
                      'spotify:artist:4nDoRrQiYLoBzwC5BhVJzF', 'spotify:artist:6KImCVD70vtIoJWnq6nGn3']))

and I am getting the following errors:

HTTP Error for GET to https://api.spotify.com/v1/recommendations returned 400 due to invalid request
Traceback (most recent call last):
  File "/Library/Python/3.8/site-packages/spotipy/client.py", line 245, in _internal_call
    response.raise_for_status()
  File "/Users/carolynzech/Library/Python/3.8/lib/python/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/recommendations?limit=20&seed_artists=06HL4z0CvFAxyc27GXpf02%2C6qqNVTkY8uBg9cP3Jd7DAH%2C4AK6F7OLvEQ5QYCBNiQWHq%2C163tK9Wjr9P9DmM0AVK7lm%2C7n2wHs1TKAczGzO7Dd2rGr%2C66CXWjxzNUsdJxJ2JdwvnR%2C6LuN9FCkKOj5PcnpouEgny%2C4dpARuHxo51G3z768sgnrY%2C4nDoRrQiYLoBzwC5BhVJzF%2C6KImCVD70vtIoJWnq6nGn3&seed_tracks=7cm50Lw03k6VvRauJtkyTj%2C3zwMVvkBe2qIKDObWgXw4N%2C0KRYCBwIpWYFNrXOmXbyUh%2C5BK0uqwY9DNfZ630STAEaq%2C7lPN2DXiMsVn7XUKtOW1CS%2C1ZycJRpLOazlLcoFGxGROO%2C0lx2cLdOt3piJbcaXIV74f%2C3UoULw70kMsiVXxW0L3A33%2C4pfrrhvplbJZAIsfosGWQP%2C385VOmGbguaPG57TgoW8Lh

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/carolynzech/playlist-maker/main.py", line 69, in <module>
    main()
  File "/Users/carolynzech/playlist-maker/main.py", line 55, in main
    print(sp.recommendations(
  File "/Library/Python/3.8/site-packages/spotipy/client.py", line 1597, in recommendations
    return self._get("recommendations", **params)
  File "/Library/Python/3.8/site-packages/spotipy/client.py", line 291, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/Library/Python/3.8/site-packages/spotipy/client.py", line 261, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_artists=06HL4z0CvFAxyc27GXpf02%2C6qqNVTkY8uBg9cP3Jd7DAH%2C4AK6F7OLvEQ5QYCBNiQWHq%2C163tK9Wjr9P9DmM0AVK7lm%2C7n2wHs1TKAczGzO7Dd2rGr%2C66CXWjxzNUsdJxJ2JdwvnR%2C6LuN9FCkKOj5PcnpouEgny%2C4dpARuHxo51G3z768sgnrY%2C4nDoRrQiYLoBzwC5BhVJzF%2C6KImCVD70vtIoJWnq6nGn3&seed_tracks=7cm50Lw03k6VvRauJtkyTj%2C3zwMVvkBe2qIKDObWgXw4N%2C0KRYCBwIpWYFNrXOmXbyUh%2C5BK0uqwY9DNfZ630STAEaq%2C7lPN2DXiMsVn7XUKtOW1CS%2C1ZycJRpLOazlLcoFGxGROO%2C0lx2cLdOt3piJbcaXIV74f%2C3UoULw70kMsiVXxW0L3A33%2C4pfrrhvplbJZAIsfosGWQP%2C385VOmGbguaPG57TgoW8Lh:
 invalid request, reason: None
Exception ignored in: <function Spotify.__del__ at 0x103da0af0>
Traceback (most recent call last):
  File "/Library/Python/3.8/site-packages/spotipy/client.py", line 188, in __del__
TypeError: isinstance() arg 2 must be a type or tuple of types
Exception ignored in: <function SpotifyAuthBase.__del__ at 0x103e884c0>
Traceback (most recent call last):
  File "/Library/Python/3.8/site-packages/spotipy/oauth2.py", line 136, in __del__
TypeError: isinstance() arg 2 must be a type or tuple of types

Any help would be much appreciated!

Originally created by @carolynzech on GitHub (Feb 20, 2021). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/644 I have the following code: ``` import spotipy from spotipy.oauth2 import SpotifyOAuth username = 'XXX' client_id = 'XXX' client_secret = 'XXX' redirect_uri = 'http://localhost:8888/callback/' scope = 'user-top-read user-read-recently-played playlist-modify-public playlist-modify-private' sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope=scope, username=username)) print(sp.recommendations( seed_tracks=['spotify:track:7cm50Lw03k6VvRauJtkyTj', 'spotify:track:3zwMVvkBe2qIKDObWgXw4N', 'spotify:track:0KRYCBwIpWYFNrXOmXbyUh', 'spotify:track:5BK0uqwY9DNfZ630STAEaq', 'spotify:track:7lPN2DXiMsVn7XUKtOW1CS', 'spotify:track:1ZycJRpLOazlLcoFGxGROO', 'spotify:track:0lx2cLdOt3piJbcaXIV74f', 'spotify:track:3UoULw70kMsiVXxW0L3A33', 'spotify:track:4pfrrhvplbJZAIsfosGWQP', 'spotify:track:385VOmGbguaPG57TgoW8Lh'], seed_artists=['spotify:artist:06HL4z0CvFAxyc27GXpf02', 'spotify:artist:6qqNVTkY8uBg9cP3Jd7DAH', 'spotify:artist:4AK6F7OLvEQ5QYCBNiQWHq', 'spotify:artist:163tK9Wjr9P9DmM0AVK7lm', 'spotify:artist:7n2wHs1TKAczGzO7Dd2rGr', 'spotify:artist:66CXWjxzNUsdJxJ2JdwvnR', 'spotify:artist:6LuN9FCkKOj5PcnpouEgny', 'spotify:artist:4dpARuHxo51G3z768sgnrY', 'spotify:artist:4nDoRrQiYLoBzwC5BhVJzF', 'spotify:artist:6KImCVD70vtIoJWnq6nGn3'])) ``` and I am getting the following errors: ``` HTTP Error for GET to https://api.spotify.com/v1/recommendations returned 400 due to invalid request Traceback (most recent call last): File "/Library/Python/3.8/site-packages/spotipy/client.py", line 245, in _internal_call response.raise_for_status() File "/Users/carolynzech/Library/Python/3.8/lib/python/site-packages/requests/models.py", line 943, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/recommendations?limit=20&seed_artists=06HL4z0CvFAxyc27GXpf02%2C6qqNVTkY8uBg9cP3Jd7DAH%2C4AK6F7OLvEQ5QYCBNiQWHq%2C163tK9Wjr9P9DmM0AVK7lm%2C7n2wHs1TKAczGzO7Dd2rGr%2C66CXWjxzNUsdJxJ2JdwvnR%2C6LuN9FCkKOj5PcnpouEgny%2C4dpARuHxo51G3z768sgnrY%2C4nDoRrQiYLoBzwC5BhVJzF%2C6KImCVD70vtIoJWnq6nGn3&seed_tracks=7cm50Lw03k6VvRauJtkyTj%2C3zwMVvkBe2qIKDObWgXw4N%2C0KRYCBwIpWYFNrXOmXbyUh%2C5BK0uqwY9DNfZ630STAEaq%2C7lPN2DXiMsVn7XUKtOW1CS%2C1ZycJRpLOazlLcoFGxGROO%2C0lx2cLdOt3piJbcaXIV74f%2C3UoULw70kMsiVXxW0L3A33%2C4pfrrhvplbJZAIsfosGWQP%2C385VOmGbguaPG57TgoW8Lh During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/carolynzech/playlist-maker/main.py", line 69, in <module> main() File "/Users/carolynzech/playlist-maker/main.py", line 55, in main print(sp.recommendations( File "/Library/Python/3.8/site-packages/spotipy/client.py", line 1597, in recommendations return self._get("recommendations", **params) File "/Library/Python/3.8/site-packages/spotipy/client.py", line 291, in _get return self._internal_call("GET", url, payload, kwargs) File "/Library/Python/3.8/site-packages/spotipy/client.py", line 261, in _internal_call raise SpotifyException( spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_artists=06HL4z0CvFAxyc27GXpf02%2C6qqNVTkY8uBg9cP3Jd7DAH%2C4AK6F7OLvEQ5QYCBNiQWHq%2C163tK9Wjr9P9DmM0AVK7lm%2C7n2wHs1TKAczGzO7Dd2rGr%2C66CXWjxzNUsdJxJ2JdwvnR%2C6LuN9FCkKOj5PcnpouEgny%2C4dpARuHxo51G3z768sgnrY%2C4nDoRrQiYLoBzwC5BhVJzF%2C6KImCVD70vtIoJWnq6nGn3&seed_tracks=7cm50Lw03k6VvRauJtkyTj%2C3zwMVvkBe2qIKDObWgXw4N%2C0KRYCBwIpWYFNrXOmXbyUh%2C5BK0uqwY9DNfZ630STAEaq%2C7lPN2DXiMsVn7XUKtOW1CS%2C1ZycJRpLOazlLcoFGxGROO%2C0lx2cLdOt3piJbcaXIV74f%2C3UoULw70kMsiVXxW0L3A33%2C4pfrrhvplbJZAIsfosGWQP%2C385VOmGbguaPG57TgoW8Lh: invalid request, reason: None Exception ignored in: <function Spotify.__del__ at 0x103da0af0> Traceback (most recent call last): File "/Library/Python/3.8/site-packages/spotipy/client.py", line 188, in __del__ TypeError: isinstance() arg 2 must be a type or tuple of types Exception ignored in: <function SpotifyAuthBase.__del__ at 0x103e884c0> Traceback (most recent call last): File "/Library/Python/3.8/site-packages/spotipy/oauth2.py", line 136, in __del__ TypeError: isinstance() arg 2 must be a type or tuple of types ``` Any help would be much appreciated!
kerem 2026-02-27 23:22:19 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@Peter-Schorn commented on GitHub (Feb 20, 2021):

The total number of seed genres, seed artists, and seed tracks must add up to five or less. See https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-recommendations

<!-- gh-comment-id:782760003 --> @Peter-Schorn commented on GitHub (Feb 20, 2021): The total number of seed genres, seed artists, and seed tracks must add up to five or less. See https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-recommendations
Author
Owner

@Peter-Schorn commented on GitHub (Feb 20, 2021):

There's also a pretty simple way to debug error you're getting in the SpotifyAuthBase.__del__. The second argument passed to isinstance() is requests.Session, which is a type. More specifically, it's a class. The fact that the error message suggests that it's not a type means that this name is somehow being reassigned to some other object that's not a type. Change the __del__ method as follows to find out what this name is being reassigned to (and what its type is):

def __del__(self):
    """Make sure the connection (pool) gets closed"""
    try:
        if isinstance(self._session, requests.Session):
            self._session.close()
    except TypeError as error:
        print(error)
        print(
            f"`requests.Session`: {requests.Session}. "
            f"type of `requests.Session`: {type(requests.Session)}"
        )
<!-- gh-comment-id:782765864 --> @Peter-Schorn commented on GitHub (Feb 20, 2021): There's also a pretty simple way to debug error you're getting in the `SpotifyAuthBase.__del__`. The second argument passed to `isinstance()` is `requests.Session`, which *is* a type. More specifically, it's a class. The fact that the error message suggests that it's not a type means that this name is somehow being reassigned to some other object that's not a type. Change the `__del__` method as follows to find out what this name is being reassigned to (and what its type is): ```python def __del__(self): """Make sure the connection (pool) gets closed""" try: if isinstance(self._session, requests.Session): self._session.close() except TypeError as error: print(error) print( f"`requests.Session`: {requests.Session}. " f"type of `requests.Session`: {type(requests.Session)}" ) ```
Author
Owner

@carolynzech commented on GitHub (Feb 21, 2021):

Your comments fixed both of my issues. Thank you for your timely and detailed response--I really appreciate it.

<!-- gh-comment-id:782882945 --> @carolynzech commented on GitHub (Feb 21, 2021): Your comments fixed both of my issues. Thank you for your timely and detailed response--I really appreciate it.
Author
Owner

@Peter-Schorn commented on GitHub (Feb 21, 2021):

I'm still curious about the cause of the error in the __del__ method. In theory, this should be unrelated to the issue with the recommendations endpoint. This error has come up in multiple issues, yet I've been unable to reproduce it. Can you try to reproduce it with the modified __del__ method that I posted?

<!-- gh-comment-id:782884019 --> @Peter-Schorn commented on GitHub (Feb 21, 2021): I'm still curious about the cause of the error in the `__del__` method. In theory, this should be unrelated to the issue with the recommendations endpoint. This error has come up in multiple issues, yet I've been unable to reproduce it. Can you try to reproduce it with the modified `__del__` method that I posted?
Author
Owner

@carolynzech commented on GitHub (Feb 22, 2021):

I also was unable to reproduce it. I wasn't able to figure out how to get around read-only permissions (I installed with pip) in PyCharm, and when I pulled directly from this repo instead, I couldn't compile the code because of a ModuleNotFound error for the cache_handler file, even though the module was clearly there. Instead, I took the code you wrote and replaced self._session with sp. When I ran the try-except at the top of the file, it didn't work, but when I instantiated the sp object inside my main method and ran the try-except there, the error went away. This isn't ideal, since now I have to pass the sp variable into every one of my functions, but it's not too big of a deal. For clarification, I had this:

# these are declared at the top of the file, outside of a function
username = 'XXX'
client_id = 'XXX'
client_secret = 'XXX'
redirect_uri = 'http://localhost:8888/callback/'
scope = 'user-top-read user-read-recently-played playlist-modify-public playlist-modify-private'

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret,
                                               redirect_uri=redirect_uri, scope=scope, username=username))
# running the try-except here didn't catch the exception

def main():
   sp.recommendations(...)

and now I have this:

# no variables declared at the top of the file

def main():
  username = 'XXX'
  client_id = 'XXX'
  client_secret = 'XXX'
  redirect_uri = 'http://localhost:8888/callback/'
  scope = 'user-top-read user-read-recently-played playlist-modify-public playlist-modify-private'
  
  sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret,
                                                 redirect_uri=redirect_uri, scope=scope, username=username))

  sp.recommendations(...)

which works fine. If you know how to get around the read-only permissions in PyCharm, I'd be happy to try to reproduce it with the __del__ method you wrote. I did some research to no avail--it prompts me to change the read-only permissions, only to tell me each time that it was unable to execute that request.

<!-- gh-comment-id:783636159 --> @carolynzech commented on GitHub (Feb 22, 2021): I also was unable to reproduce it. I wasn't able to figure out how to get around read-only permissions (I installed with pip) in PyCharm, and when I pulled directly from this repo instead, I couldn't compile the code because of a ModuleNotFound error for the cache_handler file, even though the module was clearly there. Instead, I took the code you wrote and replaced self._session with sp. When I ran the try-except at the top of the file, it didn't work, but when I instantiated the sp object inside my main method and ran the try-except there, the error went away. This isn't ideal, since now I have to pass the sp variable into every one of my functions, but it's not too big of a deal. For clarification, I had this: ``` # these are declared at the top of the file, outside of a function username = 'XXX' client_id = 'XXX' client_secret = 'XXX' redirect_uri = 'http://localhost:8888/callback/' scope = 'user-top-read user-read-recently-played playlist-modify-public playlist-modify-private' sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope=scope, username=username)) # running the try-except here didn't catch the exception def main(): sp.recommendations(...) ``` and now I have this: ``` # no variables declared at the top of the file def main(): username = 'XXX' client_id = 'XXX' client_secret = 'XXX' redirect_uri = 'http://localhost:8888/callback/' scope = 'user-top-read user-read-recently-played playlist-modify-public playlist-modify-private' sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope=scope, username=username)) sp.recommendations(...) ``` which works fine. If you know how to get around the read-only permissions in PyCharm, I'd be happy to try to reproduce it with the `__del__` method you wrote. I did some research to no avail--it prompts me to change the read-only permissions, only to tell me each time that it was unable to execute that request.
Author
Owner

@Peter-Schorn commented on GitHub (Feb 22, 2021):

What operating system are you using?

<!-- gh-comment-id:783643512 --> @Peter-Schorn commented on GitHub (Feb 22, 2021): What operating system are you using?
Author
Owner

@Peter-Schorn commented on GitHub (Feb 22, 2021):

For Linux/macOS navigate to the root directory of the spotipy library and run:

sudo chown -R $(whoami) .

For windows, see here: https://stackoverflow.com/a/25119398/12394554

<!-- gh-comment-id:783646535 --> @Peter-Schorn commented on GitHub (Feb 22, 2021): For Linux/macOS navigate to the root directory of the spotipy library and run: ``` sudo chown -R $(whoami) . ``` For windows, see here: https://stackoverflow.com/a/25119398/12394554
Author
Owner

@Peter-Schorn commented on GitHub (Feb 22, 2021):

What did you mean when you said that you "replaced self._session with sp"? If you're having trouble reproducing the error, then make sure you change as little as possible. Only change the __del__ method as I described above. Make the same exact call to the recommendations endpoint that you did in the original code (i.e., with the incorrect number of seed tracks/artists).

<!-- gh-comment-id:783649148 --> @Peter-Schorn commented on GitHub (Feb 22, 2021): What did you mean when you said that you "replaced self._session with sp"? If you're having trouble reproducing the error, then make sure you change as little as possible. Only change the `__del__` method as I described above. Make the same exact call to the recommendations endpoint that you did in the original code (i.e., with the incorrect number of seed tracks/artists).
Author
Owner

@carolynzech commented on GitHub (Feb 22, 2021):

Okay, the sudo command worked. I ran the __del__ method you wrote with the same call to recommendations as in the original post (the incorrect one), and got this:

HTTP Error for GET to https://api.spotify.com/v1/recommendations returned 400 due to invalid request
Traceback (most recent call last):
  File "/Library/Python/3.8/site-packages/spotipy/client.py", line 252, in _internal_call
    response.raise_for_status()
  File "/Users/carolynzech/Library/Python/3.8/lib/python/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/recommendations?limit=20&seed_artists=06HL4z0CvFAxyc27GXpf02%2C6qqNVTkY8uBg9cP3Jd7DAH%2C4AK6F7OLvEQ5QYCBNiQWHq%2C163tK9Wjr9P9DmM0AVK7lm%2C7n2wHs1TKAczGzO7Dd2rGr%2C66CXWjxzNUsdJxJ2JdwvnR%2C6LuN9FCkKOj5PcnpouEgny%2C4dpARuHxo51G3z768sgnrY%2C4nDoRrQiYLoBzwC5BhVJzF%2C6KImCVD70vtIoJWnq6nGn3&seed_tracks=7cm50Lw03k6VvRauJtkyTj%2C3zwMVvkBe2qIKDObWgXw4N%2C0KRYCBwIpWYFNrXOmXbyUh%2C5BK0uqwY9DNfZ630STAEaq%2C7lPN2DXiMsVn7XUKtOW1CS%2C1ZycJRpLOazlLcoFGxGROO%2C0lx2cLdOt3piJbcaXIV74f%2C3UoULw70kMsiVXxW0L3A33%2C4pfrrhvplbJZAIsfosGWQP%2C385VOmGbguaPG57TgoW8Lh

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/carolynzech/playlist-maker/main.py", line 79, in <module>
    main()
  File "/Users/carolynzech/playlist-maker/main.py", line 57, in main
    print(sp.recommendations(
  File "/Library/Python/3.8/site-packages/spotipy/client.py", line 1604, in recommendations
    return self._get("recommendations", **params)
  File "/Library/Python/3.8/site-packages/spotipy/client.py", line 298, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/Library/Python/3.8/site-packages/spotipy/client.py", line 268, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_artists=06HL4z0CvFAxyc27GXpf02%2C6qqNVTkY8uBg9cP3Jd7DAH%2C4AK6F7OLvEQ5QYCBNiQWHq%2C163tK9Wjr9P9DmM0AVK7lm%2C7n2wHs1TKAczGzO7Dd2rGr%2C66CXWjxzNUsdJxJ2JdwvnR%2C6LuN9FCkKOj5PcnpouEgny%2C4dpARuHxo51G3z768sgnrY%2C4nDoRrQiYLoBzwC5BhVJzF%2C6KImCVD70vtIoJWnq6nGn3&seed_tracks=7cm50Lw03k6VvRauJtkyTj%2C3zwMVvkBe2qIKDObWgXw4N%2C0KRYCBwIpWYFNrXOmXbyUh%2C5BK0uqwY9DNfZ630STAEaq%2C7lPN2DXiMsVn7XUKtOW1CS%2C1ZycJRpLOazlLcoFGxGROO%2C0lx2cLdOt3piJbcaXIV74f%2C3UoULw70kMsiVXxW0L3A33%2C4pfrrhvplbJZAIsfosGWQP%2C385VOmGbguaPG57TgoW8Lh:
 invalid request, reason: None
isinstance() arg 2 must be a type or tuple of types
`requests.Session`: None. type of `requests.Session`: <class 'NoneType'>
isinstance() arg 2 must be a type or tuple of types
`requests.Session`: None. type of `requests.Session`: <class 'NoneType'>

In case it helps, this error also comes up whether I call recommendations() or not. If I change the main() method to do nothing at all (just pass), it still prints:

isinstance() arg 2 must be a type or tuple of types
`requests.Session`: None. type of `requests.Session`: <class 'NoneType'>
isinstance() arg 2 must be a type or tuple of types
`requests.Session`: None. type of `requests.Session`: <class 'NoneType'>

since I'm still instantiating sp at the top of the file. The difference comes from whether sp is instantiated at the top of the file, or inside the main method. If it's inside the main method, there's no error, and if it's outside, there's this error.

<!-- gh-comment-id:783660624 --> @carolynzech commented on GitHub (Feb 22, 2021): Okay, the sudo command worked. I ran the `__del__` method you wrote with the same call to recommendations as in the original post (the incorrect one), and got this: ``` HTTP Error for GET to https://api.spotify.com/v1/recommendations returned 400 due to invalid request Traceback (most recent call last): File "/Library/Python/3.8/site-packages/spotipy/client.py", line 252, in _internal_call response.raise_for_status() File "/Users/carolynzech/Library/Python/3.8/lib/python/site-packages/requests/models.py", line 943, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/recommendations?limit=20&seed_artists=06HL4z0CvFAxyc27GXpf02%2C6qqNVTkY8uBg9cP3Jd7DAH%2C4AK6F7OLvEQ5QYCBNiQWHq%2C163tK9Wjr9P9DmM0AVK7lm%2C7n2wHs1TKAczGzO7Dd2rGr%2C66CXWjxzNUsdJxJ2JdwvnR%2C6LuN9FCkKOj5PcnpouEgny%2C4dpARuHxo51G3z768sgnrY%2C4nDoRrQiYLoBzwC5BhVJzF%2C6KImCVD70vtIoJWnq6nGn3&seed_tracks=7cm50Lw03k6VvRauJtkyTj%2C3zwMVvkBe2qIKDObWgXw4N%2C0KRYCBwIpWYFNrXOmXbyUh%2C5BK0uqwY9DNfZ630STAEaq%2C7lPN2DXiMsVn7XUKtOW1CS%2C1ZycJRpLOazlLcoFGxGROO%2C0lx2cLdOt3piJbcaXIV74f%2C3UoULw70kMsiVXxW0L3A33%2C4pfrrhvplbJZAIsfosGWQP%2C385VOmGbguaPG57TgoW8Lh During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/carolynzech/playlist-maker/main.py", line 79, in <module> main() File "/Users/carolynzech/playlist-maker/main.py", line 57, in main print(sp.recommendations( File "/Library/Python/3.8/site-packages/spotipy/client.py", line 1604, in recommendations return self._get("recommendations", **params) File "/Library/Python/3.8/site-packages/spotipy/client.py", line 298, in _get return self._internal_call("GET", url, payload, kwargs) File "/Library/Python/3.8/site-packages/spotipy/client.py", line 268, in _internal_call raise SpotifyException( spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_artists=06HL4z0CvFAxyc27GXpf02%2C6qqNVTkY8uBg9cP3Jd7DAH%2C4AK6F7OLvEQ5QYCBNiQWHq%2C163tK9Wjr9P9DmM0AVK7lm%2C7n2wHs1TKAczGzO7Dd2rGr%2C66CXWjxzNUsdJxJ2JdwvnR%2C6LuN9FCkKOj5PcnpouEgny%2C4dpARuHxo51G3z768sgnrY%2C4nDoRrQiYLoBzwC5BhVJzF%2C6KImCVD70vtIoJWnq6nGn3&seed_tracks=7cm50Lw03k6VvRauJtkyTj%2C3zwMVvkBe2qIKDObWgXw4N%2C0KRYCBwIpWYFNrXOmXbyUh%2C5BK0uqwY9DNfZ630STAEaq%2C7lPN2DXiMsVn7XUKtOW1CS%2C1ZycJRpLOazlLcoFGxGROO%2C0lx2cLdOt3piJbcaXIV74f%2C3UoULw70kMsiVXxW0L3A33%2C4pfrrhvplbJZAIsfosGWQP%2C385VOmGbguaPG57TgoW8Lh: invalid request, reason: None isinstance() arg 2 must be a type or tuple of types `requests.Session`: None. type of `requests.Session`: <class 'NoneType'> isinstance() arg 2 must be a type or tuple of types `requests.Session`: None. type of `requests.Session`: <class 'NoneType'> ``` In case it helps, this error also comes up whether I call recommendations() or not. If I change the main() method to do nothing at all (just pass), it still prints: ``` isinstance() arg 2 must be a type or tuple of types `requests.Session`: None. type of `requests.Session`: <class 'NoneType'> isinstance() arg 2 must be a type or tuple of types `requests.Session`: None. type of `requests.Session`: <class 'NoneType'> ``` since I'm still instantiating sp at the top of the file. The difference comes from whether sp is instantiated at the top of the file, or inside the main method. If it's inside the main method, there's no error, and if it's outside, there's this error.
Author
Owner

@Peter-Schorn commented on GitHub (Feb 22, 2021):

At the beginning of your code, import requests and print(requests.Session). See if it's None.

<!-- gh-comment-id:783670925 --> @Peter-Schorn commented on GitHub (Feb 22, 2021): At the beginning of your code, import requests and `print(requests.Session)`. See if it's `None`.
Author
Owner

@Peter-Schorn commented on GitHub (Feb 22, 2021):

@stephanebruckert Any idea why requests.Session would be None?

<!-- gh-comment-id:783675324 --> @Peter-Schorn commented on GitHub (Feb 22, 2021): @stephanebruckert Any idea why `requests.Session` would be `None`?
Author
Owner

@carolynzech commented on GitHub (Feb 23, 2021):

print(requests.Session)
print(requests.Session is None)

returns

<class 'requests.sessions.Session'>
False
<!-- gh-comment-id:784327795 --> @carolynzech commented on GitHub (Feb 23, 2021): ``` print(requests.Session) print(requests.Session is None) ``` returns ``` <class 'requests.sessions.Session'> False ```
Author
Owner

@Findus23 commented on GitHub (May 14, 2021):

Just FYI: I have the same issue and absolutely no clue what is going wrong. I updated a lot of dependencies in my application (including requests and spotipy from 2.16.1 to 2.18.0, but quickly downgrading both doesn't seem to fix it) and since then I get the mentioned exception on every run (both in oauth2.py and in client.py).
type(requests.Session) prints <class 'NoneType'>

I updated the code to

if requests.Session and isinstance(self._session, requests.Session):
    self._session.close()

which of course isn't a solution (as it probably breaks the Session), but as my script is not running permanently, but as a scheduled task with very few requests per run, I doubt it will hurt that much.

<!-- gh-comment-id:841162662 --> @Findus23 commented on GitHub (May 14, 2021): Just FYI: I have the same issue and absolutely no clue what is going wrong. I updated a lot of dependencies in my application (including requests and spotipy from 2.16.1 to 2.18.0, but quickly downgrading both doesn't seem to fix it) and since then I get the mentioned exception on every run (both in oauth2.py and in client.py). `type(requests.Session)` prints `<class 'NoneType'>` I updated the code to ```python if requests.Session and isinstance(self._session, requests.Session): self._session.close() ``` which of course isn't a solution (as it probably breaks the Session), but as my script is not running permanently, but as a scheduled task with very few requests per run, I doubt it will hurt that much.
Author
Owner

@Peter-Schorn commented on GitHub (May 14, 2021):

@Findus23 What do you mean by "breaks the Session"?

<!-- gh-comment-id:841450120 --> @Peter-Schorn commented on GitHub (May 14, 2021): @Findus23 What do you mean by "breaks the Session"?
Author
Owner

@Findus23 commented on GitHub (May 14, 2021):

Sorry for being vague: I mean by that this change might mean that requests use it's Session class anymore as it is not properly closed and therfore a new TCP connection is opened for every request instead of reusing them.
But this is just a blind guess.

<!-- gh-comment-id:841479371 --> @Findus23 commented on GitHub (May 14, 2021): Sorry for being vague: I mean by that this change might mean that requests use it's Session class anymore as it is not properly closed and therfore a new TCP connection is opened for every request instead of reusing them. But this is just a blind guess.
Author
Owner

@Peter-Schorn commented on GitHub (May 14, 2021):

Why don't you just catch the error as I did here? Using if requests.Session is a terrible idea because who knows what the __bool__ method will return. If you're trying to check if it's not None, then do so explicitly: if requests.Session is not None.

as it is not properly closed and therfore a new TCP connection is opened for every request instead of reusing them.

If requests.Session has somehow been assigned to None (which is what causes this exception), then self._session won't be properly closed regardless of whether you use your updated version of __del__ or the current version.

The only way to ensure that the session is properly closed in the __del__ method is to find the underlying reason that requests.Session is being assigned to None.

<!-- gh-comment-id:841508956 --> @Peter-Schorn commented on GitHub (May 14, 2021): Why don't you just catch the error as I did [here](https://github.com/plamere/spotipy/issues/644#issuecomment-782765864)? Using `if requests.Session` is a terrible idea because who knows what the `__bool__` method will return. If you're trying to check if it's not `None`, then do so explicitly: `if requests.Session is not None`. > as it is not properly closed and therfore a new TCP connection is opened for every request instead of reusing them. If `requests.Session` has somehow been assigned to `None` (which is what causes this exception), then `self._session` won't be properly closed regardless of whether you use your updated version of `__del__` or the current version. The only way to ensure that the session is properly closed in the `__del__` method is to find the underlying reason that `requests.Session` is being assigned to `None`.
Author
Owner

@Findus23 commented on GitHub (Nov 25, 2021):

Once again I tried to find out what causes this and by simplifying my code step by step, I found out that adding a from flask import Flask/import flask in the same file as spotipy causes this issue (even if Flask is never used in any way).

So this minimal example breaks for me both during development and on the main server:

import spotipy
from spotipy import SpotifyClientCredentials
from flask import Flask
from config import spotify

crm = SpotifyClientCredentials(**spotify)
sp = spotipy.Spotify(client_credentials_manager=crm)
del sp

I am using flask 2.0.2 and have absolutely no clue why this would happen.

<!-- gh-comment-id:979141010 --> @Findus23 commented on GitHub (Nov 25, 2021): Once again I tried to find out what causes this and by simplifying my code step by step, I found out that adding a `from flask import Flask`/`import flask` in the same file as spotipy causes this issue (even if Flask is never used in any way). So this minimal example breaks for me both during development and on the main server: ```python import spotipy from spotipy import SpotifyClientCredentials from flask import Flask from config import spotify crm = SpotifyClientCredentials(**spotify) sp = spotipy.Spotify(client_credentials_manager=crm) del sp ``` I am using flask 2.0.2 and have absolutely no clue why this would happen.
Author
Owner

@dieser-niko commented on GitHub (Aug 28, 2024):

@Findus23 is this still an issue? Because even after downgrading I can't reproduce this error.

<!-- gh-comment-id:2315061490 --> @dieser-niko commented on GitHub (Aug 28, 2024): @Findus23 is this still an issue? Because even after downgrading I can't reproduce this error.
Author
Owner

@Findus23 commented on GitHub (Aug 28, 2024):

@dieser-niko Good question. I checked again and it seems like I have been including the workaround of modifying del ever since. But I just tested removing it and it still seems to work, so I guess maybe some flask update fixed it?

Either way I also can't reproduce it anymore.

<!-- gh-comment-id:2315358646 --> @Findus23 commented on GitHub (Aug 28, 2024): @dieser-niko Good question. I checked again and it seems like I have been including the workaround of modifying __del__ ever since. But I just tested removing it and it still seems to work, so I guess maybe some flask update fixed it? Either way I also can't reproduce it anymore.
Author
Owner

@dieser-niko commented on GitHub (Aug 28, 2024):

The thing is, I've seen this error multiple times in other issues but was never able to reproduce it myself. Kind of frustrating.
Popped up on stackoverflow as well: https://stackoverflow.com/questions/78871882/spotipy-has-started-printing-warning-of-a-typeerror-when-calling-isinstanceself

<!-- gh-comment-id:2315426780 --> @dieser-niko commented on GitHub (Aug 28, 2024): The thing is, I've seen this error multiple times in other issues but was never able to reproduce it myself. Kind of frustrating. Popped up on stackoverflow as well: https://stackoverflow.com/questions/78871882/spotipy-has-started-printing-warning-of-a-typeerror-when-calling-isinstanceself
Author
Owner

@dieser-niko commented on GitHub (Mar 3, 2025):

Closing in favour of #621 as it seems to be related

<!-- gh-comment-id:2693681060 --> @dieser-niko commented on GitHub (Mar 3, 2025): Closing in favour of #621 as it seems to be related
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#382
No description provided.