diff --git a/haystack/components/converters/json.py b/haystack/components/converters/json.py index 9510ed9fcb..300826b196 100644 --- a/haystack/components/converters/json.py +++ b/haystack/components/converters/json.py @@ -238,6 +238,9 @@ def _get_content_and_meta(self, source: ByteStream) -> list[tuple[str, dict[str, logger.warning("Expected a scalar value but got {obj}. Skipping it.", obj=obj) continue + if text is not None and not isinstance(text, str): + text = str(text) + meta = {} if meta_fields == "*": meta = {k: v for k, v in obj.items() if k != self._content_key} diff --git a/releasenotes/notes/json-converter-scalar-content-163133a47060a2e7.yaml b/releasenotes/notes/json-converter-scalar-content-163133a47060a2e7.yaml new file mode 100644 index 0000000000..eaf729f667 --- /dev/null +++ b/releasenotes/notes/json-converter-scalar-content-163133a47060a2e7.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixed ``JSONConverter`` failing with ``ValueError`` when ``content_key`` contains numeric or boolean scalar values. + These values are now converted to strings before creating the ``Document``, while ``null`` values remain unchanged. diff --git a/test/components/converters/test_json.py b/test/components/converters/test_json.py index 59fee4875b..c3b5d37f48 100644 --- a/test/components/converters/test_json.py +++ b/test/components/converters/test_json.py @@ -511,6 +511,16 @@ def test_run_with_content_key(tmpdir): assert result["documents"][2].meta == {} +@pytest.mark.parametrize("value, expected", [(123, "123"), (1.5, "1.5"), (True, "True"), (None, None)]) +def test_run_with_content_key_with_scalar_values(value, expected): + source = ByteStream.from_string(json.dumps({"body": value})) + + converter = JSONConverter(content_key="body") + result = converter.run(sources=[source]) + + assert result["documents"][0].content == expected + + def test_run_with_content_key_and_extra_meta_fields(tmpdir): first_test_file = Path(tmpdir / "first_test_file.json") second_test_file = Path(tmpdir / "second_test_file.json")