fix(json): produce valid JSON when lines=False spans multiple batches (#7037)#8317
Open
sridhar-3009 wants to merge 2 commits into
Open
fix(json): produce valid JSON when lines=False spans multiple batches (#7037)#8317sridhar-3009 wants to merge 2 commits into
sridhar-3009 wants to merge 2 commits into
Conversation
`add_column` delegates to `map` but never updates the returned dataset's features schema. Calling `add_column` on a typed IterableDataset silently drops the `features` attribute (returns `None`), breaking any code that inspects schema after the operation. Follow the same pattern as `rename_columns`: save the existing features before the map call, infer the new column's type from the column data via PyArrow, then write the merged Features back to `_info.features`. Fixes huggingface#5752.
…iple batches
When `lines=False` and `batch_size < num_rows`, each batch was written
as its own JSON array (`[{...}]`), producing concatenated arrays that
are invalid JSON. The code guarded this with a `NotImplementedError`
for all orientations.
Fix: lift the error for array-producing orientations (`records`,
`values`) and merge batches in `_write` by stripping the outer `[`/`]`
from each batch's bytes and emitting a single wrapping `[...]`.
Dict-producing orientations (`split`, `index`, `columns`, `table`)
still raise `NotImplementedError` when `batch_size < num_rows` since
their output cannot be meaningfully merged across batches.
Also refactors `_write` to share a single iteration loop between the
single-proc and multiprocessing paths via `contextlib.nullcontext`.
Fixes huggingface#7037
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 #7037.
Dataset.to_json(lines=False)raisedNotImplementedError(and before the guard was added, silently produced invalid JSON) wheneverbatch_size < num_rows— i.e., the default case for any dataset larger thanDEFAULT_MAX_BATCH_SIZE(1000 rows).Root cause: Each batch was serialised to its own JSON array (
[{...}, ...]) and written back-to-back in the file, producing e.g.[{...}][{...}]— syntactically invalid.Fix (two changes in
src/datasets/io/json.py):write()— Narrow theNotImplementedErrorguard to dict-producing orientations (split,index,columns,table) only. Array-producing orientations (records,values) can now be batched safely._write()— Whenlines=Falseandorient in ("records", "values"), strip the outer[/]from each batch's bytes and emit a single wrapping[...], streaming with no extra memory overhead. Dict-producing orientations fall through to the unchanged write-as-is path.As a bonus, the single-proc and multiprocessing iteration loops are unified via
contextlib.nullcontext, removing ~10 lines of duplication.Verified:
Test plan
test_dataset_to_json_lines_false_batchedcoversorient="records"andorient="values"withbatch_size < num_rowsTestJsonDatasetWritersuite passes (20 tests; 3 pre-existing errors forshared_datadirfixture from missingpytest-datadirplugin, unrelated to this change)