[GH-ISSUE #1070] sp.recommendation() can't handle >5 artists/songs/genres as input - gives 401 error #630

Closed
opened 2026-02-28 00:00:23 +03:00 by kerem · 1 comment
Owner

Originally created by @macafra on GitHub (Jan 14, 2024).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1070

Describe the bug
When I use sp.recommendations(), the total amount of artists, songs and genres I put in cannot exceed 5 before I get a 401 (no token provided) error. If I put in 5 or less artists/songs/genres, it outputs a recommendation as expected.

Your code

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())
genre_list = spotify.recommendation_genre_seeds()['genres']

def get_artist_id(name):
    results = spotify.search(q='artist:' + name, type='artist')
    items = results['artists']['items']
    if len(items) > 0:
        return items[0]['id']
    else:
        return None
    
def get_song_id(name):
    results = spotify.search(q='track:' + name, type='track')
    items = results['tracks']['items']
    if len(items) > 0:
        return items[0]['id']
    else:
        return None
    
def filter_artist_list(artists):
    artist_id_list = []
    for artist in artists:
        artist_id = get_artist_id(artist)
        if artist_id != None:
            artist_id_list.append(artist_id)
        if len(artist_id_list) >= 5:
            break
    return artist_id_list

def filter_song_list(songs):
    song_list = []
    song_id_list = []
    for song in songs:
        song_id = get_song_id(song)
        if song_id != None:
            song_list.append(song)
            song_id_list.append(song_id)
        if len(song_list) >= 5:
            break
    print(song_list)
    return song_id_list

def filter_genre_list(genres):
    usable_genre_list = []
    for genre in genres:
        if genre in genre_list:
            usable_genre_list.append(genre)
        if len(usable_genre_list) >= 5:
            break
    return usable_genre_list

def show_recommendations(artists=None, genres=None, songs=None):
    results = spotify.recommendations(seed_artists=artists, seed_genres=genres, seed_tracks=songs, limit=20)
    track = results['tracks'][0]
    print('Recommendation: '+ track['name'] +  ' - ' +  track['artists'][0]['name'] + '\nLink: ' + track['external_urls']['spotify'])

liked_artists = ['Birdy', 'Rihanna', 'Beyonce', 'Ariana Grande', 'Lady Gaga']
liked_genres = ['pop', 'metal']
liked_songs = ['california king bed', 'Girls']
        
show_recommendations(filter_artist_list(liked_artists), filter_genre_list(liked_genres), filter_song_list(liked_songs))

Expected behavior
I expect recommendations() to output a recommendation regardless of list length

Output
HTTP Error for GET to https://api.spotify.com/v1/recommendations with Params: {'limit': 20, 'seed_artists': '2WX2uTcsvV5OnS0inACecP,5pKCCKE2ajJHZ9KAiaK11H,6vWDO969PvNqNYHIOW5v0m,66CXWjxzNUsdJxJ2JdwvnR,1HY2Jd0NmPuamShAr6KMms', 'seed_genres': 'pop,metal', 'seed_tracks': '1NpDnkQO0yWxTtntrC0UNz,4VXIryQMWpIdGgYR4TrjT1'} returned 400 due to invalid request
Traceback (most recent call last):
File "[...]\spotipy\client.py", line 271, in _internal_call
response.raise_for_status()
File "[...]\requests\models.py", line 1021, 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=2WX2uTcsvV5OnS0inACecP%2C5pKCCKE2ajJHZ9KAiaK11H%2C6vWDO969PvNqNYHIOW5v0m%2C66CXWjxzNUsdJxJ2JdwvnR%2C1HY2Jd0NmPuamShAr6KMms&seed_genres=pop%2Cmetal&seed_tracks=1NpDnkQO0yWxTtntrC0UNz%2C4VXIryQMWpIdGgYR4TrjT1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "[...]\spotipy_test.py", line 72, in
show_recommendations(filter_artist_list(liked_artists), filter_genre_list(liked_genres), filter_song_list(liked_songs))
File "[...]\spotipy_test.py", line 61, in show_recommendations
results = spotify.recommendations(seed_artists=artists, seed_genres=genres, seed_tracks=songs, limit=20)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "[...]\spotipy\client.py", line 1712, in recommendations
return self._get("recommendations", **params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File [...]\spotipy\client.py", line 323, in _get
return self._internal_call("GET", url, payload, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "[...]\spotipy\client.py", line 293, in _internal_call
raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_artists=2WX2uTcsvV5OnS0inACecP%2C5pKCCKE2ajJHZ9KAiaK11H%2C6vWDO969PvNqNYHIOW5v0m%2C66CXWjxzNUsdJxJ2JdwvnR%2C1HY2Jd0NmPuamShAr6KMms&seed_genres=pop%2Cmetal&seed_tracks=1NpDnkQO0yWxTtntrC0UNz%2C4VXIryQMWpIdGgYR4TrjT1:
invalid request, reason: None

Environment:

  • OS: [Windows]
  • Python version [3.11.5]
  • spotipy version [2.23.0]
  • your IDE [VS code]

Additional context
Add any other context about the problem here.

Originally created by @macafra on GitHub (Jan 14, 2024). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1070 **Describe the bug** When I use sp.recommendations(), the total amount of artists, songs and genres I put in cannot exceed 5 before I get a 401 (no token provided) error. If I put in 5 or less artists/songs/genres, it outputs a recommendation as expected. **Your code** ``` import spotipy from spotipy.oauth2 import SpotifyClientCredentials spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials()) genre_list = spotify.recommendation_genre_seeds()['genres'] def get_artist_id(name): results = spotify.search(q='artist:' + name, type='artist') items = results['artists']['items'] if len(items) > 0: return items[0]['id'] else: return None def get_song_id(name): results = spotify.search(q='track:' + name, type='track') items = results['tracks']['items'] if len(items) > 0: return items[0]['id'] else: return None def filter_artist_list(artists): artist_id_list = [] for artist in artists: artist_id = get_artist_id(artist) if artist_id != None: artist_id_list.append(artist_id) if len(artist_id_list) >= 5: break return artist_id_list def filter_song_list(songs): song_list = [] song_id_list = [] for song in songs: song_id = get_song_id(song) if song_id != None: song_list.append(song) song_id_list.append(song_id) if len(song_list) >= 5: break print(song_list) return song_id_list def filter_genre_list(genres): usable_genre_list = [] for genre in genres: if genre in genre_list: usable_genre_list.append(genre) if len(usable_genre_list) >= 5: break return usable_genre_list def show_recommendations(artists=None, genres=None, songs=None): results = spotify.recommendations(seed_artists=artists, seed_genres=genres, seed_tracks=songs, limit=20) track = results['tracks'][0] print('Recommendation: '+ track['name'] + ' - ' + track['artists'][0]['name'] + '\nLink: ' + track['external_urls']['spotify']) liked_artists = ['Birdy', 'Rihanna', 'Beyonce', 'Ariana Grande', 'Lady Gaga'] liked_genres = ['pop', 'metal'] liked_songs = ['california king bed', 'Girls'] show_recommendations(filter_artist_list(liked_artists), filter_genre_list(liked_genres), filter_song_list(liked_songs)) ``` **Expected behavior** I expect recommendations() to output a recommendation regardless of list length **Output** HTTP Error for GET to https://api.spotify.com/v1/recommendations with Params: {'limit': 20, 'seed_artists': '2WX2uTcsvV5OnS0inACecP,5pKCCKE2ajJHZ9KAiaK11H,6vWDO969PvNqNYHIOW5v0m,66CXWjxzNUsdJxJ2JdwvnR,1HY2Jd0NmPuamShAr6KMms', 'seed_genres': 'pop,metal', 'seed_tracks': '1NpDnkQO0yWxTtntrC0UNz,4VXIryQMWpIdGgYR4TrjT1'} returned 400 due to invalid request Traceback (most recent call last): File "[...]\spotipy\client.py", line 271, in _internal_call response.raise_for_status() File "[...]\requests\models.py", line 1021, 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=2WX2uTcsvV5OnS0inACecP%2C5pKCCKE2ajJHZ9KAiaK11H%2C6vWDO969PvNqNYHIOW5v0m%2C66CXWjxzNUsdJxJ2JdwvnR%2C1HY2Jd0NmPuamShAr6KMms&seed_genres=pop%2Cmetal&seed_tracks=1NpDnkQO0yWxTtntrC0UNz%2C4VXIryQMWpIdGgYR4TrjT1 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "[...]\spotipy_test.py", line 72, in <module> show_recommendations(filter_artist_list(liked_artists), filter_genre_list(liked_genres), filter_song_list(liked_songs)) File "[...]\spotipy_test.py", line 61, in show_recommendations results = spotify.recommendations(seed_artists=artists, seed_genres=genres, seed_tracks=songs, limit=20) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "[...]\spotipy\client.py", line 1712, in recommendations return self._get("recommendations", **params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File [...]\spotipy\client.py", line 323, in _get return self._internal_call("GET", url, payload, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "[...]\spotipy\client.py", line 293, in _internal_call raise SpotifyException( spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_artists=2WX2uTcsvV5OnS0inACecP%2C5pKCCKE2ajJHZ9KAiaK11H%2C6vWDO969PvNqNYHIOW5v0m%2C66CXWjxzNUsdJxJ2JdwvnR%2C1HY2Jd0NmPuamShAr6KMms&seed_genres=pop%2Cmetal&seed_tracks=1NpDnkQO0yWxTtntrC0UNz%2C4VXIryQMWpIdGgYR4TrjT1: invalid request, reason: None **Environment:** - OS: [Windows] - Python version [3.11.5] - spotipy version [2.23.0] - your IDE [VS code] **Additional context** Add any other context about the problem here.
kerem 2026-02-28 00:00:23 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@macafra commented on GitHub (Jan 14, 2024):

I noticed the intention of the function is max 5 seeds

<!-- gh-comment-id:1891008466 --> @macafra commented on GitHub (Jan 14, 2024): I noticed the intention of the function is max 5 seeds
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#630
No description provided.