[GH-ISSUE #739] KeyError: 'items' #447

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

Originally created by @Ovcheric on GitHub (Nov 4, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/739

Trying to learn how to use Spotipy to create a text file with all songs and corresponding artist from a playlist.
When using the example "Playlist Tracks", I copied and pasted the code. When I run it, I get an error: KeyError: 'items'
I have the link to the playlist, and I have authorization access. I just dont understand how to pull information from the massive wall of text I get when getting information about songs in a playlist.

Originally created by @Ovcheric on GitHub (Nov 4, 2021). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/739 Trying to learn how to use Spotipy to create a text file with all songs and corresponding artist from a playlist. When using the example "Playlist Tracks", I copied and pasted the code. When I run it, I get an error: `KeyError: 'items'` I have the link to the playlist, and I have authorization access. I just dont understand how to pull information from the massive wall of text I get when getting information about songs in a playlist.
kerem closed this issue 2026-02-27 23:22:41 +03:00
Author
Owner

@Peter-Schorn commented on GitHub (Nov 4, 2021):

post the stack trace for the error.

<!-- gh-comment-id:960491054 --> @Peter-Schorn commented on GitHub (Nov 4, 2021): post the stack trace for the error.
Author
Owner

@Ovcheric commented on GitHub (Nov 17, 2021):

Traceback (most recent call last): File "C:\Users\ovche\OneDrive\Desktop\Auto Spotify Playlist\AutoSpotifyPlaylist.py", line 49, in <module> playlist = sp.playlist_items(playlist_id)["items"] KeyError: 'items'

<!-- gh-comment-id:971215849 --> @Ovcheric commented on GitHub (Nov 17, 2021): `Traceback (most recent call last): File "C:\Users\ovche\OneDrive\Desktop\Auto Spotify Playlist\AutoSpotifyPlaylist.py", line 49, in <module> playlist = sp.playlist_items(playlist_id)["items"] KeyError: 'items'`
Author
Owner

@Ovcheric commented on GitHub (Dec 31, 2021):

If it helps, the error was in the fact that I was looking for 'items' in an array where that attribute did not exist.

I solved this issue by meticulously following along in the nested dictionary/list that you get when you do pprint(sp.playlist_items(playlistID)).

The CORRECT code if you are looking for the name of the track and artist is:

Name of the track at the top of the playlist:
sp.playlist_items(playlistID)['tracks']['items'][0]['track']['name']

Artist of the track at the top of the playlist:
sp.playlist_items(playlistID)['tracks']['items'][0]['track']['artists'][0]['name']

And if you want to iterate through the entire playlist, just use a for loop:

    for i, item in enumerate(sp.playlist_items(playlistID)['tracks']['items']):
        print(item['track']['name']) #name of the track
        print(item['track']['artists'][0]['name']) #artist of the track
<!-- gh-comment-id:1003279283 --> @Ovcheric commented on GitHub (Dec 31, 2021): If it helps, the error was in the fact that I was looking for 'items' in an array where that attribute did not exist. I solved this issue by meticulously following along in the nested dictionary/list that you get when you do `pprint(sp.playlist_items(playlistID))`. The CORRECT code if you are looking for the name of the track and artist is: Name of the track at the top of the playlist: `sp.playlist_items(playlistID)['tracks']['items'][0]['track']['name']` Artist of the track at the top of the playlist: `sp.playlist_items(playlistID)['tracks']['items'][0]['track']['artists'][0]['name']` And if you want to iterate through the entire playlist, just use a for loop: ``` for i, item in enumerate(sp.playlist_items(playlistID)['tracks']['items']): print(item['track']['name']) #name of the track print(item['track']['artists'][0]['name']) #artist of the track ```
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#447
No description provided.