[GH-ISSUE #642] Implement clear errors or sanitize requests with > or < in the playlist title. #433

Closed
opened 2026-02-27 23:00:46 +03:00 by kerem · 4 comments
Owner

Originally created by @SuppliedOrange on GitHub (Aug 19, 2024).
Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/642

This is related to a problem.
Using > and < in playlists will cause it to error with 400 Bad Request.
Originally found in https://github.com/linsomniac/spotify_to_ytmusic/issues/93
I could not find other characters affected by this. The error it returns does not make perfect sense either.

To reproduce:

.create_playlist(title="this > won't work", description="")
# Server returned HTTP 400: Bad Request.

This is what is returned:

{
  "error": {
    "code": 400,
    "message": "Sorry, something went wrong.",
    "errors": [
      {
        "message": "Sorry, something went wrong.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

Describe the solution you'd like

Might help to sanitize the request or return a more concise error. It's difficult to determine that it's related to this.

Originally created by @SuppliedOrange on GitHub (Aug 19, 2024). Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/642 ## Is your feature request related to a problem? Please describe. This is related to a problem. Using `>` and `<` in playlists will cause it to error with `400 Bad Request`. Originally found in https://github.com/linsomniac/spotify_to_ytmusic/issues/93 I could not find other characters affected by this. The error it returns does not make perfect sense either. To reproduce: ```py .create_playlist(title="this > won't work", description="") # Server returned HTTP 400: Bad Request. ``` This is what is returned: ```json { "error": { "code": 400, "message": "Sorry, something went wrong.", "errors": [ { "message": "Sorry, something went wrong.", "domain": "global", "reason": "badRequest" } ], "status": "INVALID_ARGUMENT" } } ``` ## Describe the solution you'd like Might help to sanitize the request or return a more concise error. It's difficult to determine that it's related to this.
kerem 2026-02-27 23:00:46 +03:00
Author
Owner

@sigma67 commented on GitHub (Aug 19, 2024):

Funny enough, this doesn't work on the Web either. Neither do they have a good error message for it. Not sure it's the task of this project to figure something out that YouTube's not doing

<!-- gh-comment-id:2297077140 --> @sigma67 commented on GitHub (Aug 19, 2024): Funny enough, this doesn't work on the Web either. Neither do they have a good error message for it. Not sure it's the task of this project to figure something out that YouTube's not doing
Author
Owner

@sigma67 commented on GitHub (Aug 19, 2024):

I'd prefer to raise in that case. Not sure if this is just a temporary issue.

PR welcome

<!-- gh-comment-id:2297078770 --> @sigma67 commented on GitHub (Aug 19, 2024): I'd prefer to raise in that case. Not sure if this is just a temporary issue. PR welcome
Author
Owner

@SuppliedOrange commented on GitHub (Aug 20, 2024):

Cool, it would help to return the response object within your YTMusicServerError and other exception classes. I'm guessing the INVALID_ARGUMENT status is probably related to this, so it'd be better to check it against that.

Otherwise, would this be an appropriate change to make?

def create_playlist():
        #...
        try:
            response = self._send_request(endpoint, body)

        except YTMusicServerError as error:
            if ("400" in error): # Checks if it's 400 [Bad Request]
                """
                Implemented to counter an undocumented error with no proper response
                Refers to issue https://github.com/sigma67/ytmusicapi/issues/642
                """
                invalid_characters = self._check_has_invalid_playlist_title_characters( title )
                if (invalid_characters): raise YTMusicServerError(error + f"Invalid characters in playlist title: {", ".join(invalid_characters)}")
            
            raise error

def _check_has_invalid_playlist_title_characters(self, title: str) -> list:
        """
        Checks if the playlist title has characters that youtube does not like.

        :param title: Playlist title
        :return: Boolean if playlist title has valid characters or not
        """
        invalid_characters = [">", "<"]
        found_invalid_characters = [char for char in title if char in invalid_characters]
        return found_invalid_characters

<!-- gh-comment-id:2299060940 --> @SuppliedOrange commented on GitHub (Aug 20, 2024): Cool, it would help to return the response object within your `YTMusicServerError` and other exception classes. I'm guessing the `INVALID_ARGUMENT` status is probably related to this, so it'd be better to check it against that. Otherwise, would this be an appropriate change to make? ```py def create_playlist(): #... try: response = self._send_request(endpoint, body) except YTMusicServerError as error: if ("400" in error): # Checks if it's 400 [Bad Request] """ Implemented to counter an undocumented error with no proper response Refers to issue https://github.com/sigma67/ytmusicapi/issues/642 """ invalid_characters = self._check_has_invalid_playlist_title_characters( title ) if (invalid_characters): raise YTMusicServerError(error + f"Invalid characters in playlist title: {", ".join(invalid_characters)}") raise error def _check_has_invalid_playlist_title_characters(self, title: str) -> list: """ Checks if the playlist title has characters that youtube does not like. :param title: Playlist title :return: Boolean if playlist title has valid characters or not """ invalid_characters = [">", "<"] found_invalid_characters = [char for char in title if char in invalid_characters] return found_invalid_characters ```
Author
Owner

@SuppliedOrange commented on GitHub (Aug 20, 2024):

Silly me, I closed the issue.

<!-- gh-comment-id:2299061243 --> @SuppliedOrange commented on GitHub (Aug 20, 2024): Silly me, I closed the issue.
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#433
No description provided.