Fix JSON loader schema inference for files starting with a UTF-8 BOM (#8241) - #8243
Merged
Merged
Conversation
…uggingface#8241) A leading UTF-8 BOM made the first-batch mixed-struct pre-scan (ujson) raise ValueError, which was silently swallowed, so the schema-unification path ran instead and materialized spurious nulls. PyArrow tolerates the BOM, so the inferred schema depended on whether the file started with one. Strip the BOM so the pre-scan and PyArrow see the same bytes. Adds a regression test.
|
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. |
Elsword016
pushed a commit
to Elsword016/datasets
that referenced
this pull request
Aug 6, 2026
…uggingface#8241) (huggingface#8243) A leading UTF-8 BOM made the first-batch mixed-struct pre-scan (ujson) raise ValueError, which was silently swallowed, so the schema-unification path ran instead and materialized spurious nulls. PyArrow tolerates the BOM, so the inferred schema depended on whether the file started with one. Strip the BOM so the pre-scan and PyArrow see the same bytes. Adds a regression test. Co-authored-by: archievi <13202986+archievi@users.noreply.github.com>
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.
Fixes #8241.
What's the bug
When a JSONL file starts with a UTF-8 BOM (
\xef\xbb\xbf),load_dataset("json", ...)infers a different schema than the same file without the BOM.The JSON builder's first-batch "mixed-struct-types" pre-scan parses each line with
ujsonto decide whether to preserve heterogeneous nested dicts as aJsontype:ujsonrejects the leading BOM withValueError, the broadexcept ValueErrorswallows it, andfind_mixed_struct_types_field_pathsis never called. PyArrow'sread_jsontolerates the BOM and instead unifies the schema, materializing spuriousnulls for fields absent from individual records. So the user-visible content silently changes depending on whether the file happens to start with a BOM.Fix
Strip a leading UTF-8 BOM from the first batch (guarded to the start of the file) so the pre-scan and PyArrow operate on the same bytes.
Test
Added a regression test: a BOM-prefixed variant of the existing
jsonl_file_with_lists_of_dicts_of_varying_keysfixture, asserting it produces the same schema (EXPECTED_LISTS_OF_DICTS_WITH_VARYING_KEYS) as the non-BOM file. It fails onmainand passes with this change.ruff check,ruff format, andpytest tests/packaged_modules/test_json.py::test_json_generate_tablesall pass locally.