`session.playlist_container.append(some_string)` ends with `spotify.error.LibError: Invalid input` #175

AndydeCleyre opened this Issue Sep 4, 2015 · 5 comments


None yet

2 participants



I'm trying to get the hang of pyspotify 2.0.2, and am probably doing a few things wrong. My current goal is to create a new playlist populated with tracks. When it hits session.playlist_container.append I get the following traceback:

Traceback (most recent call last):
  File "./make_spotify_playlist.py", line 115, in <module>
  File "./make_spotify_playlist.py", line 53, in new_playlist_from_track_ids
  File "/usr/lib/python3.4/_collections_abc.py", line 708, in append
    self.insert(len(self), value)
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/playlist_container.py", line 393, in insert
    self[index:index] = [value]
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/playlist_container.py", line 219, in __setitem__
    self.add_new_playlist(val, index=i)
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/__init__.py", line 58, in wrapper
    return f(*args, **kwargs)
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/playlist_container.py", line 261, in add_new_playlist
    self.move_playlist(self.__len__() - 1, index)
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/playlist_container.py", line 361, in move_playlist
    self._sp_playlistcontainer, from_index, to_index, int(dry_run)))
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/error.py", line 31, in maybe_raise
    raise LibError(error_type)
spotify.error.LibError: Invalid input

Here are the entire contents of the offending script:

#!/usr/bin/env python3

# This is a modified copy from the pyspotify examples folder
# Original license can be read at https://github.com/mopidy/pyspotify/blob/v2.x/develop/LICENSE

from sys import argv
from threading import Event
from collections import defaultdict
from contextlib import suppress
from time import time

from spotify import Session, EventLoop, SessionEvent, AlsaSink, ConnectionState, PlayerState

from _private import USER, PASSWORD

class SpotifyPlayer:

    def __init__(self, username=USER, password=PASSWORD, track_id=None, track_uri=None, session=None, event_loop=None, events=None):
        self.session = session or Session()

        self.loop = event_loop or EventLoop(self.session)
        if not event_loop:
        self.events = events or defaultdict(Event)

        self.session.on(SessionEvent.CONNECTION_STATE_UPDATED, self.on_connection_state_updated)
        self.session.on(SessionEvent.END_OF_TRACK, self.on_end_of_track)

        self.session.login(username, password)
        self.audio = AlsaSink(self.session)

        if track_id:
        elif track_uri:

    def on_connection_state_updated(self, session):
        if self.session.connection.state is ConnectionState.LOGGED_IN:
            self.events['logged in'].set()

    def on_end_of_track(self, session):
        self.events['end of track'].set()

    def new_playlist_from_track_ids(self, track_ids, name=None):
        name = name or str(time())
        self.events['logged in'].wait()#blocks!
        playlist = self.session.playlist_container[-1]
        playlist.add_tracks([self.session.get_track('spotify:track:' + t_id) for t_id in track_ids])
        return playlist

    def block_until_end_of_track(self):
        with suppress(KeyboardInterrupt):
            while not self.events['end of track'].wait(0.1):

    def play_track_uri(self, track_uri, block=False):
        self.events['logged in'].wait()
        track = self.session.get_track(track_uri).load()
        if block:

    def play_track_id(self, track_id, block=False):
        self.play_track_uri('spotify:track:' + track_id, block)

    def pause(self):
        self.session.player.play(session.player.state != PlayerState.PLAYING)

if __name__ == '__main__':
    sp = SpotifyPlayer()
@jodal jodal closed this in 66e7da1 Sep 4, 2015

Amazing, thank you!

jodal commented Sep 4, 2015

Thanks to you! Easy to fix bugs when you get both a stack trace and a script to reproduce :-)


So this now returns a Playlist which has a URI, but the playlist does not appear on my Spotify account, as checked on the Android app, Mopify, and the Spotify web player. I looked through the Playlist docs, but I haven't found any extra steps mentioned for creating a new playlist.

jodal commented Sep 5, 2015

You'll need to make sure the playlist and its changes are synced to the server before quitting your program. Since you have an EventLoop running, it should already call process_events() for you, so that is taken care of. Also, it is a good idea to logout properly and wait for the logged out event before quitting the process.

Relevant parts of the API:

Untested pseudo code:

def on_logged_out(session):

session.on(SessionEvent.LOGGED_OUT, on_logged_out)

while playlist.has_pending_changes:
    session.process_events()  # The event loop does this, so you can probably just sleep a bit instead.


Perfect, thanks

@jodal jodal added the bug label Sep 5, 2015
@jodal jodal self-assigned this Sep 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment