Skip to content

Let a dataset with an Audio column be flattened - #8452

Open
LeSingh1 wants to merge 1 commit into
huggingface:mainfrom
LeSingh1:fix-audio-flatten-consistency
Open

Let a dataset with an Audio column be flattened#8452
LeSingh1 wants to merge 1 commit into
huggingface:mainfrom
LeSingh1:fix-audio-flatten-consistency

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown

Dataset.flatten() fails on any dataset that holds a decodable Audio column:

from datasets import Dataset, Features, Audio, Value

features = Features({"audio": Audio(), "text": Value("string")})
ds = Dataset.from_dict({"audio": [None], "text": ["a"]}, features=features)
ds.flatten()
ValueError: Cannot flatten a decoded Audio feature.

The same dataset with an Image, Video or Pdf column flattens without complaint:

feature Features.flatten() Dataset.flatten()
Image() {'audio': Image(...), 'text': ...} ['audio', 'text']
Video() leaves it alone ['audio', 'text']
Pdf() leaves it alone ['audio', 'text']
Audio() ValueError ValueError

So a single audio column makes the whole dataset impossible to flatten — even when the user only wants to expand an unrelated struct column, and even though flattening does not decode anything.

Cause

Image.flatten, Video.flatten and Pdf.flatten all share the same docstring and shape:

"If in the decodable state, return the feature itself, otherwise flatten the feature into a dictionary."

Audio.flatten is the odd one out:

def flatten(self):
    """If in the decodable state, raise an error, otherwise flatten the feature into a dictionary."""
    if self.decode:
        raise ValueError("Cannot flatten a decoded Audio feature.")

Features.flatten calls subfeature.flatten() on every feature that has the method, so the raise propagates out of Features.flatten() and Dataset.flatten().

Fix

Return the feature itself when it is decodable, exactly like the other three media features. Audio(decode=False) still flattens to {"bytes": Value("binary"), "path": Value("string")} as before.

No test asserted the old error — grep -rn "Cannot flatten" src/ tests/ matched only the line being removed — so nothing existing had to be adjusted.

Tests

tests/features/test_audio.py::test_audio_feature_flatten checks Audio().flatten(), Audio(decode=False).flatten(), Features.flatten() and Dataset.flatten(). It fails on main with the ValueError above, and needs no audio backend so it runs without torchcodec.

tests/features/, tests/test_formatting.py, tests/test_table.py, tests/test_arrow_dataset.py and tests/test_dataset_dict.py: 1134 passed, 0 failures.

Dataset.flatten() fails on any dataset that holds a decodable Audio column:

    ds = Dataset.from_dict({"audio": [None], "text": ["a"]},
                           features=Features({"audio": Audio(), "text": Value("string")}))
    ds.flatten()
    ValueError: Cannot flatten a decoded Audio feature.

The same dataset with an Image, Video or Pdf column flattens fine: those three
return the feature itself when it is decodable, so Features.flatten() leaves the
media column alone and only expands the other columns. Audio raised instead, so
a single audio column made the whole dataset impossible to flatten, even when
the user only wanted to expand an unrelated struct column.

Return the feature itself like the other media features do. Audio(decode=False)
still flattens to bytes/path exactly as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant