Skip to content

Commit 76d5686

Browse files
chuenchen309claudedavidsbatista
authored
fix: guard malformed JSON in JSONConverter content_key path (#11980)
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: David S. Batista <dsbatista@gmail.com>
1 parent 07b8d93 commit 76d5686

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

haystack/components/converters/json.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,15 @@ def _get_content_and_meta(self, source: ByteStream) -> list[tuple[str, dict[str,
211211
else:
212212
# We just load the whole file as JSON if the user didn't provide a jq filter.
213213
# We put it in a list even if it's not to ease handling it later on.
214-
objects = [json.loads(file_content)]
214+
try:
215+
objects = [json.loads(file_content)]
216+
except json.JSONDecodeError as exc:
217+
logger.warning(
218+
"Failed to extract text from {source}. Skipping it. Error: {error}",
219+
source=source.meta["file_path"],
220+
error=exc,
221+
)
222+
return []
215223

216224
result = []
217225
if self._content_key is not None:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed ``JSONConverter`` crashing on a malformed JSON source when it is
5+
configured with ``content_key`` and no ``jq_schema``. The non-jq branch
6+
called ``json.loads`` without a guard, so a single invalid ``.json`` file
7+
raised an unhandled ``JSONDecodeError`` and aborted the whole run. Invalid
8+
JSON is now caught, logged, and skipped, matching the behavior of the
9+
``jq_schema`` path.

test/components/converters/test_json.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,23 @@ def test_run_with_bad_encoding(tmpdir, caplog):
258258
assert result == {"documents": []}
259259

260260

261+
def test_run_with_malformed_json_and_content_key(tmpdir, caplog):
262+
test_file = Path(tmpdir / "test_file.json")
263+
test_file.write_text("This is not valid JSON.", "utf-8")
264+
265+
sources = [test_file]
266+
converter = JSONConverter(content_key="motivation")
267+
268+
caplog.clear()
269+
with caplog.at_level(logging.WARNING):
270+
result = converter.run(sources=sources)
271+
272+
records = caplog.records
273+
assert len(records) == 1
274+
assert records[0].msg.startswith(f"Failed to extract text from {test_file}. Skipping it. Error:")
275+
assert result == {"documents": []}
276+
277+
261278
def test_run_with_single_meta(tmpdir):
262279
first_test_file = Path(tmpdir / "first_test_file.json")
263280
second_test_file = Path(tmpdir / "second_test_file.json")

0 commit comments

Comments
 (0)