Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

AndydeCleyre opened this issue Sep 4, 2015 · 5 comments


Copy link

@AndydeCleyre AndydeCleyre commented Sep 4, 2015


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 "./", line 115, in <module>
  File "./", line 53, in new_playlist_from_track_ids
  File "/usr/lib/python3.4/", line 708, in append
    self.insert(len(self), value)
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/", line 393, in insert
    self[index:index] = [value]
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/", line 219, in __setitem__
    self.add_new_playlist(val, index=i)
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/", line 58, in wrapper
    return f(*args, **kwargs)
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/", line 261, in add_new_playlist
    self.move_playlist(self.__len__() - 1, index)
  File "/home/<user/project>/env/lib/python3.4/site-packages/spotify/", 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/", 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

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.loop.start() = 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) = 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:
  ['logged in'].set()

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

    def new_playlist_from_track_ids(self, track_ids, name=None):
        name = name or str(time())['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['end of track'].wait(0.1):

    def play_track_uri(self, track_uri, block=False):['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): != PlayerState.PLAYING)

if __name__ == '__main__':
    sp = SpotifyPlayer()
Copy link

@AndydeCleyre AndydeCleyre commented Sep 4, 2015

Amazing, thank you!

Copy link

@jodal jodal commented Sep 4, 2015

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

Copy link

@AndydeCleyre AndydeCleyre commented Sep 5, 2015

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.

Copy link

@jodal 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.


Copy link

@AndydeCleyre AndydeCleyre commented Sep 5, 2015

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
None yet

No branches or pull requests

2 participants