[GH-ISSUE #929] missed artists genres list in tracks calling api #558

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

Originally created by @mase-git on GitHub (Jan 8, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/929

Describe the bug
According to the official documentation, calling the GET API tracks/?id=(list of ids), it retrieves a collection of tracks info related to the ids list in input. In these information, we can retrieves some data about tracks artists and albums. However, in the JSON response, there are some missed fields such as "genres" in the artists key.

An example of a piece of the main structure of the JSON response from the official documentation is:

    "artists": [
        {
            "external_urls": {
                "spotify": "string"
            },
            "followers": {
                "href": "string",
                "total": 0
            },
            "genres": [ 
                "Prog rock",
                "Grunge"
            ]
        }]

Spotipy should implement the tracks/?id invocation with the following function:

def tracks(self, tracks, market=None):
    """ returns a list of tracks given a list of track IDs, URIs, or URLs

        Parameters:
            - tracks - a list of spotify URIs, URLs or IDs. Maximum: 50 IDs.
            - market - an ISO 3166-1 alpha-2 country code.
    """

    tlist = [self._get_id("track", t) for t in tracks]
    return self._get("tracks/?ids=" + ",".join(tlist), market=market)`

Unfortunately, the response follows an array of the Get Track call JSON response and not the Several Tracks call API output.

Your code
I tested the bug with a customized version of examples/show_track.py script:

from spotipy.oauth2 import SpotifyClientCredentials
import sys
import spotipy

if __name__ == '__main__':
    max_tracks_per_call = 50
    if len(sys.argv) > 1:
        file = open(sys.argv[1])
    else:
        file = sys.stdin
    tids = file.read().split()

    client_credentials_manager = SpotifyClientCredentials()
    sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
    for start in range(0, len(tids), max_tracks_per_call):
        results = sp.tracks(tids[start: start + max_tracks_per_call])
        for track in results['tracks']:
            print(track['name'] + ' - ' + track['artists'][0]['name'])
            print(track['name']  + ' -  Genres: ' + track['artists'][0]['genres'][0])  #KeyError 'genres'


Expected behavior
Output with the JSON structure defined in the official Spotify documentation.

Output

Bat Country - Emancipator
Traceback (most recent call last):
  File "/Users/kode/Desktop/spotipy/examples/show_tracks.py", line 24, in <module>
    print(track['name']  + ' -  Genres: ' + track['artists'][0]['genres'][0])  #KeyError 'genres'
KeyError: 'genres'

Environment:

  • OS: MacOS Ventura Version 13.1
  • Python version 3.10.8
  • spotipy version 2.22.0

Additional context
I think that the error occurs for a changing of version in Spotify API and a misalignment with the Spotipy function invocation.

Originally created by @mase-git on GitHub (Jan 8, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/929 **Describe the bug** According to the [official documentation](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-several-tracks), calling the GET API tracks/?id=(list of ids), it retrieves a collection of tracks info related to the ids list in input. In these information, we can retrieves some data about tracks artists and albums. However, in the JSON response, there are some missed fields such as _"genres"_ in the artists key. An example of a piece of the main structure of the JSON response from the official documentation is: ``` "artists": [ { "external_urls": { "spotify": "string" }, "followers": { "href": "string", "total": 0 }, "genres": [ "Prog rock", "Grunge" ] }] ``` Spotipy should implement the _tracks/?id_ invocation with the following function: def tracks(self, tracks, market=None): """ returns a list of tracks given a list of track IDs, URIs, or URLs Parameters: - tracks - a list of spotify URIs, URLs or IDs. Maximum: 50 IDs. - market - an ISO 3166-1 alpha-2 country code. """ tlist = [self._get_id("track", t) for t in tracks] return self._get("tracks/?ids=" + ",".join(tlist), market=market)` Unfortunately, the response follows an array of the [Get Track call](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-track) JSON response and not the [Several Tracks ](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-several-tracks)call API output. **Your code** I tested the bug with a customized version of _examples/show_track.py_ script: ``` from spotipy.oauth2 import SpotifyClientCredentials import sys import spotipy if __name__ == '__main__': max_tracks_per_call = 50 if len(sys.argv) > 1: file = open(sys.argv[1]) else: file = sys.stdin tids = file.read().split() client_credentials_manager = SpotifyClientCredentials() sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) for start in range(0, len(tids), max_tracks_per_call): results = sp.tracks(tids[start: start + max_tracks_per_call]) for track in results['tracks']: print(track['name'] + ' - ' + track['artists'][0]['name']) print(track['name'] + ' - Genres: ' + track['artists'][0]['genres'][0]) #KeyError 'genres' ``` **Expected behavior** Output with the JSON structure defined in the official Spotify documentation. **Output** ``` Bat Country - Emancipator Traceback (most recent call last): File "/Users/kode/Desktop/spotipy/examples/show_tracks.py", line 24, in <module> print(track['name'] + ' - Genres: ' + track['artists'][0]['genres'][0]) #KeyError 'genres' KeyError: 'genres' ``` **Environment:** - OS: MacOS Ventura Version 13.1 - Python version 3.10.8 - spotipy version 2.22.0 **Additional context** I think that the error occurs for a changing of version in Spotify API and a misalignment with the Spotipy function invocation.
kerem 2026-02-27 23:23:20 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@stephanebruckert commented on GitHub (Jan 8, 2023):

  • I know that not all artists have genres associated to them. To confirm this is not the problem, can you please share the track ID you are working with?

  • in comparison, do the artist genres correctly appear when using the /track endpoint, aka spotipy.client.Spotify.track?

  • /tracks?ids={ids} seems to already exist github.com/spotipy-dev/spotipy@922d51df02/spotipy/client.py (L347..L356), I'm confused as to why you suggest we should implement it

  • Unfortunately, the response follows an array of the Get Track call JSON response and not the Several Tracks call API output.

    I'm not sure what you mean. Do you have an example? For both endpoints the JSON have similar artist objects

<!-- gh-comment-id:1374920495 --> @stephanebruckert commented on GitHub (Jan 8, 2023): - I know that not all artists have genres associated to them. To confirm this is not the problem, can you please share the track ID you are working with? - in comparison, do the artist genres correctly appear when using the `/track` endpoint, aka [`spotipy.client.Spotify.track`](https://spotipy.readthedocs.io/en/2.22.0/#spotipy.client.Spotify.track)? - `/tracks?ids={ids}` seems to already exist https://github.com/spotipy-dev/spotipy/blob/922d51df02536171db2087f562695bc7884652f2/spotipy/client.py#L347..L356, I'm confused as to why you suggest we should implement it - > Unfortunately, the response follows an array of the [Get Track call](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-track) JSON response and not the [Several Tracks ](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-several-tracks)call API output. I'm not sure what you mean. Do you have an example? For both endpoints the JSON have similar `artist` objects
Author
Owner

@mase-git commented on GitHub (Jan 8, 2023):

{
    "artists": [{"external_urls": {"spotify": "https://open.spotify.com/artist/3TVXtAsR1Inumwj472S9r4"},
    "href": "https://api.spotify.com/v1/artists/3TVXtAsR1Inumwj472S9r4",
    "id": "3TVXtAsR1Inumwj472S9r4",
    "name": "Drake",
    "type": "artist",
    "uri": "spotify:artist:3TVXtAsR1Inumwj472S9r4"},
   {"external_urls": {"spotify": "https://open.spotify.com/artist/1URnnhqYAYcrqrcwql10ft"},
    "href": "https://api.spotify.com/v1/artists/1URnnhqYAYcrqrcwql10ft",
    "id": "1URnnhqYAYcrqrcwql10ft",
    "name": "21 Savage",
    "type": "artist",
    "uri": "spotify:artist:1URnnhqYAYcrqrcwql10ft"}]

}

Meanwhile, the artists attribute of the spotipy.client.Spotify.tracks invocation obtains:

```

"artists": [
        {
            "external_urls": {
                "spotify": "https: //open.spotify.com/artist/3TVXtAsR1Inumwj472S9r4"
            },
            "href": "https://api.spotify.com/v1/artists/3TVXtAsR1Inumwj472S9r4",
            "id": "3TVXtAsR1Inumwj472S9r4",
            "name": "Drake",
            "type": "artist",
            "uri": "spotify:artist:3TVXtAsR1Inumwj472S9r4"
        },
        {
            "external_urls": {
                "spotify": "https://open.spotify.com/artist/1URnnhqYAYcrqrcwql10ft"
            },
            "href": "https://api.spotify.com/v1/artists/1URnnhqYAYcrqrcwql10ft",
            "id": "1URnnhqYAYcrqrcwql10ft",
            "name": "21 Savage",
            "type": "artist",
            "uri": "spotify:artist:1URnnhqYAYcrqrcwql10ft"
        }
    ]

However, the JSON response for /tracks/?id={ids} from the official Spotify documentation is:

"artists": [
        {
          "external_urls": {
            "spotify": "string"
          },
          "followers": {
            "href": "string",
            "total": 0
          },
          "genres": [
            "Prog rock",
            "Grunge"
          ],
          "href": "string",
          "id": "string",
          "images": [
            {
              "url": "https://i.scdn.co/image/ab67616d00001e02ff9ca10b55ce82ae553c8228\n",
              "height": 300,
              "width": 300
            }
          ],
          "name": "string",
          "popularity": 0,
          "type": "artist",
          "uri": "string"
        }
      ]

I am not sure that the problem is related to the Spotipy side.

<!-- gh-comment-id:1374933530 --> @mase-git commented on GitHub (Jan 8, 2023): - I tried with Drake on the "Rich Flex" track. Related IDs info are: - Track of "Rich Flex": `spotify:track:1bDbXMyjaUIooNwFE9wn0N` - Artist ID of Drake: `spotify:artist:3TVXtAsR1Inumwj472S9r4` If you give the artist ID in input to [examples/show_artists.py](https://github.com/spotipy-dev/spotipy/blob/master/examples/show_artist.py), you can check that genres attribute exists for it. - I tried with the [spotipy.client.Spotify.track](https://spotipy.readthedocs.io/en/2.22.0/#spotipy.client.Spotify.track), the result follows the JSON response of the [/track/{id} ](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-track)API where the genres key is not part of the artists attributes. Meanwhile, the previous key should be part of JSON response information for the [/tracks/?ids={ids}](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-several-tracks). - I wasn't so clear here, I am sorry. I know that https://github.com/spotipy-dev/spotipy/blob/922d51df02536171db2087f562695bc7884652f2/spotipy/client.py#L347..L356 implements the function that I wrote above. I mean that the JSON response of [spotipy.client.Spotify.tracks](https://github.com/spotipy-dev/spotipy/blob/922d51df02536171db2087f562695bc7884652f2/spotipy/client.py#L347..L356) didn't match the JSON structure defined in the [Spotify API documentation](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-several-tracks) - The artists attribute of the JSON response in the `spotipy.client.Spotify.track` invocation is: ``` { "artists": [{"external_urls": {"spotify": "https://open.spotify.com/artist/3TVXtAsR1Inumwj472S9r4"}, "href": "https://api.spotify.com/v1/artists/3TVXtAsR1Inumwj472S9r4", "id": "3TVXtAsR1Inumwj472S9r4", "name": "Drake", "type": "artist", "uri": "spotify:artist:3TVXtAsR1Inumwj472S9r4"}, {"external_urls": {"spotify": "https://open.spotify.com/artist/1URnnhqYAYcrqrcwql10ft"}, "href": "https://api.spotify.com/v1/artists/1URnnhqYAYcrqrcwql10ft", "id": "1URnnhqYAYcrqrcwql10ft", "name": "21 Savage", "type": "artist", "uri": "spotify:artist:1URnnhqYAYcrqrcwql10ft"}] } ``` Meanwhile, the artists attribute of the `spotipy.client.Spotify.tracks` invocation obtains: ``` "artists": [ { "external_urls": { "spotify": "https: //open.spotify.com/artist/3TVXtAsR1Inumwj472S9r4" }, "href": "https://api.spotify.com/v1/artists/3TVXtAsR1Inumwj472S9r4", "id": "3TVXtAsR1Inumwj472S9r4", "name": "Drake", "type": "artist", "uri": "spotify:artist:3TVXtAsR1Inumwj472S9r4" }, { "external_urls": { "spotify": "https://open.spotify.com/artist/1URnnhqYAYcrqrcwql10ft" }, "href": "https://api.spotify.com/v1/artists/1URnnhqYAYcrqrcwql10ft", "id": "1URnnhqYAYcrqrcwql10ft", "name": "21 Savage", "type": "artist", "uri": "spotify:artist:1URnnhqYAYcrqrcwql10ft" } ] However, the JSON response for` /tracks/?id={ids}` from the official Spotify documentation is: ``` "artists": [ { "external_urls": { "spotify": "string" }, "followers": { "href": "string", "total": 0 }, "genres": [ "Prog rock", "Grunge" ], "href": "string", "id": "string", "images": [ { "url": "https://i.scdn.co/image/ab67616d00001e02ff9ca10b55ce82ae553c8228\n", "height": 300, "width": 300 } ], "name": "string", "popularity": 0, "type": "artist", "uri": "string" } ] ``` I am not sure that the problem is related to the Spotipy side.
Author
Owner

@stephanebruckert commented on GitHub (Jan 8, 2023):

OK I see. My understanding is this is a bug on the Spotify API side, not Python related.

The same happens if you try it in the console with ID 1bDbXMyjaUIooNwFE9wn0N https://developer.spotify.com/console/get-track/

"artists": [
    {
      "external_urls": {
        "spotify": "https://open.spotify.com/artist/3TVXtAsR1Inumwj472S9r4"
      },
      "href": "https://api.spotify.com/v1/artists/3TVXtAsR1Inumwj472S9r4",
      "id": "3TVXtAsR1Inumwj472S9r4",
      "name": "Drake",
      "type": "artist",
      "uri": "spotify:artist:3TVXtAsR1Inumwj472S9r4"
    },

This would need to be reported at https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer

Perhaps they voluntarily include a minimised artist object inside the tracks, and they forgot to update the doc.

<!-- gh-comment-id:1374936338 --> @stephanebruckert commented on GitHub (Jan 8, 2023): OK I see. My understanding is this is a bug on the Spotify API side, not Python related. The same happens if you try it in the console with ID `1bDbXMyjaUIooNwFE9wn0N` https://developer.spotify.com/console/get-track/ ``` "artists": [ { "external_urls": { "spotify": "https://open.spotify.com/artist/3TVXtAsR1Inumwj472S9r4" }, "href": "https://api.spotify.com/v1/artists/3TVXtAsR1Inumwj472S9r4", "id": "3TVXtAsR1Inumwj472S9r4", "name": "Drake", "type": "artist", "uri": "spotify:artist:3TVXtAsR1Inumwj472S9r4" }, ``` This would need to be reported at https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer Perhaps they voluntarily include a minimised artist object inside the tracks, and they forgot to update the doc.
Author
Owner

@mase-git commented on GitHub (Jan 8, 2023):

Oh ok, I didn't notice that. Thank you for the clarification! 👌

<!-- gh-comment-id:1374936822 --> @mase-git commented on GitHub (Jan 8, 2023): Oh ok, I didn't notice that. Thank you for the clarification! 👌
Author
Owner

@mase-git commented on GitHub (Jan 12, 2023):

Update:

According to the official Spotify Documentation, an alternative solution for this bug is search by artist name and focus on the artist genre. It can't solve completely the problem, however it guarantee a genre type to associate with the artist and/or album. This kind of information is pretty useful in some data analysis project where the track and artist genre can have some statistical correlation and provides interesting insights.

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials # to access authorised Spotify data
from env import Environment # class to put id and secret key of your Spotify API project

client_id = Environment.spotipy_client_id
client_secret = Environment.spotipy_client_secret
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)

sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
result = sp.search("AJR") # search by artist name
track = result['tracks']['items'][0]

artist = sp.artist(track["artists"][0]["external_urls"]["spotify"])
print("artist genres:", artist["genres"])

album = sp.album(track["album"]["external_urls"]["spotify"])
print("album genres:", album["genres"])
print("album release-date:", album["release_date"])
<!-- gh-comment-id:1380730729 --> @mase-git commented on GitHub (Jan 12, 2023): Update: According to the official Spotify Documentation, an alternative solution for this bug is _search by artist name_ and focus on the artist genre. It can't solve completely the problem, however it guarantee a genre type to associate with the artist and/or album. This kind of information is pretty useful in some data analysis project where the track and artist genre can have some statistical correlation and provides interesting insights. ``` import spotipy from spotipy.oauth2 import SpotifyClientCredentials # to access authorised Spotify data from env import Environment # class to put id and secret key of your Spotify API project client_id = Environment.spotipy_client_id client_secret = Environment.spotipy_client_secret client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret) sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) result = sp.search("AJR") # search by artist name track = result['tracks']['items'][0] artist = sp.artist(track["artists"][0]["external_urls"]["spotify"]) print("artist genres:", artist["genres"]) album = sp.album(track["album"]["external_urls"]["spotify"]) print("album genres:", album["genres"]) print("album release-date:", album["release_date"]) ```
Author
Owner

@stephanebruckert commented on GitHub (Jan 16, 2023):

Yep! Thanks for the feedback

I've created a quick discussion in Spotify community https://community.spotify.com/t5/Spotify-for-Developers/Track-endpoints-not-returning-all-artist-fields-as-documented/m-p/5489383#M7675

I'll close the issue as there is nothing that spotipy can do about it now or in the future.

<!-- gh-comment-id:1384560707 --> @stephanebruckert commented on GitHub (Jan 16, 2023): Yep! Thanks for the feedback I've created a quick discussion in Spotify community https://community.spotify.com/t5/Spotify-for-Developers/Track-endpoints-not-returning-all-artist-fields-as-documented/m-p/5489383#M7675 I'll close the issue as there is nothing that spotipy can do about it now or in the future.
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#558
No description provided.