From 3ab4e2157968df572dc62b3ffa6d8a62b283afa9 Mon Sep 17 00:00:00 2001 From: Bagatur <22008038+baskaryan@users.noreply.github.com> Date: Thu, 10 Aug 2023 23:39:25 -0700 Subject: [PATCH] fix json tool (#9096) --- libs/langchain/langchain/tools/json/tool.py | 2 +- libs/langchain/tests/unit_tests/tools/test_json.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/libs/langchain/langchain/tools/json/tool.py b/libs/langchain/langchain/tools/json/tool.py index 6f6473d51e..6c75de20ce 100644 --- a/libs/langchain/langchain/tools/json/tool.py +++ b/libs/langchain/langchain/tools/json/tool.py @@ -20,7 +20,7 @@ def _parse_input(text: str) -> List[Union[str, int]]: """Parse input of the form data["key1"][0]["key2"] into a list of keys.""" _res = re.findall(r"\[.*?]", text) # strip the brackets and quotes, convert to int if possible - res = [i[1:-1].replace('"', "") for i in _res] + res = [i[1:-1].replace('"', "").replace("'", "") for i in _res] res = [int(i) if i.isdigit() else i for i in res] return res diff --git a/libs/langchain/tests/unit_tests/tools/test_json.py b/libs/langchain/tests/unit_tests/tools/test_json.py index 36a96595e0..b677b1577d 100644 --- a/libs/langchain/tests/unit_tests/tools/test_json.py +++ b/libs/langchain/tests/unit_tests/tools/test_json.py @@ -30,6 +30,10 @@ def test_json_spec_value() -> None: assert spec.value('data["baz"]') == "{'test': {'foo': [1, 2, 3]}}" assert spec.value('data["baz"]["test"]') == "{'foo': [1, 2, 3]}" assert spec.value('data["baz"]["test"]["foo"]') == "[1, 2, 3]" + assert spec.value("data['foo']") == "bar" + assert spec.value("data['baz']") == "{'test': {'foo': [1, 2, 3]}}" + assert spec.value("data['baz']['test']") == "{'foo': [1, 2, 3]}" + assert spec.value("data['baz']['test']['foo']") == "[1, 2, 3]" def test_json_spec_value_max_length() -> None: