From d9670a5945712de7ae8e6435a38c63fbba9ab373 Mon Sep 17 00:00:00 2001 From: Laurentiu Piciu Date: Thu, 5 Oct 2023 21:08:09 +0300 Subject: [PATCH] openai_functions_multi_agent: solved the case when the "arguments" is valid JSON but it does not contain `actions` key (#10543) Description: There are cases when the output from the LLM comes fine (i.e. function_call["arguments"] is a valid JSON object), but it does not contain the key "actions". So I split the validation in 2 steps: loading arguments as JSON and then checking for "actions" in it. --------- Co-authored-by: Bagatur --- .../agents/openai_functions_multi_agent/base.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 0fbafd591b..fae917805e 100644 --- a/libs/langchain/langchain/agents/openai_functions_multi_agent/base.py +++ b/libs/langchain/langchain/agents/openai_functions_multi_agent/base.py @@ -45,12 +45,21 @@ def _parse_ai_message(message: BaseMessage) -> Union[List[AgentAction], AgentFin if function_call: try: - tools = json.loads(function_call["arguments"])["actions"] + arguments = json.loads(function_call["arguments"]) except JSONDecodeError: raise OutputParserException( f"Could not parse tool input: {function_call} because " f"the `arguments` is not valid JSON." ) + + try: + tools = arguments["actions"] + except (TypeError, KeyError): + raise OutputParserException( + f"Could not parse tool input: {function_call} because " + f"the `arguments` JSON does not contain `actions` key." + ) + final_tools: List[AgentAction] = [] for tool_schema in tools: _tool_input = tool_schema["action"]