From 507c195a4bea61acc55b19f0900292804aa658b0 Mon Sep 17 00:00:00 2001 From: chyroc Date: Fri, 29 Dec 2023 07:06:27 +0800 Subject: [PATCH] Patch: improve openai functions call parser compatibility (#15197) ```shell Python 3.11.6 (main, Nov 2 2023, 04:39:43) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> s = {'name': 'gc', 'arguments': '{"prompt":"hi\nbob."}'} >>> import json >>> json.loads(s['arguments']) Traceback (most recent call last): File "", line 1, in File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 346, in loads return _default_decoder.decode(s) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) ^^^^^^^^^^^^^^^^^^^^^^ json.decoder.JSONDecodeError: Invalid control character at: line 1 column 14 (char 13) >>> json.loads(s['arguments'].replace('\n', '\\n')) {'prompt': 'hi\nbob.'} >>> ``` --------- Co-authored-by: Nuno Campos --- .../langchain/agents/openai_functions_multi_agent/base.py | 2 +- .../langchain/agents/output_parsers/openai_functions.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/langchain/langchain/agents/openai_functions_multi_agent/base.py b/libs/langchain/langchain/agents/openai_functions_multi_agent/base.py index d2594486375..026171b25ee 100644 --- a/libs/langchain/langchain/agents/openai_functions_multi_agent/base.py +++ b/libs/langchain/langchain/agents/openai_functions_multi_agent/base.py @@ -41,7 +41,7 @@ def _parse_ai_message(message: BaseMessage) -> Union[List[AgentAction], AgentFin if function_call: try: - arguments = json.loads(function_call["arguments"]) + arguments = json.loads(function_call["arguments"], strict=False) except JSONDecodeError: raise OutputParserException( f"Could not parse tool input: {function_call} because " diff --git a/libs/langchain/langchain/agents/output_parsers/openai_functions.py b/libs/langchain/langchain/agents/output_parsers/openai_functions.py index 38f604723d4..b0e0436bf7b 100644 --- a/libs/langchain/langchain/agents/output_parsers/openai_functions.py +++ b/libs/langchain/langchain/agents/output_parsers/openai_functions.py @@ -46,7 +46,7 @@ class OpenAIFunctionsAgentOutputParser(AgentOutputParser): _tool_input = {} else: # otherwise it returns a json object - _tool_input = json.loads(function_call["arguments"]) + _tool_input = json.loads(function_call["arguments"], strict=False) except JSONDecodeError: raise OutputParserException( f"Could not parse tool input: {function_call} because "