[GH-ISSUE #277] sp.audio_features() not working correctly. Spotify updated endpoints? #151

Closed
opened 2026-02-27 23:21:06 +03:00 by kerem · 8 comments
Owner

Originally created by @aalshukri on GitHub (Apr 22, 2018).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/277

Hi,

I have discovered that the function sp.audio_features() is not working correctly. This function currently returns nulls.

I think the problem relates to the endpoint in spotipy.

Minimal code example which returns nulls.
features = sp.audio_features(tid) print(json.dumps(features, indent=4))

I have also tried the the example code:
features = sp.audio_features(tids) delta = time.time() - start for feature in features: print(json.dumps(feature, indent=4)) print() analysis = sp._get(feature['analysis_url']) print(json.dumps(analysis, indent=4)) print() print ("features retrieved in %.2f seconds" % (delta,))
Reference: https://github.com/plamere/spotipy/blob/master/examples/audio_features.py

The above example failed because "sp._get(feature['analysis_url'])" does not return any JSON results (it returned nulls as mentioned) which causes an exception.

What I expected from this function was a list of features as shown below:
{ "danceability": 0.696, "energy": 0.905, "key": 2, "loudness": -2.743, "mode": 1, "speechiness": 0.103, "acousticness": 0.011, "instrumentalness": 0.000905, "liveness": 0.302, "valence": 0.625, "tempo": 114.944, "type": "audio_features", "id": "11dFghVXANMlKmJXsNCbNl", "uri": "spotify:track:11dFghVXANMlKmJXsNCbNl", "track_href": "https://api.spotify.com/v1/tracks/11dFghVXANMlKmJXsNCbNl", "analysis_url": "https://api.spotify.com/v1/audio-analysis/11dFghVXANMlKmJXsNCbNl", "duration_ms": 207960, "time_signature": 4 }
Reference: https://beta.developer.spotify.com/console/get-audio-features-track/

The functionality can be replicated using the spotify web console
GET https://api.spotify.com/v1/audio-features/{id}

Thanks for your help
Ash

Originally created by @aalshukri on GitHub (Apr 22, 2018). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/277 Hi, I have discovered that the function sp.audio_features() is not working correctly. This function currently returns nulls. I think the problem relates to the endpoint in spotipy. Minimal code example which returns nulls. ` features = sp.audio_features(tid) print(json.dumps(features, indent=4)) ` I have also tried the the example code: ` features = sp.audio_features(tids) delta = time.time() - start for feature in features: print(json.dumps(feature, indent=4)) print() analysis = sp._get(feature['analysis_url']) print(json.dumps(analysis, indent=4)) print() print ("features retrieved in %.2f seconds" % (delta,)) ` Reference: https://github.com/plamere/spotipy/blob/master/examples/audio_features.py The above example failed because "sp._get(feature['analysis_url'])" does not return any JSON results (it returned nulls as mentioned) which causes an exception. What I expected from this function was a list of features as shown below: ` { "danceability": 0.696, "energy": 0.905, "key": 2, "loudness": -2.743, "mode": 1, "speechiness": 0.103, "acousticness": 0.011, "instrumentalness": 0.000905, "liveness": 0.302, "valence": 0.625, "tempo": 114.944, "type": "audio_features", "id": "11dFghVXANMlKmJXsNCbNl", "uri": "spotify:track:11dFghVXANMlKmJXsNCbNl", "track_href": "https://api.spotify.com/v1/tracks/11dFghVXANMlKmJXsNCbNl", "analysis_url": "https://api.spotify.com/v1/audio-analysis/11dFghVXANMlKmJXsNCbNl", "duration_ms": 207960, "time_signature": 4 } ` Reference: https://beta.developer.spotify.com/console/get-audio-features-track/ The functionality can be replicated using the spotify web console ` GET https://api.spotify.com/v1/audio-features/{id} ` Thanks for your help Ash
kerem 2026-02-27 23:21:06 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@shatteringlass commented on GitHub (Aug 5, 2018):

I can confirm this. Possibly I'll submit a PR.

EDIT: strangely enough, the current implementation works for string arguments but not lists. I was able to fetch audio features for a comma separated string of track IDs. There seems to be nothing wrong in the else branch of the function code.

<!-- gh-comment-id:410535144 --> @shatteringlass commented on GitHub (Aug 5, 2018): I can confirm this. Possibly I'll submit a PR. EDIT: strangely enough, the current implementation works for string arguments but not lists. I was able to fetch audio features for a comma separated string of track IDs. There seems to be nothing wrong in the [else branch](https://github.com/plamere/spotipy/blob/4c2c1d763a3653aa225c4af848409ec31286a6bf/spotipy/client.py#L862) of the function code.
Author
Owner

@lyons7 commented on GitHub (Sep 19, 2018):

Did this ever get fixed? I couldn't even get the comma separated string of track IDs work-around to work!

<!-- gh-comment-id:422900252 --> @lyons7 commented on GitHub (Sep 19, 2018): Did this ever get fixed? I couldn't even get the comma separated string of track IDs work-around to work!
Author
Owner

@eribradl commented on GitHub (Oct 3, 2018):

I just ran into this same issue today, and it looks to me like audio_features('something') is expecting 'something' to be a list. Similarly, the response you get back is also a list.
@aalshukri To make your first example work, try:
features = sp.audio_features(tid)[0]

<!-- gh-comment-id:426831212 --> @eribradl commented on GitHub (Oct 3, 2018): I just ran into this same issue today, and it looks to me like audio_features('something') is expecting 'something' to be a list. Similarly, the response you get back is also a list. @aalshukri To make your first example work, try: features = sp.audio_features(tid)[0]
Author
Owner

@Brakjen commented on GitHub (May 7, 2019):

I am not getting this to work either. It returns a list of None. However, I can get the features if I set up my own get request using requests, and json to parse the output. The function below could be made more sophisticated to accept lists of track ids, and not a single id. Just pass your token and track id as kwargs.

import requests
from bs4 import BeautifulSoup as bs
import json

def get_track_features(token=None, track_id=None): 
    url = "https://api.spotify.com/v1/audio-features/"+track_id
    with requests.session() as sesh:
        header = {"Authorization": "Bearer {}".format(token)}
        response = sesh.get(url, headers=header)
        feature = str(bs(response.content, "html.parser"))
        return json.loads(feature)
<!-- gh-comment-id:490270851 --> @Brakjen commented on GitHub (May 7, 2019): I am not getting this to work either. It returns a list of None. However, I can get the features if I set up my own get request using requests, and json to parse the output. The function below could be made more sophisticated to accept lists of track ids, and not a single id. Just pass your token and track id as kwargs. ``` import requests from bs4 import BeautifulSoup as bs import json def get_track_features(token=None, track_id=None): url = "https://api.spotify.com/v1/audio-features/"+track_id with requests.session() as sesh: header = {"Authorization": "Bearer {}".format(token)} response = sesh.get(url, headers=header) feature = str(bs(response.content, "html.parser")) return json.loads(feature) ```
Author
Owner

@paarmann commented on GitHub (Feb 6, 2020):

Works as expected in spotipy=2.7.1

query_ids = ['id1', 'id2',...]
response = sp.audio_features(query_ids)

Returns:

[{'danceability': 0.817, 'energy': 0.781, 'key': 7, 'loudness': -4.178, 'mode': 0, 'speechiness': 0.112, 'acousticness': 0.00838, 'instrumentalness': 5.21e-06, 'liveness': 0.123, 'valence': 0.359, 'tempo': 104.0, 'type': 'audio_features', 'id': '07s9NNOT0sZQp7TyolLLgu', 'uri': 'spotify:track:07s9NNOT0sZQp7TyolLLgu', 'track_href': 'https://api.spotify.com/v1/tracks/07s9NNOT0sZQp7TyolLLgu', 'analysis_url': 'https://api.spotify.com/v1/audio-analysis/07s9NNOT0sZQp7TyolLLgu', 'duration_ms': 216707, 'time_signature': 4}, {'danceability': 0.659, ...

The result list may contain Null values if there are no audio features available for the id at this position.

<!-- gh-comment-id:583132613 --> @paarmann commented on GitHub (Feb 6, 2020): Works as expected in spotipy=2.7.1 ``` query_ids = ['id1', 'id2',...] response = sp.audio_features(query_ids) ``` Returns: ``` [{'danceability': 0.817, 'energy': 0.781, 'key': 7, 'loudness': -4.178, 'mode': 0, 'speechiness': 0.112, 'acousticness': 0.00838, 'instrumentalness': 5.21e-06, 'liveness': 0.123, 'valence': 0.359, 'tempo': 104.0, 'type': 'audio_features', 'id': '07s9NNOT0sZQp7TyolLLgu', 'uri': 'spotify:track:07s9NNOT0sZQp7TyolLLgu', 'track_href': 'https://api.spotify.com/v1/tracks/07s9NNOT0sZQp7TyolLLgu', 'analysis_url': 'https://api.spotify.com/v1/audio-analysis/07s9NNOT0sZQp7TyolLLgu', 'duration_ms': 216707, 'time_signature': 4}, {'danceability': 0.659, ... ``` The result list may contain Null values if there are no audio features available for the id at this position.
Author
Owner

@stephanebruckert commented on GitHub (Feb 9, 2020):

Thanks for the feedback @paarmann, gonna close this one

<!-- gh-comment-id:583839422 --> @stephanebruckert commented on GitHub (Feb 9, 2020): Thanks for the feedback @paarmann, gonna close this one
Author
Owner

@anushkacodez commented on GitHub (Feb 13, 2025):

Works as expected in spotipy=2.7.1

query_ids = ['id1', 'id2',...]
response = sp.audio_features(query_ids)

Returns:

[{'danceability': 0.817, 'energy': 0.781, 'key': 7, 'loudness': -4.178, 'mode': 0, 'speechiness': 0.112, 'acousticness': 0.00838, 'instrumentalness': 5.21e-06, 'liveness': 0.123, 'valence': 0.359, 'tempo': 104.0, 'type': 'audio_features', 'id': '07s9NNOT0sZQp7TyolLLgu', 'uri': 'spotify:track:07s9NNOT0sZQp7TyolLLgu', 'track_href': 'https://api.spotify.com/v1/tracks/07s9NNOT0sZQp7TyolLLgu', 'analysis_url': 'https://api.spotify.com/v1/audio-analysis/07s9NNOT0sZQp7TyolLLgu', 'duration_ms': 216707, 'time_signature': 4}, {'danceability': 0.659, ...

The result list may contain Null values if there are no audio features available for the id at this position.

Hi this doesn't work anymore. Is there any other way to get audio features in python?

<!-- gh-comment-id:2657060542 --> @anushkacodez commented on GitHub (Feb 13, 2025): > Works as expected in spotipy=2.7.1 > > ``` > query_ids = ['id1', 'id2',...] > response = sp.audio_features(query_ids) > ``` > > Returns: > > ``` > [{'danceability': 0.817, 'energy': 0.781, 'key': 7, 'loudness': -4.178, 'mode': 0, 'speechiness': 0.112, 'acousticness': 0.00838, 'instrumentalness': 5.21e-06, 'liveness': 0.123, 'valence': 0.359, 'tempo': 104.0, 'type': 'audio_features', 'id': '07s9NNOT0sZQp7TyolLLgu', 'uri': 'spotify:track:07s9NNOT0sZQp7TyolLLgu', 'track_href': 'https://api.spotify.com/v1/tracks/07s9NNOT0sZQp7TyolLLgu', 'analysis_url': 'https://api.spotify.com/v1/audio-analysis/07s9NNOT0sZQp7TyolLLgu', 'duration_ms': 216707, 'time_signature': 4}, {'danceability': 0.659, ... > ``` > > The result list may contain Null values if there are no audio features available for the id at this position. Hi this doesn't work anymore. Is there any other way to get audio features in python?
Author
Owner

@dieser-niko commented on GitHub (Feb 13, 2025):

No, you're somewhat late to the party. Please check #1172

<!-- gh-comment-id:2657348422 --> @dieser-niko commented on GitHub (Feb 13, 2025): No, you're somewhat late to the party. Please check #1172
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/spotipy#151
No description provided.