[GH-ISSUE #69] get_library_albums and get_library_songs fail if library is empty #51

Closed
opened 2026-02-27 22:07:45 +03:00 by kerem · 5 comments
Owner

Originally created by @tadly on GitHub (Aug 28, 2020).
Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/69

Calls like...

  • YTMusic.get_library_albums
  • YTMusic.get_library_songs
  • likely many more, haven't gotten that far yet

...often result in KeyErrors for various reasons.

YTMusic.get_library_albums

If there are 0 albums in the library, YTMusic.get_library_albums throws KeyError: 'items' because of this
Possible fix: results.get('items', [])

YTMusic.get_library_songs

also throws KeyError: 'items' because of this
Possible fix: items.extend(parse_func(results.get(continuation_contents, [])))

I'm sure there are many more places where problems like this would appear.
It would make sense to either do more investigating as to why those keys don't exist in certain conditions and properly work around it or use default values via .get() as outlined above.

Sadly I'm way to busy with other stuff to proved a PR. This issue is all I can do for now (and the foreseeable future)

Originally created by @tadly on GitHub (Aug 28, 2020). Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/69 Calls like... * `YTMusic.get_library_albums` * `YTMusic.get_library_songs` * likely many more, haven't gotten that far yet ...often result in `KeyError`s for various reasons. #### `YTMusic.get_library_albums` If there are 0 albums in the library, `YTMusic.get_library_albums` throws `KeyError: 'items'` because of [this](https://github.com/sigma67/ytmusicapi/blob/09daac66fc1a6e6a55854a21c92b8b7e861a793f/ytmusicapi/mixins/library.py#L92) Possible fix: `results.get('items', [])` #### `YTMusic.get_library_songs` also throws `KeyError: 'items'` because of [this](https://github.com/sigma67/ytmusicapi/blob/09daac66fc1a6e6a55854a21c92b8b7e861a793f/ytmusicapi/parsers/utils.py#L60) Possible fix: `items.extend(parse_func(results.get(continuation_contents, [])))` I'm sure there are many more places where problems like this would appear. It would make sense to either do more investigating as to why those keys don't exist in certain conditions and properly work around it or use default values via `.get()` as outlined above. Sadly I'm way to busy with other stuff to proved a PR. This issue is all I can do for now (and the foreseeable future)
kerem closed this issue 2026-02-27 22:07:45 +03:00
Author
Owner

@kawaiiDango commented on GitHub (Sep 10, 2020):

I got a key error too and get_library_upload_songs(limit=100000) sometimes fails. Not sure if this is a rate limit issue.

Traceback (most recent call last):
  File "sync.py", line 188, in <module>
    uploadedTracks = getAllUploadedTracks()
  File "sync.py", line 42, in getAllUploadedTracks
    tracks = ytmusic.get_library_upload_songs(limit=100000)
  File "C:\Users\k\AppData\Roaming\Python\Python38\site-packages\ytmusicapi\mixins\uploads.py", line 45, in get_library_upload_songs
    get_continuations(results, 'musicShelfContinuation', limit - len(songs),
  File "C:\Users\k\AppData\Roaming\Python\Python38\site-packages\ytmusicapi\parsers\utils.py", line 58, in get_continuations
    results = response['continuationContents'][continuation_type]
KeyError: 'continuationContents'

The response object looks like this:

{'responseContext': {'visitorData': 'CgtvVTcxa1EtbV9hayinher6BQ%3D%3D', 'serviceTrackingParams': [{'service': 'GFEEDBACK', 'params': [{'key': 'browse_id', 'value': 'FEmusic_library_privately_owned_tracks'}, {'key': 'logged_in',
'value': '1'}, {'key': 'country-type', 'value': 'B'}, {'key': 'e', 'value': '23890891,23845309,23939002,23790586,23905281,23935843,23914750,23820529,23934570,23914966,23933563,23937655,23858564,23881483,1714250,23897872,23908862,23916052,23933139,23939671,23856479,23880073,23942832,23940173,23907445,23932183,24650115,23859025,23935319,23940180,23930220,23937102,23853114,23911055,23945473,23944833,23925265,23886777,23942424,23915909,23930724,23915032,23755966,23935261,23794339,23782085,23842233,23872595,23910775,23928508,23941096,23915932,23841299,23936409,23918657,23932763,23890990,23797973,23923862,23834232,23864523,23895138,23897295,23942822,23911301,23862294,23865206,23748146,23816681,23862582,23894009,23928507,23874337,23932706,23939572,23938895,23842638,23941167,23902702,23927097,23928679,23927284,23836115,23904600,23939585,23862346,23940773,23904038,23944253,9473404,23862065,23931923,23882125,23744176,23925932,23885639,23832002,23927190,23884697,23917917,23896570,23938773,23928204,23811985,9473375,23942505,39320820,23941594,23896704,23925759,23938133,23945391,23927089,23874051,23854276,23932754,23919306,23919025,23942831,23837886,23745613,9473387,23839597,23943708,23946074,23884315,23895618,23897257,23915985,23897503,23860381,23910764,23943830,23937609,23871729,23932187,23944520,23944779,23934970,23900854,23886227,23927102,23935545,23921645,23804281,23936487,23884386,23921357,23934344,23929958,23890275,23862025,23940625,23929653,23938619,23897263,23874254,23940704,23943286,23911225,23917913,23878504,23868136,23896666,23939360,23709532,23900376,23902008,23857948,23874981,23754764,23921982,23943379,23880323,23882502,23919917,23898321,23841635,23901082,23907436,23891345,23912894,23930481,23923390,23884995,23932753,23857970,23944069,23944810,23918597,23919575,23884593,23939237,23942379,23920909,23854352,23861666,23910680,23939014,23935723,23940502,23944543,23937161,23789385,23865230,23939021'}]}, {'service': 'CSI', 'params': [{'key': 'c', 'value': 'WEB_REMIX'}, {'key': 'cver', 'value': '0.1'}, {'key': 'yt_li', 'value': '1'}, {'key': 'GetBrowsePrivatelyOwnedTracksPage_rid', 'value': '0xd596bb316bd6100c'}]}, {'service': 'ECATCHER', 'params': [{'key': 'client.version', 'value': '0.1'}, {'key': 'client.name', 'value': 'WEB_REMIX'}]}]}, 'trackingParams': 'CAAQhGciEwiKnvihrd_rAhWY69UKHauWCxk='}
<!-- gh-comment-id:690681662 --> @kawaiiDango commented on GitHub (Sep 10, 2020): I got a key error too and get_library_upload_songs(limit=100000) sometimes fails. Not sure if this is a rate limit issue. ``` Traceback (most recent call last): File "sync.py", line 188, in <module> uploadedTracks = getAllUploadedTracks() File "sync.py", line 42, in getAllUploadedTracks tracks = ytmusic.get_library_upload_songs(limit=100000) File "C:\Users\k\AppData\Roaming\Python\Python38\site-packages\ytmusicapi\mixins\uploads.py", line 45, in get_library_upload_songs get_continuations(results, 'musicShelfContinuation', limit - len(songs), File "C:\Users\k\AppData\Roaming\Python\Python38\site-packages\ytmusicapi\parsers\utils.py", line 58, in get_continuations results = response['continuationContents'][continuation_type] KeyError: 'continuationContents' ``` The `response` object looks like this: ``` {'responseContext': {'visitorData': 'CgtvVTcxa1EtbV9hayinher6BQ%3D%3D', 'serviceTrackingParams': [{'service': 'GFEEDBACK', 'params': [{'key': 'browse_id', 'value': 'FEmusic_library_privately_owned_tracks'}, {'key': 'logged_in', 'value': '1'}, {'key': 'country-type', 'value': 'B'}, {'key': 'e', 'value': '23890891,23845309,23939002,23790586,23905281,23935843,23914750,23820529,23934570,23914966,23933563,23937655,23858564,23881483,1714250,23897872,23908862,23916052,23933139,23939671,23856479,23880073,23942832,23940173,23907445,23932183,24650115,23859025,23935319,23940180,23930220,23937102,23853114,23911055,23945473,23944833,23925265,23886777,23942424,23915909,23930724,23915032,23755966,23935261,23794339,23782085,23842233,23872595,23910775,23928508,23941096,23915932,23841299,23936409,23918657,23932763,23890990,23797973,23923862,23834232,23864523,23895138,23897295,23942822,23911301,23862294,23865206,23748146,23816681,23862582,23894009,23928507,23874337,23932706,23939572,23938895,23842638,23941167,23902702,23927097,23928679,23927284,23836115,23904600,23939585,23862346,23940773,23904038,23944253,9473404,23862065,23931923,23882125,23744176,23925932,23885639,23832002,23927190,23884697,23917917,23896570,23938773,23928204,23811985,9473375,23942505,39320820,23941594,23896704,23925759,23938133,23945391,23927089,23874051,23854276,23932754,23919306,23919025,23942831,23837886,23745613,9473387,23839597,23943708,23946074,23884315,23895618,23897257,23915985,23897503,23860381,23910764,23943830,23937609,23871729,23932187,23944520,23944779,23934970,23900854,23886227,23927102,23935545,23921645,23804281,23936487,23884386,23921357,23934344,23929958,23890275,23862025,23940625,23929653,23938619,23897263,23874254,23940704,23943286,23911225,23917913,23878504,23868136,23896666,23939360,23709532,23900376,23902008,23857948,23874981,23754764,23921982,23943379,23880323,23882502,23919917,23898321,23841635,23901082,23907436,23891345,23912894,23930481,23923390,23884995,23932753,23857970,23944069,23944810,23918597,23919575,23884593,23939237,23942379,23920909,23854352,23861666,23910680,23939014,23935723,23940502,23944543,23937161,23789385,23865230,23939021'}]}, {'service': 'CSI', 'params': [{'key': 'c', 'value': 'WEB_REMIX'}, {'key': 'cver', 'value': '0.1'}, {'key': 'yt_li', 'value': '1'}, {'key': 'GetBrowsePrivatelyOwnedTracksPage_rid', 'value': '0xd596bb316bd6100c'}]}, {'service': 'ECATCHER', 'params': [{'key': 'client.version', 'value': '0.1'}, {'key': 'client.name', 'value': 'WEB_REMIX'}]}]}, 'trackingParams': 'CAAQhGciEwiKnvihrd_rAhWY69UKHauWCxk='} ```
Author
Owner

@tadly commented on GitHub (Sep 11, 2020):

In my case it might actually be because of the transfer from google play music to youtube music.

While doing so, they migrated non existing entries which are now stuck in the yt database which I am unable to remove.
I've been in contact with support for the past 3-4 weeks and they haven't been able to solve the issue yet 👏

Whether or not this is the actual root-cause I don't know but it would make sense.

My OP still holds true though.

<!-- gh-comment-id:690938752 --> @tadly commented on GitHub (Sep 11, 2020): In my case it might actually be because of the transfer from google play music to youtube music. While doing so, they migrated **non existing entries** which are now stuck in the yt database which I am unable to remove. I've been in contact with support for the past 3-4 weeks and they haven't been able to solve the issue yet 👏 Whether or not this is the actual root-cause I don't know but it would make sense. My OP still holds true though.
Author
Owner

@sigma67 commented on GitHub (Sep 11, 2020):

The origin of this issue is the unreliability of the API itself. Sometimes the continuation contents will be missing randomly, which is an error on YouTube's end. The hard part is finding all the situations where the API can fail.

In my opinion, the way this issue is worded it's kind of a catchall-complaint that there can be random server-side errors with YT's API. It would be more helpful to point out specific error cases for specific API calls, so we can implement the corresponding error handling as needed.

The issue you mention with empty libraries was recently fixed for uploads (#74) and should be simple to fix for the calls you mentioned as well. I'll rename this issue accordingly and suggest we close it when that issue is fixed. If you find other cases, feel free to open a separate issue for those.

@kawaiiDango I believe this issue was fixed in ca8a73046b and should be part of the next release.

<!-- gh-comment-id:690961175 --> @sigma67 commented on GitHub (Sep 11, 2020): The origin of this issue is the unreliability of the API itself. Sometimes the continuation contents will be missing randomly, which is an error on YouTube's end. The hard part is finding all the situations where the API can fail. In my opinion, the way this issue is worded it's kind of a catchall-complaint that there can be random server-side errors with YT's API. It would be more helpful to point out specific error cases for specific API calls, so we can implement the corresponding error handling as needed. The issue you mention with empty libraries was recently fixed for uploads (#74) and should be simple to fix for the calls you mentioned as well. I'll rename this issue accordingly and suggest we close it when that issue is fixed. If you find other cases, feel free to open a separate issue for those. @kawaiiDango I believe this issue was fixed in ca8a73046bb43441d8e40a84569e0383bcb305f6 and should be part of the next release.
Author
Owner

@sigma67 commented on GitHub (Sep 16, 2020):

So I've fixed get_library_songs, unfortunately I couldn't reproduce the issue with get_library_albums.

If you have no albums in the library, the function should end here:
github.com/sigma67/ytmusicapi@09daac66fc/ytmusicapi/mixins/library.py (L89-L90)

So there's no way you'd get an error at l. 92. Perhaps there was a different issue?

<!-- gh-comment-id:693632165 --> @sigma67 commented on GitHub (Sep 16, 2020): So I've fixed get_library_songs, unfortunately I couldn't reproduce the issue with get_library_albums. If you have no albums in the library, the function should end here: https://github.com/sigma67/ytmusicapi/blob/09daac66fc1a6e6a55854a21c92b8b7e861a793f/ytmusicapi/mixins/library.py#L89-L90 So there's no way you'd get an error at l. 92. Perhaps there was a different issue?
Author
Owner

@tadly commented on GitHub (Sep 17, 2020):

As mentioned in in my previous comment, there's a bug within youtube music which prevents me from removing certain, dead entries.
This might be the cause for it.

Google support hasn't been helpful so far

<!-- gh-comment-id:694254500 --> @tadly commented on GitHub (Sep 17, 2020): As mentioned in [in my previous comment](https://github.com/sigma67/ytmusicapi/issues/69#issuecomment-690938752), there's a bug within youtube music which prevents me from removing certain, dead entries. This **might** be the cause for it. Google support hasn't been helpful so far
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/ytmusicapi#51
No description provided.