diff --git a/libs/community/langchain_community/document_loaders/json_loader.py b/libs/community/langchain_community/document_loaders/json_loader.py index d255523e7f2b5..ce86b9850e777 100644 --- a/libs/community/langchain_community/document_loaders/json_loader.py +++ b/libs/community/langchain_community/document_loaders/json_loader.py @@ -188,7 +188,7 @@ def _get_text(self, sample: Any) -> str: # In case the text is None, set it to an empty string elif isinstance(content, str): return content - elif isinstance(content, dict): + elif isinstance(content, (dict, list)): return json.dumps(content) if content else "" else: return str(content) if content is not None else "" diff --git a/libs/community/tests/unit_tests/document_loaders/test_json_loader.py b/libs/community/tests/unit_tests/document_loaders/test_json_loader.py index c4b1df4f2a30f..2f4e066e304b5 100644 --- a/libs/community/tests/unit_tests/document_loaders/test_json_loader.py +++ b/libs/community/tests/unit_tests/document_loaders/test_json_loader.py @@ -1,4 +1,5 @@ import io +from pathlib import Path from typing import Any, Dict import pytest @@ -12,7 +13,7 @@ def test_load_valid_string_content(mocker: MockerFixture) -> None: - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content="value1", @@ -37,7 +38,7 @@ def test_load_valid_string_content(mocker: MockerFixture) -> None: def test_load_valid_dict_content(mocker: MockerFixture) -> None: - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content='{"text": "value1"}', @@ -64,7 +65,7 @@ def test_load_valid_dict_content(mocker: MockerFixture) -> None: def test_load_valid_bool_content(mocker: MockerFixture) -> None: - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content="False", @@ -93,7 +94,7 @@ def test_load_valid_bool_content(mocker: MockerFixture) -> None: def test_load_valid_numeric_content(mocker: MockerFixture) -> None: - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content="99", @@ -122,7 +123,7 @@ def test_load_valid_numeric_content(mocker: MockerFixture) -> None: def test_load_invalid_test_content(mocker: MockerFixture) -> None: - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) mocker.patch("builtins.open", mocker.mock_open()) mocker.patch( @@ -139,7 +140,7 @@ def test_load_invalid_test_content(mocker: MockerFixture) -> None: def test_load_jsonlines(mocker: MockerFixture) -> None: - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content="value1", @@ -177,7 +178,7 @@ def test_load_jsonlines(mocker: MockerFixture) -> None: ), ) def test_load_jsonlines_list(params: Dict, mocker: MockerFixture) -> None: - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content="value1", @@ -250,7 +251,7 @@ def test_json_meta_01( mocker.patch("builtins.open", mocker.mock_open()) mocker.patch(patch_func, return_value=patch_func_value) - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content="value1", @@ -300,7 +301,7 @@ def test_json_meta_02( mocker.patch("builtins.open", mocker.mock_open()) mocker.patch(patch_func, return_value=patch_func_value) - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content="value1", @@ -336,7 +337,7 @@ def metadata_func(record: Dict, metadata: Dict) -> Dict: def test_load_json_with_jq_parsable_content_key( params: Dict, mocker: MockerFixture ) -> None: - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content="value1", @@ -364,7 +365,7 @@ def test_load_json_with_jq_parsable_content_key( def test_load_json_with_nested_jq_parsable_content_key(mocker: MockerFixture) -> None: - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content="message1", @@ -401,7 +402,7 @@ def test_load_json_with_nested_jq_parsable_content_key(mocker: MockerFixture) -> def test_load_json_with_nested_jq_parsable_content_key_with_metadata( mocker: MockerFixture, ) -> None: - file_path = "/workspaces/langchain/test.json" + file_path = str(Path("/workspaces/langchain/test.json").resolve()) expected_docs = [ Document( page_content="message1",