[GH-ISSUE #946] recommendations timeout #570

Closed
opened 2026-02-27 23:23:24 +03:00 by kerem · 4 comments
Owner

Originally created by @yuma-dev on GitHub (Mar 4, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/946

Describe the bug
Whenever I try to use the recommendations function I get this error:
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.spotify.com', port=443): Read timed out. (read timeout=5)
I've tried at different times because I thought the Spotify servers might be messed up rn but it keeps happening with new code and different parameters.

Your code
only one of many examples:

def create_recommendations_playlist():
    # Set up the client credentials manager with your client ID and secret
    client_id = "********************************"
    client_secret = "********************************"

    # initializing the Spotify client using client id and client secret
    sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri="http://localhost:8000/callback", scope="playlist-modify-public user-top-read"))


    # get the user's top tracks 
    top_tracks = sp.current_user_top_tracks(limit=50, time_range="short_term")
    top_tracks_ids = [track['id'] for track in top_tracks["items"]]

    # get recommended tracks based on your top tracks
    recommendations = pd.DataFrame(sp.recommendations(seed_tracks=top_tracks_ids, limit=20)["tracks"])
    recommendations_ids = recommendations['id'].tolist()

    # create a new playlist with the recommended tracks
    playlist = sp.user_playlist_create(user=sp.current_user()["id"], name="Your Recommendations", public=True)
    playlist_id = playlist["id"]

    # add the recommended tracks to the playlist
    add_tracks_to_playlist = sp.playlist_add_items(playlist_id=playlist_id, items=recommendations_ids)
    return "Successfully created a new playlist with {} recommended tracks added.".format(len(recommendations_ids))

Expected behavior
doesn't Time out

Output

Traceback (most recent call last):
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
                       ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1374, in getresponse
    response.begin()
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 318, in begin
    version, status, reason = self._read_status()
                              ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 279, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 705, in readinto
    return self._sock.recv_into(b)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 1278, in recv_into
    return self.read(nbytes, buffer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 1134, in read
    return self._sslobj.read(len, buffer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 489, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\packages\six.py", line 770, in reraise
    raise value
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 451, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 340, in _raise_timeout
    raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.spotify.com', port=443): Read timed out. (read timeout=5)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Fabia\OneDrive\Dokumente\Coding\Python\captainai\lib\test.py", line 35, in <module>
    create_playlist()
  File "C:\Users\Fabia\OneDrive\Dokumente\Coding\Python\captainai\lib\test.py", line 24, in create_playlist
    recommendations = pd.DataFrame(sp.recommendations(seed_tracks=top_tracks_ids, limit=20)["tracks"])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 1667, in recommendations
    return self._get("recommendations", **params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 297, in _get
    return self._internal_call("GET", url, payload, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 240, in _internal_call
    response = self._session.request(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 578, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.spotify.com', port=443): Read timed out. (read timeout=5)

Process finished with exit code 1

Environment:

  • OS: Windows 11
  • Python version: 3.11.0
  • spotipy version: 2.22.0
Originally created by @yuma-dev on GitHub (Mar 4, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/946 **Describe the bug** Whenever I try to use the recommendations function I get this error: `requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.spotify.com', port=443): Read timed out. (read timeout=5)` I've tried at different times because I thought the Spotify servers might be messed up rn but it keeps happening with new code and different parameters. **Your code** only one of many examples: ```py def create_recommendations_playlist(): # Set up the client credentials manager with your client ID and secret client_id = "********************************" client_secret = "********************************" # initializing the Spotify client using client id and client secret sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri="http://localhost:8000/callback", scope="playlist-modify-public user-top-read")) # get the user's top tracks top_tracks = sp.current_user_top_tracks(limit=50, time_range="short_term") top_tracks_ids = [track['id'] for track in top_tracks["items"]] # get recommended tracks based on your top tracks recommendations = pd.DataFrame(sp.recommendations(seed_tracks=top_tracks_ids, limit=20)["tracks"]) recommendations_ids = recommendations['id'].tolist() # create a new playlist with the recommended tracks playlist = sp.user_playlist_create(user=sp.current_user()["id"], name="Your Recommendations", public=True) playlist_id = playlist["id"] # add the recommended tracks to the playlist add_tracks_to_playlist = sp.playlist_add_items(playlist_id=playlist_id, items=recommendations_ids) return "Successfully created a new playlist with {} recommended tracks added.".format(len(recommendations_ids)) ``` **Expected behavior** doesn't Time out **Output** ```console Traceback (most recent call last): File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request six.raise_from(e, None) File "<string>", line 3, in raise_from File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request httplib_response = conn.getresponse() ^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1374, in getresponse response.begin() File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 318, in begin version, status, reason = self._read_status() ^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 279, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 705, in readinto return self._sock.recv_into(b) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 1278, in recv_into return self.read(nbytes, buffer) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 1134, in read return self._sslobj.read(len, buffer) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TimeoutError: The read operation timed out During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 489, in send resp = conn.urlopen( ^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen retries = retries.increment( ^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\retry.py", line 550, in increment raise six.reraise(type(error), error, _stacktrace) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\packages\six.py", line 770, in reraise raise value File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen httplib_response = self._make_request( ^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 451, in _make_request self._raise_timeout(err=e, url=url, timeout_value=read_timeout) File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 340, in _raise_timeout raise ReadTimeoutError( urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.spotify.com', port=443): Read timed out. (read timeout=5) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Fabia\OneDrive\Dokumente\Coding\Python\captainai\lib\test.py", line 35, in <module> create_playlist() File "C:\Users\Fabia\OneDrive\Dokumente\Coding\Python\captainai\lib\test.py", line 24, in create_playlist recommendations = pd.DataFrame(sp.recommendations(seed_tracks=top_tracks_ids, limit=20)["tracks"]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 1667, in recommendations return self._get("recommendations", **params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 297, in _get return self._internal_call("GET", url, payload, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 240, in _internal_call response = self._session.request( ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 587, in request resp = self.send(prep, **send_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 701, in send r = adapter.send(request, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 578, in send raise ReadTimeout(e, request=request) requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.spotify.com', port=443): Read timed out. (read timeout=5) Process finished with exit code 1 ``` **Environment:** - OS: Windows 11 - Python version: 3.11.0 - spotipy version: 2.22.0
kerem 2026-02-27 23:23:24 +03:00
  • closed this issue
  • added the
    api-bug
    label
Author
Owner

@Heroesluk commented on GitHub (Mar 22, 2023):

It works when you limit number of provided seed tracks to 5 or less though?

spotipy.recommendations function specifies that
""" Get a list of recommended tracks for one to five seeds.
(at least one of seed_artists, seed_tracks and seed_genres
are needed)"""

If i understand correctly this is intended behaviour that you can't provide more then 5 tracks/artists/genres

<!-- gh-comment-id:1479394445 --> @Heroesluk commented on GitHub (Mar 22, 2023): It works when you limit number of provided seed tracks to 5 or less though? spotipy.recommendations function specifies that """ Get a list of recommended tracks for one to five seeds. (at least one of `seed_artists`, `seed_tracks` and `seed_genres` are needed)""" If i understand correctly this is intended behaviour that you can't provide more then 5 tracks/artists/genres
Author
Owner

@jwreiser commented on GitHub (Mar 7, 2024):

I will provide a list of one seed and still get a time out. This was working before without a code change or updating spotipy so I imagine there is some issue on Spotify's side (is it rate limiting just this call (the others are fine)? Is it having an issue finding a recommendation?) I even lowered the limit from 20 (where it used to work) to 3 and it still times out. An even bigger problem is I can not get the requests_timeout to be honored so my application freezes a long time waiting for the timeout.
Here is my setup:

scope = "user-read-playback-state,user-modify-playback-state,user-library-modify,user-follow-modify"#
sp = spotipy.Spotify(client_credentials_manager=SpotifyOAuth(username='XXXXXXXXX', scope=scope),requests_timeout=15)

# and one of my recommendation calls:
try:
    recommendations = sp.recommendations(seed_artists=[artist_id], limit=10)
    for track in recommendations['tracks']:
        if not filter_results or track['uri'] not in list(songs_df):
            songs.append(track['uri'])
except:
    print('could not get song recommendations')
<!-- gh-comment-id:1983476401 --> @jwreiser commented on GitHub (Mar 7, 2024): I will provide a list of one seed and still get a time out. This was working before without a code change or updating spotipy so I imagine there is some issue on Spotify's side (is it rate limiting just this call (the others are fine)? Is it having an issue finding a recommendation?) I even lowered the limit from 20 (where it used to work) to 3 and it still times out. An even bigger problem is I can not get the requests_timeout to be honored so my application freezes a long time waiting for the timeout. Here is my setup: ```python scope = "user-read-playback-state,user-modify-playback-state,user-library-modify,user-follow-modify"# sp = spotipy.Spotify(client_credentials_manager=SpotifyOAuth(username='XXXXXXXXX', scope=scope),requests_timeout=15) # and one of my recommendation calls: try: recommendations = sp.recommendations(seed_artists=[artist_id], limit=10) for track in recommendations['tracks']: if not filter_results or track['uri'] not in list(songs_df): songs.append(track['uri']) except: print('could not get song recommendations') ```
Author
Owner

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

You could try out the same request on the Web API Documentation, just to make sure that this is not caused by the library.

<!-- gh-comment-id:2268867378 --> @dieser-niko commented on GitHub (Aug 5, 2024): You could try out the same request on the [Web API Documentation](https://developer.spotify.com/documentation/web-api/reference/get-recommendations), just to make sure that this is not caused by the library.
Author
Owner

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

I'm going to have to close this issue, the recommendations endpoint is now deprecated.

<!-- gh-comment-id:2569205315 --> @dieser-niko commented on GitHub (Jan 3, 2025): I'm going to have to close this issue, the recommendations endpoint is now deprecated.
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#570
No description provided.