fix: preserve features in IterableDataset.add_column (fixes #5752)#8115
Open
IgnazioDS wants to merge 1 commit intohuggingface:mainfrom
Open
fix: preserve features in IterableDataset.add_column (fixes #5752)#8115IgnazioDS wants to merge 1 commit intohuggingface:mainfrom
IgnazioDS wants to merge 1 commit intohuggingface:mainfrom
Conversation
…ce#5752) map() unconditionally sets info.features to its `features` parameter, which defaults to None. add_column() called map() without passing features, so every add_column() call wiped out the existing schema. Fix: infer the new column's Arrow type and merge it with the existing features before delegating to map(), so the returned dataset retains the full schema including the newly added column.
lhoestq
approved these changes
Apr 7, 2026
Member
lhoestq
left a comment
There was a problem hiding this comment.
lgtm ! I just added a suggestion, lmk what you think before we merge
| # Without this, map() would set info.features=None (its default), losing all schema info. | ||
| new_features = None | ||
| if self._info.features is not None: | ||
| column_features = _infer_features_from_batch({name: list(column)}) |
Member
There was a problem hiding this comment.
this could help avoid unnecessary data copies, since _infer_features_from_batch does copy all the data into arrow
Suggested change
| column_features = _infer_features_from_batch({name: list(column)}) | |
| column_features = _infer_features_from_batch({name: list(column[:config.DEFAULT_MAX_BATCH_SIZE])}) |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
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.
Summary
Fixes #5752.
IterableDataset.add_column()lost the dataset's feature schema on every call because it delegated tomap()without passing thefeaturesargument.map()unconditionally writesinfo.features = features, so the defaultNonesilently overwrote the existing schema.Root cause (
iterable_dataset.py:3459):Fix: infer the new column's Arrow type via the existing
_infer_features_from_batchhelper, merge it with the existing features, and pass the result tomap().If the dataset had no features to begin with (
_info.features is None), behaviour is unchanged —map()receivesNoneas before.Test plan
.add_column(), assert.featuresis not None and contains the new columntest_iterable_dataset.pytests still pass