Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

feat(ingest): support line-delimited JSON files (#1486)

sunchaokun3933774e2692
diff --git a/semantica/cli.py b/semantica/cli.py
index df8d4c77..d233f685 100644
--- a/semantica/cli.py
+++ b/semantica/cli.py
@@ -1076,7 +1076,7 @@ def init_cmd(cli_ctx: CLIContext, force: bool) -> None:
@click.argument("path", default=".", type=click.Path(exists=True))
@click.option("--type", "ingestor_type", default=None, help="Force ingestor type.")
@click.option("--store", "store_override", default=None, help="Target graph backend.")
[email protected]("--patterns", default="*.pdf,*.docx,*.txt,*.csv,*.json",
[email protected]("--patterns", default="*.pdf,*.docx,*.txt,*.csv,*.json,*.jsonl,*.ndjson",
show_default=True, help="Comma-separated glob patterns to match.")
@click.pass_obj
def watch_cmd(cli_ctx: CLIContext, path: str, ingestor_type: Optional[str],
@@ -1923,7 +1923,9 @@ _INGEST_TYPES = [
"snowflake", "stream",
]
-_INGEST_FORMATS = ["pdf", "docx", "csv", "excel", "html", "json", "parquet", "xml", "rdf"]
+_INGEST_FORMATS = [
+ "pdf", "docx", "csv", "excel", "html", "json", "jsonl", "ndjson", "parquet", "xml", "rdf"
+]
_GRAPH_STORE_ENV_BACKEND_HINTS = {
"GRAPH_STORE_NEO4J_URI": "neo4j",
"GRAPH_STORE_FALKORDB_HOST": "falkordb",
diff --git a/semantica/ingest/file_ingestor.py b/semantica/ingest/file_ingestor.py
index 752f2720..f2a4ca41 100644
--- a/semantica/ingest/file_ingestor.py
+++ b/semantica/ingest/file_ingestor.py
@@ -109,6 +109,8 @@ class FileTypeDetector:
# Initialize Python's MIME types database
mimetypes.init()
+ mimetypes.add_type("application/x-ndjson", ".ndjson")
+ mimetypes.add_type("application/x-ndjson", ".jsonl")
self.logger.debug(
"File type detector initialized with "
diff --git a/semantica/utils/constants.py b/semantica/utils/constants.py
index f06020d7..9efaac91 100644
--- a/semantica/utils/constants.py
+++ b/semantica/utils/constants.py
@@ -54,6 +54,8 @@ SUPPORTED_DOCUMENT_FORMATS = [
"html",
"xml",
"json",
+ "jsonl",
+ "ndjson",
"csv",
"xlsx",
"pptx",
diff --git a/tests/ingest/test_file_ingestor.py b/tests/ingest/test_file_ingestor.py
index dc58c4d6..de7d6592 100644
--- a/tests/ingest/test_file_ingestor.py
+++ b/tests/ingest/test_file_ingestor.py
@@ -122,6 +122,38 @@ def test_type_detector_extended() -> None:
assert detected_unknown == "unknown"
[email protected]("suffix", [".jsonl", ".ndjson"])
+def test_type_detector_supports_line_delimited_json(suffix: str) -> None:
+ """Line-delimited JSON is a common interchange format for bulk data."""
+
+ detector = FileTypeDetector()
+
+ assert detector.detect_type(f"resources{suffix}") == suffix.lstrip(".")
+ assert detector.is_supported(suffix.lstrip("."))
+
+
[email protected]("suffix", [".jsonl", ".ndjson"])
+def test_ingest_line_delimited_json_file_preserves_content(
+ tmp_path: Path, suffix: str
+) -> None:
+ """FHIR Bulk/NDJSON resources can be ingested as a supported file."""
+
+ path = tmp_path / f"Patient.000{suffix}"
+ path.write_text(
+ '{"resourceType":"Patient","id":"synthetic-1"}\n'
+ '{"resourceType":"Patient","id":"synthetic-2"}\n',
+ encoding="utf-8",
+ )
+
+ result = FileIngestor().ingest_file(path)
+
+ expected_type = suffix.lstrip(".")
+ assert result.file_type == expected_type
+ assert result.mime_type == "application/x-ndjson"
+ assert result.metadata["is_supported"] is True
+ assert result.text.count('"resourceType":"Patient"') == 2
+
+
# --- CloudStorageIngestor Tests ---
@patch("boto3.client")
def test_cloud_storage_s3(mock_boto: MagicMock) -> None:
diff --git a/tests/test_cli_commands.py b/tests/test_cli_commands.py
index bb0e6d42..67114925 100644
--- a/tests/test_cli_commands.py
+++ b/tests/test_cli_commands.py
@@ -332,6 +332,35 @@ class TestIngest:
assert captured["kwargs"]["batch_size"] == 500
assert captured["kwargs"]["format"] == "csv"
+ @pytest.mark.parametrize("fmt", ["ndjson", "jsonl"])
+ def test_ingest_format_line_delimited_json(self, runner, monkeypatch, fmt):
+ captured = {}
+
+ def fake_ingest_file(sources, **kwargs):
+ captured["sources"] = sources
+ captured["kwargs"] = kwargs
+ return [{"path": sources}]
+
+ monkeypatch.setattr("semantica.ingest.methods.ingest_file", fake_ingest_file)
+
+ result = runner.invoke(
+ cli_module.main,
+ ["ingest", f"data.{fmt}", "--type", "file", "--format", fmt, "--json"],
+ )
+
+ _ok(result)
+ data = _json_output(result)
+ assert data["files"] == [{"path": f"data.{fmt}"}]
+ assert captured["sources"] == f"data.{fmt}"
+ assert captured["kwargs"]["method"] == "file"
+ assert captured["kwargs"]["format"] == fmt
+
+ def test_watch_help_shows_line_delimited_json_patterns(self, runner):
+ result = runner.invoke(cli_module.main, ["watch", "--help"])
+ _ok(result)
+ assert "*.jsonl" in result.output
+ assert "*.ndjson" in result.output
+
def test_runtime_path_passes_source_positionally_with_auto_detection(self, runner, monkeypatch):
captured = {}