[GH-ISSUE #686] spotipy.recommendations() doesn't work for tracks #406

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

Originally created by @lohriialo on GitHub (May 31, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/686

spotipy version = 2.18.0

client_credentials_manager = SpotifyClientCredentials(SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET)
api = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

this works

random_artists = ["0C8ZW7ezQVs4URX5aX7Kqx", "1SupJlEpv7RS2tPNRaHViT"]
recommendation = api.recommendations(seed_artists=random_artists)
for track in recommendation['tracks']:
  print(track['name'], '-', track['artists'][0]['name'])

but this doesn't

random_tracks = ["-0kcet4aPpQ", "0RNDeBcIK00"]
recommendation = api.recommendations(seed_tracks=random_tracks)
for track in recommendation['tracks']:
  print(track['name'], '-', track['artists'][0]['name'])

or a combination of both doesn't work either

recommendation = api.recommendations(seed_artists=random_artists, seed_tracks=random_tracks)
for track in recommendation['tracks']:
  print(track['name'], '-', track['artists'][0]['name'])

Error is :

HTTP Error for GET to https://api.spotify.com/v1/recommendations returned 400 due to invalid request
Traceback (most recent call last):
  File "/Users/test/PycharmProjects/test/venv/lib/python3.6/site-packages/spotipy/client.py", line 245, in _internal_call
    response.raise_for_status()
  File "/Users/test/PycharmProjects/test/venv/lib/python3.6/site-packages/requests/models.py", line 862, 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_tracks=-0kcet4aPpQ%2C05vC8lMeelg%2C0RNDeBcIK00

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/test/PycharmProjects/test/spotify/user_recommendation/get_user_recommendation_old.py", line 37, in <module>
    recommendation = sp.recommendations(seed_tracks=random_tracks)
  File "/Users/test/PycharmProjects/test/venv/lib/python3.6/site-packages/spotipy/client.py", line 1652, in recommendations
    return self._get("recommendations", **params)
  File "/Users/test/PycharmProjects/test/venv/lib/python3.6/site-packages/spotipy/client.py", line 291, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/Users/test/PycharmProjects/test/venv/lib/python3.6/site-packages/spotipy/client.py", line 266, in _internal_call
    headers=response.headers,
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_tracks=-0kcet4aPpQ%2C05vC8lMeelg%2C0RNDeBcIK00:
 invalid request, reason: None
Originally created by @lohriialo on GitHub (May 31, 2021). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/686 `spotipy version = 2.18.0` ``` client_credentials_manager = SpotifyClientCredentials(SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET) api = spotipy.Spotify(client_credentials_manager=client_credentials_manager) ``` this works ``` random_artists = ["0C8ZW7ezQVs4URX5aX7Kqx", "1SupJlEpv7RS2tPNRaHViT"] recommendation = api.recommendations(seed_artists=random_artists) for track in recommendation['tracks']: print(track['name'], '-', track['artists'][0]['name']) ``` but this doesn't ``` random_tracks = ["-0kcet4aPpQ", "0RNDeBcIK00"] recommendation = api.recommendations(seed_tracks=random_tracks) for track in recommendation['tracks']: print(track['name'], '-', track['artists'][0]['name']) ``` or a combination of both doesn't work either ``` recommendation = api.recommendations(seed_artists=random_artists, seed_tracks=random_tracks) for track in recommendation['tracks']: print(track['name'], '-', track['artists'][0]['name']) ``` Error is : ``` HTTP Error for GET to https://api.spotify.com/v1/recommendations returned 400 due to invalid request Traceback (most recent call last): File "/Users/test/PycharmProjects/test/venv/lib/python3.6/site-packages/spotipy/client.py", line 245, in _internal_call response.raise_for_status() File "/Users/test/PycharmProjects/test/venv/lib/python3.6/site-packages/requests/models.py", line 862, 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_tracks=-0kcet4aPpQ%2C05vC8lMeelg%2C0RNDeBcIK00 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/test/PycharmProjects/test/spotify/user_recommendation/get_user_recommendation_old.py", line 37, in <module> recommendation = sp.recommendations(seed_tracks=random_tracks) File "/Users/test/PycharmProjects/test/venv/lib/python3.6/site-packages/spotipy/client.py", line 1652, in recommendations return self._get("recommendations", **params) File "/Users/test/PycharmProjects/test/venv/lib/python3.6/site-packages/spotipy/client.py", line 291, in _get return self._internal_call("GET", url, payload, kwargs) File "/Users/test/PycharmProjects/test/venv/lib/python3.6/site-packages/spotipy/client.py", line 266, in _internal_call headers=response.headers, spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_tracks=-0kcet4aPpQ%2C05vC8lMeelg%2C0RNDeBcIK00: invalid request, reason: None ```
kerem 2026-02-27 23:22:27 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@stephanebruckert commented on GitHub (May 31, 2021):

@lohriialo your random_tracks don't seem to be valid?

See an example in the tests
github.com/plamere/spotipy@25f9df2dd5/tests/integration/test_non_user_endpoints.py (L85)

github.com/plamere/spotipy@25f9df2dd5/tests/integration/test_non_user_endpoints.py (L25)

<!-- gh-comment-id:851354325 --> @stephanebruckert commented on GitHub (May 31, 2021): @lohriialo your `random_tracks` don't seem to be valid? See an example in the tests https://github.com/plamere/spotipy/blob/25f9df2dd5a223903299aa1c0519186dc6d3b18e/tests/integration/test_non_user_endpoints.py#L85 https://github.com/plamere/spotipy/blob/25f9df2dd5a223903299aa1c0519186dc6d3b18e/tests/integration/test_non_user_endpoints.py#L25
Author
Owner

@lohriialo commented on GitHub (May 31, 2021):

Hmm i'm not sure docs says to give a list of tracks IDs, those are valid track IDs, so I just pass those tracks just as artist list

<!-- gh-comment-id:851356549 --> @lohriialo commented on GitHub (May 31, 2021): Hmm i'm not sure [docs says](https://spotipy.readthedocs.io/en/2.18.0/#spotipy.client.Spotify.recommendations) to give a list of tracks IDs, those are valid track IDs, so I just pass those tracks just as artist list
Author
Owner

@lohriialo commented on GitHub (May 31, 2021):

Aah ok, got it, my track IDs are invalid!

<!-- gh-comment-id:851357504 --> @lohriialo commented on GitHub (May 31, 2021): Aah ok, got it, my track IDs are invalid!
Author
Owner

@lohriialo commented on GitHub (May 31, 2021):

Thanks for the quick pointer!

<!-- gh-comment-id:851358020 --> @lohriialo commented on GitHub (May 31, 2021): Thanks for the quick pointer!
Author
Owner

@stephanebruckert commented on GitHub (May 31, 2021):

no worries!

<!-- gh-comment-id:851358224 --> @stephanebruckert commented on GitHub (May 31, 2021): no worries!
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#406
No description provided.