[GH-ISSUE #1033] Querying Spotify for song details is not returning with the 'Track ID'. #618

Closed
opened 2026-02-28 00:00:18 +03:00 by kerem · 7 comments
Owner

Originally created by @Artholos on GitHub (Oct 18, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1033

Describe the bug
Querying Spotify for song details is not returning with the 'Track ID'. Here's my code to search for popular songs in various genres. I need three details about each track: Name, artist, and track ID. But the track ID is not being returned. According to the documentation on https://developer.spotify.com/documentation/web-api/reference/search shows that the track ID located in: 'tracks > items > id'

Here's my function that returns list of top 10 songs in a genre:

def find_popular_song(The_G = 'any', The_C = 'US', EyeLimit = 10):
    print("Finding a popular song...")
    # Get a list of songs from Spotify
    if The_G == 'any':
        playlist_id = '37i9dQZEVXbMDoHDwVN2tF'
    else:
        playlists = sp.category_playlists(category_id=The_G, country=The_C, limit=1)
        playlist_id = playlists["playlists"]["items"][0]["id"]
    results = sp.playlist(playlist_id, fields="tracks.items(track(name, artists(name)))")
    print(f"Results found: \n\n{results}")
    print(f"Building Arrays...")
    song_names = []
    song_ids = []
    artist_names = []
    for i in range(EyeLimit):
        song_names.append(results["tracks"]["items"][i]["track"]["name"])
        song_ids.append(results["tracks"]["items"][i]["id"])
        artist_names.append(results["tracks"]["items"][i]["track"]["artists"][0]["name"])

    # Query the database for matching songs
    print(f"Checking the database for any matches...")
    TheEye = 0
    for sn in song_names:
        if not is_song_in_database(song_names[sn], artist_names[sn]) and tempo_check(song_ids[sn]):
            The_Song = [song_names[sn], song_ids[sn], artist_names[sn]]
            print(f"{song_names[sn]} by {artist_names[sn]} is ready going to get the BSP Treatment! Congriggles!")
            return The_Song
        if TheEye >= EyeLimit:
            print(f"No songs found in the top {EyeLimit} for the genre: {The_G}")
            return 'no songs'
        TheEye += 1

Expected behavior
I expect that the track ID would be returned so it can be used later in other Spotify API calls.

Output

Exception has occurred: KeyError
'id'
  File "D:\My-Program-Files\ICBS\Spotify-Functions.py", line 72, in find_popular_song
    song_ids.append(results["tracks"]["items"][i]["id"])
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "D:\My-Program-Files\ICBS\Spotify-Functions.py", line 122, in <module>
    The_Goods = find_popular_song()
                ^^^^^^^^^^^^^^^^^^^
KeyError: 'id'

image

Environment:

  • OS: Windows 10
  • Python version: 3.11.5 venv
  • spotipy version: 2.23.0 (Latest)
  • your IDE: VS Code
Originally created by @Artholos on GitHub (Oct 18, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1033 **Describe the bug** Querying Spotify for song details is not returning with the 'Track ID'. Here's my code to search for popular songs in various genres. I need three details about each track: Name, artist, and track ID. But the track ID is not being returned. According to the documentation on https://developer.spotify.com/documentation/web-api/reference/search shows that the track ID located in: 'tracks > items > id' Here's my function that returns list of top 10 songs in a genre: ```python3 def find_popular_song(The_G = 'any', The_C = 'US', EyeLimit = 10): print("Finding a popular song...") # Get a list of songs from Spotify if The_G == 'any': playlist_id = '37i9dQZEVXbMDoHDwVN2tF' else: playlists = sp.category_playlists(category_id=The_G, country=The_C, limit=1) playlist_id = playlists["playlists"]["items"][0]["id"] results = sp.playlist(playlist_id, fields="tracks.items(track(name, artists(name)))") print(f"Results found: \n\n{results}") print(f"Building Arrays...") song_names = [] song_ids = [] artist_names = [] for i in range(EyeLimit): song_names.append(results["tracks"]["items"][i]["track"]["name"]) song_ids.append(results["tracks"]["items"][i]["id"]) artist_names.append(results["tracks"]["items"][i]["track"]["artists"][0]["name"]) # Query the database for matching songs print(f"Checking the database for any matches...") TheEye = 0 for sn in song_names: if not is_song_in_database(song_names[sn], artist_names[sn]) and tempo_check(song_ids[sn]): The_Song = [song_names[sn], song_ids[sn], artist_names[sn]] print(f"{song_names[sn]} by {artist_names[sn]} is ready going to get the BSP Treatment! Congriggles!") return The_Song if TheEye >= EyeLimit: print(f"No songs found in the top {EyeLimit} for the genre: {The_G}") return 'no songs' TheEye += 1 ``` **Expected behavior** I expect that the track ID would be returned so it can be used later in other Spotify API calls. **Output** ``` Exception has occurred: KeyError 'id' File "D:\My-Program-Files\ICBS\Spotify-Functions.py", line 72, in find_popular_song song_ids.append(results["tracks"]["items"][i]["id"]) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^ File "D:\My-Program-Files\ICBS\Spotify-Functions.py", line 122, in <module> The_Goods = find_popular_song() ^^^^^^^^^^^^^^^^^^^ KeyError: 'id' ``` ![image](https://github.com/spotipy-dev/spotipy/assets/135184826/a016e56e-3ac5-47e2-bf8f-86c443d02e51) **Environment:** - OS: Windows 10 - Python version: 3.11.5 venv - spotipy version: 2.23.0 (Latest) - your IDE: VS Code
kerem 2026-02-28 00:00:18 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@dieser-niko commented on GitHub (Nov 3, 2023):

Generally a good idea. I think it would be best to use JSON schemas and have them as python objects.

But I'm not sure if the OpenAPI definition from Spotify is actually representing the API correctly. If I'm not mistaken, there is/was actually a flaw in the definition, but I'll have to check that again sometime.

Edit: found a related issue: https://github.com/spotipy-dev/spotipy/issues/975

<!-- gh-comment-id:1793212689 --> @dieser-niko commented on GitHub (Nov 3, 2023): Generally a good idea. I think it would be best to use JSON schemas and have them as python objects. But I'm not sure if the OpenAPI definition from Spotify is actually representing the API correctly. If I'm not mistaken, there is/was actually a flaw in the definition, but I'll have to check that again sometime. Edit: found a related issue: https://github.com/spotipy-dev/spotipy/issues/975
Author
Owner

@dieser-niko commented on GitHub (May 23, 2024):

I'm currently going through some issues and I'm realizing that I probably commented on the wrong issue. Do you still need help or can we close this here?

<!-- gh-comment-id:2126657343 --> @dieser-niko commented on GitHub (May 23, 2024): I'm currently going through some issues and I'm realizing that I probably commented on the wrong issue. Do you still need help or can we close this here?
Author
Owner

@Artholos commented on GitHub (May 23, 2024):

Oh heyo, I'm so sorry, I totally forgot. I couldn't find a work around to the issue so I gave up and went to do something else. It looks like you found the related problem, so I think we're good to close this issue.

Thanks so much!

<!-- gh-comment-id:2127019867 --> @Artholos commented on GitHub (May 23, 2024): Oh heyo, I'm so sorry, I totally forgot. I couldn't find a work around to the issue so I gave up and went to do something else. It looks like you found the related problem, so I think we're good to close this issue. Thanks so much!
Author
Owner

@dieser-niko commented on GitHub (May 23, 2024):

It looks like you found the related problem

Yeah no, that's the thing. The related issue doesn't seem to be related to your issue.

<!-- gh-comment-id:2127026705 --> @dieser-niko commented on GitHub (May 23, 2024): > It looks like you found the related problem Yeah no, that's the thing. The related issue doesn't seem to be related to your issue.
Author
Owner

@Artholos commented on GitHub (May 23, 2024):

Alright then, I've dug up the program and the issue is still happening, probably because I haven't changed anything yet. What do you suggest?

<!-- gh-comment-id:2127071165 --> @Artholos commented on GitHub (May 23, 2024): Alright then, I've dug up the program and the issue is still happening, probably because I haven't changed anything yet. What do you suggest?
Author
Owner

@dieser-niko commented on GitHub (May 23, 2024):

I've spotted two issues. The first one is here:

...
    if The_G == 'any':
        playlist_id = '37i9dQZEVXbMDoHDwVN2tF'
    else:
        playlists = sp.category_playlists(category_id=The_G, country=The_C, limit=1)
        playlist_id = playlists["playlists"]["items"][0]["id"]
-    results = sp.playlist(playlist_id, fields="tracks.items(track(name, artists(name)))")
+    results = sp.playlist(playlist_id, fields="tracks.items(track(id, name, artists(name)))")
    print(f"Results found: \n\n{results}")
    print(f"Building Arrays...")
...

It seems that you've filtered out the ID.

The second one here:

...
   for i in range(EyeLimit):
        song_names.append(results["tracks"]["items"][i]["track"]["name"])
-        song_ids.append(results["tracks"]["items"][i]["id"])
+        song_ids.append(results["tracks"]["items"][i]["track"]["id"])
        artist_names.append(results["tracks"]["items"][i]["track"]["artists"][0]["name"])
...

I think that one's self explanatory.

<!-- gh-comment-id:2127132529 --> @dieser-niko commented on GitHub (May 23, 2024): I've spotted two issues. The first one is here: ```diff ... if The_G == 'any': playlist_id = '37i9dQZEVXbMDoHDwVN2tF' else: playlists = sp.category_playlists(category_id=The_G, country=The_C, limit=1) playlist_id = playlists["playlists"]["items"][0]["id"] - results = sp.playlist(playlist_id, fields="tracks.items(track(name, artists(name)))") + results = sp.playlist(playlist_id, fields="tracks.items(track(id, name, artists(name)))") print(f"Results found: \n\n{results}") print(f"Building Arrays...") ... ``` It seems that you've filtered out the ID. The second one here: ```diff ... for i in range(EyeLimit): song_names.append(results["tracks"]["items"][i]["track"]["name"]) - song_ids.append(results["tracks"]["items"][i]["id"]) + song_ids.append(results["tracks"]["items"][i]["track"]["id"]) artist_names.append(results["tracks"]["items"][i]["track"]["artists"][0]["name"]) ... ``` I think that one's self explanatory.
Author
Owner

@Artholos commented on GitHub (May 23, 2024):

おh thank you! I can't believe I missed those haha! I suppose a second set of eyes really makes all the difference. I'm getting a completely new error now down in the database part. Looks like I have a project again! Thank you so much for your help :D

<!-- gh-comment-id:2127151149 --> @Artholos commented on GitHub (May 23, 2024): おh thank you! I can't believe I missed those haha! I suppose a second set of eyes really makes all the difference. I'm getting a completely new error now down in the database part. Looks like I have a project again! Thank you so much for your help :D
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#618
No description provided.