Repository navigation
Add json line support for DocumentLoaderOperator - #72246
Conversation
str.splitlines() also breaks on U+2028, U+2029 and U+0085, which are legal unescaped characters inside a JSON string (JSON only forbids raw U+0000-U+001F there). A valid record containing one was torn in half, reported as invalid, and every later line number was shifted -- which matters because scraped web text is a common input for this operator. A trailing \r from a CRLF file is JSON whitespace, so json.loads still tolerates it. Generated-by: Claude Opus 5
b6033b0 to
d680310
Compare
potiuk
left a comment
There was a problem hiding this comment.
The .jsonl dispatch, the parsers and the docs/docstring updates all line up, and each of the four new tests fails when the change is reverted.
I am not approving this yet, and that is not about the code: @SameerMesiah97's review thread is still open. For what it is worth I think you have already addressed it — the head commit wraps json.JSONDecodeError into a ValueError reporting the real JSONL line rather than the always-1 line of a per-line decode, and test_invalid_json_line pins line 3, column 13. It just needs them to confirm and resolve it. Leaving this as a comment so that stays their call.
I rebased the branch on main (it was 519 commits behind) and pushed one fixup, which I would like your eyes on since it is a correctness change rather than a style one.
splitlines() is the wrong split for JSON Lines
str.splitlines() breaks on more separators than JSON Lines defines. U+2028, U+2029 and U+0085 are legal unescaped characters inside a JSON string — JSON only forbids raw U+0000–U+001F there — so a valid record containing one was torn in half:
>>> json.loads('{"a": "x
y"}') # valid JSON
{'a': 'x
y'}
>>> '{"a": "x
y"}\n{"b": 1}\n'.splitlines()
['{"a": "x', 'y"}', '{"b": 1}'] # 2 bogus parse failures
>>> '{"a": "x
y"}\n{"b": 1}\n'.split("\n")
['{"a": "x
y"}', '{"b": 1}', ''] # correctThe record then failed with Invalid JSON on line N and every subsequent line number was shifted — which matters here because scraped web text is a prime input for a document loader. The fixup switches to text.split("\n") and adds a regression test with an embedded U+2028; I confirmed the trailing \r from CRLF files is JSON whitespace, so json.loads still tolerates it. Restoring splitlines() fails exactly the new test.
Smaller observation
- The error names the line but not the file. In directory mode
execute()loops over many files, soInvalid JSON on line 3, column 13does not say which one — and the operator's own docs set the opposite expectation: "A failed decode includes the offending file path in the error so directory-mode runs are easy to diagnose."_decode()already threads asource_hint; passing one into_parse_json_lines_text(str(file_path), or something like<bytes:.jsonl>for the bytes path) would follow that pattern for one extra argument. I left this one for you rather than guessing at the hint format.
This review was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. The findings
below are observations, not blockers; an Apache Airflow
maintainer — a real person — will take the next look at the
PR. If you think a finding is mis-applied, please reply on
the PR and a maintainer will weigh in.More on how Apache Airflow handles maintainer review:
contributing-docs/05_pull_requests.rst.
|
Thank for reviewing! |
potiuk
left a comment
There was a problem hiding this comment.
Thanks — the open thread is resolved, the split("\n") fixup is kept, and the new source_hint means a bad line in directory mode now points at the actual file, pinned by test_invalid_json_line_in_file_names_source. That was the only thing holding my approval. Approving.
This review was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.More on how Apache Airflow handles maintainer review:
contributing-docs/05_pull_requests.rst.
Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting
Why
DocumentLoaderOperatorsupports regular JSON, but it does not recognize JSON Lines (.jsonl) files.What
.jsonlparsing without additional dependencies.item_indexmetadata.source_byteswithfile_type=".jsonl".Different with JSON file
For example, this regular JSON file produces two documents because its top-level array is expanded:
[ {"body": "First"}, {"body": "Second"} ]In a JSON Lines file, each non-empty line produces exactly one document. The array on the first line remains one document instead of being expanded, so this file also produces two documents:
[{"body": "First"}, {"body": "Second"}] {"body": "Third"}Testing
Before
After
Was generative AI tooling used to co-author this PR?
Generated-by: Codex (GPT-5.6-sol) following the guidelines
{pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.