Skip to content

Add json line support for DocumentLoaderOperator - #72246

Merged
potiuk merged 5 commits into
apache:mainfrom
jeff3071:add-jsonl-support-for-document-loader
Sep 22, 2026
Merged

potiuk merged 5 commits into
apache:mainfrom
jeff3071:add-jsonl-support-for-document-loader

Conversation

@jeff3071

Copy link
Copy Markdown
Contributor

Why

DocumentLoaderOperator supports regular JSON, but it does not recognize JSON Lines (.jsonl) files.

What

  • Add built-in .jsonl parsing without additional dependencies.
  • Produce one document per non-empty line and assign consecutive item_index metadata.
  • Support JSON Lines from file paths, directory discovery, and source_bytes with file_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

::group::Log message source details
/root/airflow/logs/dag_id=example_document_loader_jsonl/run_id=manual__2026-08-29T06:23:56.559631+00:00/task_id=load_events/attempt=1.log
::endgroup::
[2026-08-29T06:23:57.671540Z] INFO - ::group::Pre Execute
Task Identity ti_id=01a04c30-6aa2-7411-a073-f1ad1073cd42 dag_id=example_document_loader_jsonl task_id=load_events run_id=manual__2026-08-29T06:23:56.559631+00:00 try_number=1 map_index=-1
[2026-08-29T06:24:03.707696Z] INFO - DAG bundles loaded: dags-folder
[2026-08-29T06:24:03.708918Z] INFO - Filling up the DagBag from /files/dags/example_document_loader_jsonl.py
[2026-08-29T06:24:03.781146Z] INFO - Worker startup parse complete bundle_name=dags-folder  bundle_version=null  dag_file=example_document_loader_jsonl.py  bundle_prepare_ms=4  dag_file_parse_ms=72 
[2026-08-29T06:24:03.824981Z] INFO - ::endgroup::
[2026-08-29T06:24:03.825318Z] ERROR - Task failed with exceptionValueError: No parser registered for extension '.jsonl'. Supported extensions: .csv, .docx, .json, .md, .pdf, .txt. Set 'parser' explicitly to override auto-detection.

After

::group::Log message source details
/root/airflow/logs/dag_id=example_document_loader_jsonl/run_id=manual__2026-08-29T06:10:10.890844+00:00/task_id=load_events/attempt=1.log
::endgroup::
[2026-08-29T06:10:11.897592Z] INFO - ::group::Pre Execute
Task Identity ti_id=01a04c23-d167-7cbb-b0a1-888ca1cbeed1 dag_id=example_document_loader_jsonl task_id=load_events run_id=manual__2026-08-29T06:10:10.890844+00:00 try_number=1 map_index=-1
[2026-08-29T06:10:17.924766Z] INFO - DAG bundles loaded: dags-folder
[2026-08-29T06:10:17.926232Z] INFO - Filling up the DagBag from /files/dags/example_document_loader_jsonl.py
[2026-08-29T06:10:18.003334Z] INFO - Worker startup parse complete bundle_name=dags-folder  bundle_version=null  dag_file=example_document_loader_jsonl.py  bundle_prepare_ms=3  dag_file_parse_ms=77 
[2026-08-29T06:10:18.046544Z] INFO - ::endgroup::
[2026-08-29T06:10:18.047021Z] INFO - Parsed 3 documents from 1 file(s)
[2026-08-29T06:10:18.047139Z] INFO - ::group::Post Execute
[2026-08-29T06:10:18.047330Z] INFO - Pushing xcom ti=RuntimeTaskInstance(id=UUID('01a04c23-d167-7cbb-b0a1-888ca1cbeed1'), task_id='load_events', dag_id='example_document_loader_jsonl', run_id='manual__2026-08-29T06:10:10.890844+00:00', try_number=1, dag_version_id=UUID('01a04c22-161b-7e24-bb58-fbac6568e066'), map_index=-1, hostname='d5873b0b3ad5', context_carrier={'traceparent': '00-a8201c08aed315888a0215944bfaf514-c055bbb704c1e9ff-00'}, queue='default', task=<Task(DocumentLoaderOperator): load_events>, bundle_instance=LocalDagBundle(name=dags-folder), max_tries=0, start_date=datetime.datetime(2026, 8, 29, 6, 10, 17, 912797, tzinfo=datetime.timezone.utc), end_date=None, state=<TaskInstanceState.RUNNING: 'running'>, is_mapped=False, rendered_map_index=None, sentry_integration='') 
[2026-08-29T06:10:18.095744Z] INFO - ::endgroup::

Was generative AI tooling used to co-author this PR?
  • Yes — Codex (GPT-5.6-sol)

Generated-by: Codex (GPT-5.6-sol) following the guidelines


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {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.

@ColtenOuO ColtenOuO 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.

LGTM

jeff3071 and others added 4 commits September 21, 2026 00:02
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
@potiuk
potiuk force-pushed the add-jsonl-support-for-document-loader branch from b6033b0 to d680310 Compare September 20, 2026 22:04

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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}', '']        # correct

The 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, so Invalid JSON on line 3, column 13 does 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 a source_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.

@jeff3071

Copy link
Copy Markdown
Contributor Author

Thank for reviewing!
Added source_hint in _parse_json_lines_text just like _decode. And the error message would contain source_hint now.

@jeff3071
jeff3071 requested a review from potiuk September 22, 2026 11:45

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@potiuk
potiuk merged commit f0e82be into apache:main Sep 22, 2026
83 checks passed
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.

4 participants