Skip to content

Commit 0bb2865

Browse files
authored
Fix JSONL loader handling of blank/invalid lines (#2434)
* Fix JSONL loader for blank and invalid rows * Sync docs for schema and metadata fields * Revert "Sync docs for schema and metadata fields" This reverts commit 55f170f.
1 parent 82c339a commit 0bb2865

4 files changed

Lines changed: 52 additions & 1 deletion

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "patch",
3+
"description": "Fix JSONL input loader to skip blank lines and ignore invalid JSON rows"
4+
}

packages/graphrag-input/graphrag_input/jsonl.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,28 @@ async def read_file(self, path: str) -> list[TextDocument]:
3434
- output - list with a TextDocument for each row in the file.
3535
"""
3636
text = await self._storage.get(path, encoding=self._encoding)
37-
rows = [json.loads(line) for line in text.splitlines()]
37+
rows: list[dict] = []
38+
for line_number, line in enumerate(text.splitlines(), start=1):
39+
if not line.strip():
40+
continue
41+
42+
try:
43+
parsed_row = json.loads(line)
44+
except json.JSONDecodeError:
45+
logger.warning(
46+
"Skipping malformed JSONL row in %s at line %s",
47+
path,
48+
line_number,
49+
)
50+
continue
51+
52+
if isinstance(parsed_row, dict):
53+
rows.append(parsed_row)
54+
else:
55+
logger.warning(
56+
"Skipping non-object JSONL row in %s at line %s",
57+
path,
58+
line_number,
59+
)
60+
3861
return await self.process_data_columns(rows, path)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{ "title": "Hello", "text": "Hi how are you today?"}
2+
3+
not json
4+
{ "title": "Goodbye", "text": "I'm outta here"}
5+
6+
["not", "an", "object"]
7+
{ "title": "Adios", "text": "See you later"}

tests/unit/indexing/input/test_jsonl_loader.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,20 @@ async def test_jsonl_loader_one_file_with_title():
4040
documents = await reader.read_files()
4141
assert len(documents) == 3
4242
assert documents[0].title == "Hello"
43+
44+
45+
async def test_jsonl_loader_skips_blank_and_invalid_rows():
46+
config = InputConfig(
47+
type=InputType.JsonLines,
48+
title_column="title",
49+
)
50+
storage = create_storage(
51+
StorageConfig(
52+
base_dir="tests/unit/indexing/input/data/jsonl-with-invalid-and-blank-lines",
53+
)
54+
)
55+
reader = create_input_reader(config, storage)
56+
documents = await reader.read_files()
57+
58+
assert len(documents) == 3
59+
assert [document.title for document in documents] == ["Hello", "Goodbye", "Adios"]

0 commit comments

Comments
 (0)