[GH-ISSUE #494] KeyError: 'searchSuggestionRenderer' when using get_search_suggestions with 'a' as query #366

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

Originally created by @Johna-123 on GitHub (Dec 27, 2023).
Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/494

Describe the bug
I get an error when using the get_search_suggestions function with these parameters:
query="a"
detailed_runs=True

Here's the error from Flask:

[2023-12-27 14:05:16,222] ERROR in app: Exception on /api/get_search_suggestions [GET]
Traceback (most recent call last):
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1455, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 869, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask_cors\extension.py", line 176, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
                                                ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 867, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 852, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\Documents\react\ytmusic\python\app.py", line 24, in get_search_suggestions
    return ytmusic.get_search_suggestions(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\ytmusicapi\mixins\search.py", line 307, in get_search_suggestions
    search_suggestions = parse_search_suggestions(response, detailed_runs)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\ytmusicapi\parsers\search.py", line 224, in parse_search_suggestions
    suggestion_content = raw_suggestion['searchSuggestionRenderer']
                         ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'searchSuggestionRenderer'
127.0.0.1 - - [27/Dec/2023 14:05:18] "GET /api/get_search_suggestions?query=a&detailed_runs=false HTTP/1.1" 500 -

And this is my code:

from flask import Flask, request
from flask_cors import CORS
from ytmusicapi import YTMusic

app = Flask(__name__)
CORS(app)
ytmusic = YTMusic("oauth.json")

@app.route("/api/get_search_suggestions", methods=["GET"])
def get_search_suggestions():
	query = request.args["query"]
	detailed_runs = True if request.args["detailed_runs"] == "true" else False
	return ytmusic.get_search_suggestions(
		query=query,
		detailed_runs=detailed_runs
	)

I sent a GET request from Firefox to the flask server: http://127.0.0.1:5000/api/get_search_suggestions?query=a&detailed_runs=true

To Reproduce
Steps to reproduce the behavior:

  1. Use the get_search_suggestions() function with the following parameters:
    • query="a"
    • detailed_runs=True

Additional context
At first I thought single characters may not be supported for the query, but when setting the query as c, I get a response:

[
  {
    "runs": [
      {
        "bold": true,
        "text": "c"
      },
      {
        "text": "alum scott"
      }
    ],
    "text": "calum scott"
  },
  {
    "runs": [
      {
        "bold": true,
        "text": "c"
      },
      {
        "text": "cm"
      }
    ],
    "text": "ccm"
  },
  {
    "runs": [
      {
        "bold": true,
        "text": "c"
      },
      {
        "text": "itizen tv live"
      }
    ],
    "text": "citizen tv live"
  },
  {
    "runs": [
      {
        "bold": true,
        "text": "c"
      },
      {
        "text": "hristmas music"
      }
    ],
    "text": "christmas music"
  }
]

The same error also occurs for some other single characters as the query.

The error occurs when detailed_runs is set to both True and False.

Originally created by @Johna-123 on GitHub (Dec 27, 2023). Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/494 **Describe the bug** I get an error when using the `get_search_suggestions` function with these parameters: `query="a"` `detailed_runs=True` Here's the error from Flask: ``` [2023-12-27 14:05:16,222] ERROR in app: Exception on /api/get_search_suggestions [GET] Traceback (most recent call last): File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1455, in wsgi_app response = self.full_dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 869, in full_dispatch_request rv = self.handle_user_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask_cors\extension.py", line 176, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) ^^^^^^^^^^^^^^^^^^ File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 867, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 852, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Owner\Documents\react\ytmusic\python\app.py", line 24, in get_search_suggestions return ytmusic.get_search_suggestions( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\ytmusicapi\mixins\search.py", line 307, in get_search_suggestions search_suggestions = parse_search_suggestions(response, detailed_runs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\ytmusicapi\parsers\search.py", line 224, in parse_search_suggestions suggestion_content = raw_suggestion['searchSuggestionRenderer'] ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^ KeyError: 'searchSuggestionRenderer' 127.0.0.1 - - [27/Dec/2023 14:05:18] "GET /api/get_search_suggestions?query=a&detailed_runs=false HTTP/1.1" 500 - ``` And this is my code: ```python from flask import Flask, request from flask_cors import CORS from ytmusicapi import YTMusic app = Flask(__name__) CORS(app) ytmusic = YTMusic("oauth.json") @app.route("/api/get_search_suggestions", methods=["GET"]) def get_search_suggestions(): query = request.args["query"] detailed_runs = True if request.args["detailed_runs"] == "true" else False return ytmusic.get_search_suggestions( query=query, detailed_runs=detailed_runs ) ``` I sent a GET request from Firefox to the flask server: `http://127.0.0.1:5000/api/get_search_suggestions?query=a&detailed_runs=true` **To Reproduce** Steps to reproduce the behavior: 1. Use the `get_search_suggestions()` function with the following parameters: - `query="a"` - `detailed_runs=True` **Additional context** At first I thought single characters may not be supported for the query, but when setting the query as `c`, I get a response: ```json [ { "runs": [ { "bold": true, "text": "c" }, { "text": "alum scott" } ], "text": "calum scott" }, { "runs": [ { "bold": true, "text": "c" }, { "text": "cm" } ], "text": "ccm" }, { "runs": [ { "bold": true, "text": "c" }, { "text": "itizen tv live" } ], "text": "citizen tv live" }, { "runs": [ { "bold": true, "text": "c" }, { "text": "hristmas music" } ], "text": "christmas music" } ] ``` The same error also occurs for some other single characters as the query. The error occurs when `detailed_runs` is set to both `True` and `False`.
kerem closed this issue 2026-02-27 23:00:24 +03:00
Author
Owner

@sigma67 commented on GitHub (Dec 27, 2023):

Cannot reproduce. Works fine for me. What does your preview look like?

<!-- gh-comment-id:1870635979 --> @sigma67 commented on GitHub (Dec 27, 2023): Cannot reproduce. Works fine for me. What does your preview look like?
Author
Owner

@Johna-123 commented on GitHub (Dec 27, 2023):

You mean what I get in the browser? Error 500.

<!-- gh-comment-id:1870639349 --> @Johna-123 commented on GitHub (Dec 27, 2023): You mean what I get in the browser? Error 500.
Author
Owner

@sigma67 commented on GitHub (Dec 27, 2023):

So you can't even type this query in your YouTube Music search bar? Seems like something's wrong on your end

<!-- gh-comment-id:1870647287 --> @sigma67 commented on GitHub (Dec 27, 2023): So you can't even type this query in your YouTube Music search bar? Seems like something's wrong on your end
Author
Owner

@Johna-123 commented on GitHub (Dec 27, 2023):

No, I can type it in YouTube Music and it's works well.

<!-- gh-comment-id:1870650036 --> @Johna-123 commented on GitHub (Dec 27, 2023): No, I can type it in YouTube Music and it's works well.
Author
Owner

@Johna-123 commented on GitHub (Dec 27, 2023):

The problem seems to occur only when authenticated.

<!-- gh-comment-id:1870666130 --> @Johna-123 commented on GitHub (Dec 27, 2023): The problem seems to occur only when authenticated.
Author
Owner

@jcbirdwell commented on GitHub (Dec 29, 2023):

search suggestions is returning historySuggestionRenderer objects mixed in with the searchSuggestionRenderers when authenticated, based on past searches. replicate in test.py with

def test_auth_search_suggestions(self):
    # add search term to history
    first_pass = self.yt_oauth.search('b')
    self.assertGreater(len(first_pass), 0)
    # get results
    results = self.yt_oauth.get_search_suggestions('b', detailed_runs=True)
    self.assertGreater(len(results), 0)

Adding items to search history seems to be a bit finicky / sometimes takes a second to start getting returned by suggestions., so it may take an extra run or adding a sleep timeout to get the error.

<!-- gh-comment-id:1871665627 --> @jcbirdwell commented on GitHub (Dec 29, 2023): search suggestions is returning historySuggestionRenderer objects mixed in with the searchSuggestionRenderers when authenticated, based on past searches. replicate in test.py with ```python def test_auth_search_suggestions(self): # add search term to history first_pass = self.yt_oauth.search('b') self.assertGreater(len(first_pass), 0) # get results results = self.yt_oauth.get_search_suggestions('b', detailed_runs=True) self.assertGreater(len(results), 0) ``` Adding items to search history seems to be a bit finicky / sometimes takes a second to start getting returned by suggestions., so it may take an extra run or adding a sleep timeout to get the error.
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#366
No description provided.