Skip to content

Preserve Dataset.map output types over JSON list columns - #9325

Open
ldj7672 wants to merge 2 commits into
huggingface:mainfrom
ldj7672:fix/map-list-json-type-change
Open

ldj7672 wants to merge 2 commits into
huggingface:mainfrom
ldj7672:fix/map-list-json-type-change

Conversation

@ldj7672

@ldj7672 ldj7672 commented Dec 4, 2026

Copy link
Copy Markdown

Fixes #9324.

When Dataset.map() replaces a List(Json()) column with a string or a mapping, attempting to reuse the input feature can silently store a list of characters or keys. This was reported in an offline GRPO chat-template workflow. I reproduced the same failure with a local JSONL fixture containing messages with different key sets.

The JSON encoder now rejects strings, bytes, and mappings at a list step. The Arrow writer then falls back to inferring the mapped column's actual type. This leaves list-like inputs on the existing encoding path.

Verification

  • Four regression cases cover row and batch mapping with string and mapping outputs. All four fail on upstream main and pass with this change on PyArrow 24.0.0.
  • Confirmed the JSONL workflow with row and batch mapping, both with and without two workers.
  • The Arrow writer, Arrow dataset, table, features, and JSON test suites passed on macOS arm64 with PyArrow 24.0.0 (1,130 passed). make quality passed.

AI assistance was used to develop and test the patch.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the new test around lines 4950–4967, please also cover the bytes branch through persistence. This concrete addition passes on 466fa31 and fails on parent json.py because the feature remains List(Json()) instead of binary (five selected cases pass on head, all five fail on parent):

def test_map_list_json_to_binary_survives_disk_roundtrip(tmp_path):
    dataset = Dataset.from_dict(
        {"messages": [[{"role": "user"}], [{"role": "assistant", "thinking": "next"}]]},
        features=Features({"messages": List(Json())}),
    )
    payload = bytes([0, 255, 10, 65])
    mapped = dataset.map(lambda batch: {"messages": [payload] * len(batch["messages"])}, batched=True)
    assert mapped.features == Features({"messages": Value("binary")})
    assert mapped.to_dict() == {"messages": [payload, payload]}
    mapped.save_to_disk(tmp_path / "mapped")
    restored = load_from_disk(tmp_path / "mapped")
    assert restored.features == mapped.features
    assert restored.to_dict() == mapped.to_dict()

AI-assisted test suggestion, based on this PR reproducer; Windows CPU/local files, Ruff passed. No remote data, multiprocessing or full-suite claim.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the disk round-trip regression in 93feaea. It maps two rows to distinct byte payloads, then checks that the feature is Value("binary") and the bytes are identical before and after save_to_disk and load_from_disk.

On macOS arm64 with PyArrow 24.0.0, all five focused regressions fail on upstream main and pass on the PR. The related Arrow, feature, and JSON suites finished with 1,156 passed and 45 skipped. make style and make quality passed. I also verified the binary round-trip with row and batch mapping using one and two processes on PyArrow 26.0.1.

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.

Dataset.map stores a string returned over a List(Json) column as a list of its characters

2 participants