Skip to content

Studio: keep JSONL schema recovery working with a UTF-8 BOM - #18015

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
DimitriChrysafis:fix/studio-json-schema-bom
Nov 18, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
DimitriChrysafis:fix/studio-json-schema-bom

Conversation

@DimitriChrysafis

Copy link
Copy Markdown
Contributor

JSONL schema recovery fails when a file starts with a UTF-8 BOM. The ordinary datasets loader accepts that preamble, but when a later block or shard adds a column or message field, json_rows_features rereads the files as plain UTF-8. The BOM reaches json.loads, the scan returns no schema, and loading ends with the original dataset-generation error. The same rows without a BOM recover normally.

Read the schema scan with utf-8-sig, matching the JSONL loader's handling of the preamble. BOM characters inside training text remain intact, and invalid UTF-8 still prevents schema recovery.

Validation on macOS, Python 3.12.9, datasets 4.3.0, PyArrow 21.0.0 and Hugging Face Hub 1.32.0:

  • Seven regression cases fail on main. They cover BOMs in either or both shards, new top-level and nested message fields, and an optional field after the first read block.
  • All 14 schema-drift tests pass, including the fixed-schema BOM and invalid-UTF-8 controls.
  • All 69 selected JSON schema, cache retry, CSV preservation and CLI media tests pass, including unsloth_cli/tests/test_train_vision_dataset.py.
  • Repository-pinned Ruff lint and formatter, duplicate-definition and import-hoist checks, and git diff --check pass.
python -m pytest studio/backend/tests/test_dataset_json_schema_drift.py \
  studio/backend/tests/test_dataset_cache_safe.py \
  studio/backend/tests/test_csv_cells_as_written.py \
  studio/backend/tests/test_csv_differing_headers.py \
  studio/backend/tests/test_cli_media_check_imports.py \
  unsloth_cli/tests/test_train_vision_dataset.py -q -rs --tb=short --timeout=60

These use real local dataset loads; model operations in the CSV and CLI tests are mocked. No model training or GPU run. This verifies JSONL input; datasets 4.3.0 still rejects JSON arrays with a BOM in its own reader.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-18T13:38:22.580440Z
We couldn’t complete this review. Comment @codex review to try again.
9df7f54 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: f4fcf4f745

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 9df7f544ca

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Nov 18, 2026
#18324)

## Multi-LoRA packing: load the num_proc policy without importing
unsloth


`studio/backend/tests/test_dataset_map_num_proc.py::test_the_batched_packing_pool_asks_the_same_veto`
fails in `(Python 3.13, a-f)` on every PR since #17701, for example
#18015, #18108, #18142 and #18303:

```
core/training/multi_lora_trainer.py:686: in _pack_num_proc
    from unsloth_zoo.dataset_num_proc import get_dataset_num_proc
E   ModuleNotFoundError: No module named 'unsloth_zoo'
core/training/multi_lora_trainer.py:688: in _pack_num_proc
    from unsloth.dataset_num_proc import get_dataset_num_proc
unsloth/_gpu_init.py:241: in <module>
E   ImportError: Unsloth: Please install unsloth_zoo via `pip install unsloth_zoo` then retry!
```

### Cause

`_pack_num_proc`, the multi-LoRA packing pool's worker count from
#15733, looks up the shared `dataset_num_proc` policy in two steps:

1. it imports `unsloth_zoo`;
2. if that fails, it falls back to `from unsloth.dataset_num_proc import
...`.

That fallback runs the whole `unsloth` package `__init__`, which needs
`unsloth_zoo` anyway and patches torch on the way.

The rest of the backend already avoids this.
`utils.hardware.hardware._shared_policy()` tries `unsloth_zoo` first and
otherwise loads the stdlib-only `dataset_num_proc.py` straight off disk,
precisely so that no caller runs `import unsloth`.

#17701 added the first test that reaches `_pack_num_proc`, and the
backend shard runs without `unsloth_zoo`, so the fallback fails there.

### Fix

`_pack_num_proc` now gets the policy from `_shared_policy()`, the same
loader the single-job tokenize pool uses.

- **With a policy**, which is every real install: the call is unchanged,
`get_dataset_num_proc(count)` on the same module.
- **With no policy at all:** it keeps that function's contract, so a
serial count of 1 still means `None`.

### Verification

- Faithful repro of the CI state: `core.training.trainer` already
imported, then `unsloth_zoo` made unimportable, then `_pack_num_proc`
called with a tight host-memory snapshot.
  - `main`: `ModuleNotFoundError: No module named 'unsloth_zoo'`.
  - This PR: returns `None`, as the test expects.
- With `unsloth_zoo` installed: `tests/test_dataset_map_num_proc.py` and
the multi-LoRA tests pass (88 passed).
- ruff is clean.
@danielhanchen
danielhanchen marked this pull request as ready for review November 18, 2026 13:38
@danielhanchen
danielhanchen self-requested a review as a code owner November 18, 2026 13:38
@danielhanchen
danielhanchen merged commit 10d3fc2 into unslothai:main Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants