Skip to content

fix(dataprep): skip .jsonl lines that are valid JSON but not objects - #7195

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
chuenchen309:fix/json-non-dict-line
Jul 18, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
chuenchen309:fix/json-non-dict-line

Conversation

@chuenchen309

Copy link
Copy Markdown
Contributor

Summary

_read_file_by_format json.loads each line of a .jsonl and passes the result to _extract_text_from_json, which assumes it got a dict:

for field in self._TEXT_FIELDS:
    if field in data and isinstance(data[field], str):
        return data[field]

A JSON line doesn't 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, and data[field] raises:

line why it trips result
"context" "text" in "context" is True — substring TypeError: string indices must be integers
["text", "foo"] "text" in [...] is True — membership TypeError: list indices must be integers
42 not iterable TypeError: argument of type 'int' is not iterable

The TypeError escapes past except json.JSONDecodeError: continue, so one odd line kills the whole load.

That except is 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 (body is also in _TEXT_FIELDS).

Reachability

unsloth-cli.py:253 — args.dataset.endswith((".txt", ".md", ".json", ".jsonl")) auto-detects and calls load_from_file. RawTextDataLoader is exported from unsloth/__init__.py.

Testing

Added to tests/test_raw_text.py, following the file's existing per-test MockTokenizer convention. The new test fails on main and passes here; the two existing tests pass on both. python tests/test_raw_text.py and pytest tests/test_raw_text.py are both green (3 passed).

I deliberately didn't run scripts/run_ruff_format.py over the test file — it reformats pre-existing assert (...) blocks that have nothing to do with this change, and CI doesn't run ruff format. ruff check is 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.

`_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]>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 90e9f0420f

ℹ️ 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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 544a1b63d3

ℹ️ 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
danielhanchen merged commit e55d0e6 into unslothai:main Jul 18, 2026
45 of 47 checks passed
VectorCipher pushed a commit to VectorCipher/unsloth that referenced this pull request Jul 20, 2026
…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]>
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.

2 participants