Spotify API NoneType error when creating DataFrame from audio features

I’m working with the Spotify API to extract audio characteristics for songs and I keep running into an AttributeError. Here’s my current code:

audio_data = {}
track_list = list(music_df['track_id'].unique())
index = 0
batch_size = 50

while index < len(track_list):
    current_batch = track_list[index:index+batch_size]
    batch_features = spotify_client.audio_features(current_batch)
    audio_data.update({song_id: song_data 
                      for song_id, song_data in zip(current_batch, batch_features)})
    index += batch_size

# Check one item
print(audio_data['7xyz9mN8pQr2Kj3LmV4Ht'])

# Try to convert to DataFrame
result_df = pd.DataFrame.from_dict(audio_data, orient='index')

The error I get is AttributeError: 'NoneType' object has no attribute 'items'. The issue seems to be that some values in my dictionary are None instead of the expected audio feature objects. Right now my data looks like a nested dictionary structure, but I want to flatten it so each audio feature becomes its own column. How can I handle the None values and properly convert this to a DataFrame where columns like energy and loudness are separate?

had this exact issue last week! spotify’s api returns None for tracks without audio features. just filter out the None values before creating your dataframe: audio_data = {k: v for k, v in audio_data.items() if v is not None} then use from_dict like normal.

This happens because Spotify’s API sometimes returns None for tracks that are unavailable or restricted. Don’t just filter out the None values like other answers suggest - you’ll lose data integrity. Instead, replace None entries with empty dictionaries first, then use pd.json_normalize() since it handles nested data better than from_dict. Try audio_data = {k: v if v else {} for k, v in audio_data.items()} then result_df = pd.json_normalize(list(audio_data.values())). You’ll keep all your original tracks and automatically flatten the nested audio features into separate columns.

Been there with music APIs. Manual filtering works but it’s a pain when you’re juggling multiple data sources or the API keeps changing.

I automated this whole thing with Latenode. Built a scenario that handles Spotify API calls, filters out None responses automatically, and spits out clean DataFrames. No error handling code needed.

What’s great is Latenode’s data transformation nodes flatten nested JSON automatically. No more None value headaches or DataFrame conversion errors. Just connect your Spotify API, add a filter node for empty responses, and run it through the DataFrame transformer.

I process thousands of tracks daily with this setup. It handles rate limits, retries failures, and dumps clean data wherever I want it. Way more reliable than Python scripts that break every time Spotify updates.

Check it out: https://latenode.com

The issue is that pd.DataFrame.from_dict() expects all dictionary values to have the same structure, but None values mess this up. I’ve hit this same problem with music API data before. Skip the preprocessing and handle it during DataFrame creation instead. Wrap your conversion in a try-except block and use pd.DataFrame([v for v in audio_data.values() if v is not None]) - this skips the problematic entries automatically. You’ll avoid the AttributeError since you’re feeding pandas a list of valid dictionaries instead of letting it choke on None values.