Skip to content

Raise a clear error for values that cannot be encoded as an image - #8450

Open
LeSingh1 wants to merge 1 commit into
huggingface:mainfrom
LeSingh1:fix-image-encode-example-type-error
Open

Raise a clear error for values that cannot be encoded as an image#8450
LeSingh1 wants to merge 1 commit into
huggingface:mainfrom
LeSingh1:fix-image-encode-example-type-error

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown

Putting an unsupported value in an Image() column fails with an error from deep inside the encoder rather than a message about the image:

from datasets import Dataset, Features, Image

Dataset.from_dict({"im": [123]}, features=Features({"im": Image()}))
AttributeError: 'int' object has no attribute 'get'

Same for a float, a bool, a set, or any other unsupported object. A 0-dimensional numpy array fails differently but just as opaquely:

Image().encode_example(np.array(5))
IndexError: tuple index out of range

Cause

Image.encode_example handles str, Path, bytes, np.ndarray and PIL.Image.Image, and then falls straight through to the mapping branches:

elif value.get("path") is not None and os.path.isfile(value["path"]):

so any value that is not one of the supported types and not a mapping raises AttributeError on .get.

The Video feature already guards exactly this case:

# src/datasets/features/video.py
else:
    raise TypeError(f"Unsupported encode_example type: {type(value)}")

Image simply never got the same guard.

Fix

  • reject values that are neither a supported type nor a mapping with the same TypeError: Unsupported encode_example type: ... message Video already uses, so the two features report this identically;
  • reject 0-dimensional arrays in encode_np_array with an explicit ValueError instead of letting the shape indexing blow up.

Mapping is used rather than dict so that any mapping type keeps working. Mappings that are missing both path and bytes still raise the existing, already explicit ValueError, and every currently supported input is unchanged — str, Path, bytes, dicts, PIL images and 1D/2D/3D arrays all still encode exactly as before.

Tests

  • test_image_feature_encode_example_unsupported_type covers int, float, bool, set and a plain object;
  • test_image_feature_encode_example_zero_dimensional_array covers the 0-d array through both Image.encode_example and encode_np_array;
  • test_image_feature_encode_example_missing_keys pins the existing ValueError for a mapping without usable keys, so this change cannot swallow it.

The first two fail on main (6 parametrizations); the third passes before and after and is there as a regression guard.

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

Putting an unsupported value in an Image column fails with an error from deep
inside the encoder rather than a message about the image:

    Dataset.from_dict({"im": [123]}, features=Features({"im": Image()}))
    AttributeError: 'int' object has no attribute 'get'

Image.encode_example handles str, Path, bytes, ndarray and PIL images, then
assumes anything else is a mapping and calls value.get("path") on it. The Video
feature already guards this exact case with
'TypeError: Unsupported encode_example type: ...', so reuse that message.

A 0-dimensional array holds no image either and raised
'IndexError: tuple index out of range' from encode_np_array, so reject it with
an explicit message too.

Mappings that are missing 'path' and 'bytes' keep their existing ValueError.
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