Repository navigation
fix(dataprep): skip .jsonl lines that are valid JSON but not objects - #7195
Conversation
`_read_file_by_format` json.loads each line and hands the result to
`_extract_text_from_json`, which assumes a dict:
for field in self._TEXT_FIELDS:
if field in data and isinstance(data[field], str):
A JSON line does not have to be an object -- `"context"`, `["text"]` and
`42` are all valid JSON. For those, `field in data` stops being a key
lookup and becomes a substring/membership test, so `data[field]` raises:
"context" -> "text" in "context" is True (substring!)
-> TypeError: string indices must be integers
["text", "foo"] -> TypeError: list indices must be integers
42 -> TypeError: argument of type 'int' is not iterable
The TypeError escapes past `except json.JSONDecodeError: continue`, so the
whole load dies on one odd line.
That except clause is also the tell: a *malformed* line is already skipped
gracefully. A *well-formed* line that happens not to be an object should be
too -- it carries no text either way. This makes the two agree.
Reachable from `unsloth-cli.py:253` (`--dataset foo.jsonl` auto-detect) and
`RawTextDataLoader` is exported from `unsloth/__init__.py`.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request adds a check in _extract_text_from_json to ensure the parsed JSON data is a dictionary before extracting text. This prevents TypeError exceptions when processing valid non-object JSON lines (such as strings, lists, or numbers). A unit test has also been added to verify that these non-object JSON lines are safely skipped. I have no feedback to provide as the implementation is correct and well-tested.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…nslothai#7195) * fix(dataprep): skip .jsonl lines that are valid JSON but not objects `_read_file_by_format` json.loads each line and hands the result to `_extract_text_from_json`, which assumes a dict: for field in self._TEXT_FIELDS: if field in data and isinstance(data[field], str): A JSON line does not have to be an object -- `"context"`, `["text"]` and `42` are all valid JSON. For those, `field in data` stops being a key lookup and becomes a substring/membership test, so `data[field]` raises: "context" -> "text" in "context" is True (substring!) -> TypeError: string indices must be integers ["text", "foo"] -> TypeError: list indices must be integers 42 -> TypeError: argument of type 'int' is not iterable The TypeError escapes past `except json.JSONDecodeError: continue`, so the whole load dies on one odd line. That except clause is also the tell: a *malformed* line is already skipped gracefully. A *well-formed* line that happens not to be an object should be too -- it carries no text either way. This makes the two agree. Reachable from `unsloth-cli.py:253` (`--dataset foo.jsonl` auto-detect) and `RawTextDataLoader` is exported from `unsloth/__init__.py`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Slim the non-object jsonl regression test and shorten the guard comment --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Co-authored-by: Daniel Han <[email protected]>
Summary
_read_file_by_formatjson.loadseach line of a.jsonland passes the result to_extract_text_from_json, which assumes it got a dict:A JSON line doesn't have to be an object —
"context",["text"]and42are all valid JSON. For those,field in datastops being a key lookup and becomes a substring / membership test, anddata[field]raises:"context""text" in "context"isTrue— substringTypeError: string indices must be integers["text", "foo"]"text" in [...]isTrue— membershipTypeError: list indices must be integers42TypeError: argument of type 'int' is not iterableThe
TypeErrorescapes pastexcept json.JSONDecodeError: continue, so one odd line kills the whole load.That
exceptis also the tell: a malformed line is already skipped gracefully. A well-formed line that just isn't an object should be too — it carries no text either way. This makes the two agree."context"containing"text"is what makes it easy to hit rather than exotic;"subtext"and"a body of work"do it too (bodyis also in_TEXT_FIELDS).Reachability
unsloth-cli.py:253—args.dataset.endswith((".txt", ".md", ".json", ".jsonl"))auto-detects and callsload_from_file.RawTextDataLoaderis exported fromunsloth/__init__.py.Testing
Added to
tests/test_raw_text.py, following the file's existing per-test MockTokenizer convention. The new test fails onmainand passes here; the two existing tests pass on both.python tests/test_raw_text.pyandpytest tests/test_raw_text.pyare both green (3 passed).I deliberately didn't run
scripts/run_ruff_format.pyover the test file — it reformats pre-existingassert (...)blocks that have nothing to do with this change, and CI doesn't run ruff format.ruff checkis clean, and the diff is 55 insertions with no deletions.Disclosure: written with Claude Code. I verified the reproduction, the reachability, and the test red/green myself.