Coerce ClassLabel.names to plain str to fix invalid YAML in README.md - #8454
Open
RudrenduPaul wants to merge 1 commit into
Open
Coerce ClassLabel.names to plain str to fix invalid YAML in README.md#8454RudrenduPaul wants to merge 1 commit into
RudrenduPaul wants to merge 1 commit into
Conversation
numpy.str_ (and similar numpy-backed) entries slip into ClassLabel.names when built from numpy.unique(...) or a pandas column. PyYAML's default Dumper only matches representers by exact type, so these fall back to unsafe !!python/object/apply tags when the dataset card metadata is dumped, producing a README.md that fails to parse back with yaml.safe_load. Fixes huggingface#6919
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Coerce ClassLabel.names to plain str to fix invalid YAML in README.md
Fixes #6919
Problem
push_to_hubcan fail with:This happens whenever a
ClassLabel'snameslist contains elements thatare not exactly
str— most commonlynumpy.str_, which shows up whenevernamesis built fromnumpy.unique(...)or read out of a pandas column(a very common pattern for NER/token-classification labels, exactly as
described in the issue).
Root cause
ClassLabel.__post_init__already normalizes the internal_int2strmapping with
str(name), but it left the publicnameslist untouched.When the dataset card YAML metadata is generated
(
Features._to_yaml_list->yaml.dump), PyYAML's defaultDumperselects a representer by exact type match, not
isinstance. Sincenumpy.str_is a subclass ofstrbut notstritself, it fallsthrough to the generic object representer, which serializes it with
unsafe
!!python/object/apply:numpy...tags (including base64-encodedbinary state). The resulting README.md is technically written, but it
can no longer be parsed back with the strict
yaml.safe_loadused tovalidate dataset card metadata, hence the "unknown tag" error.
Fix
Coerce every element of
ClassLabel.namesto a plainstrin__post_init__, mirroring what already happens for_int2str. Thisguarantees only native Python strings ever reach the YAML dumper,
regardless of where
namesoriginally came from.Testing
(
test_class_label_names_are_coerced_to_strintests/features/test_features.py) that builds aClassLabelfromnumpy.str_values, dumps its YAML representation, and asserts itround-trips through
yaml.safe_load— this reproduces the exactfailure from the issue and fails without the fix.
tests/features/test_features.py,tests/test_info.py,and
tests/test_metadata_util.pysuites locally: 226 passed, 3skipped, no regressions.
derived via
numpy.unique, cast toSequence(ClassLabel(names=list(labels))), then dumping the resultingDatasetInfoto YAML) — the dump now round-trips throughyaml.safe_loadcleanly instead of raisingConstructorError.Scope
Only
src/datasets/features/features.py(the fix) andtests/features/test_features.py(the regression test) are touched. Nounrelated code or docs were changed.
Note: this PR was prepared with AI assistance (Claude Code) under my
direction — I reviewed the root-cause analysis, the fix, and the test
before submitting.