From 6f368e9eab0a22c59980d0c09c2bb8a109fff926 Mon Sep 17 00:00:00 2001 From: Shawn Lee Date: Fri, 8 Nov 2024 05:47:19 +0800 Subject: [PATCH] community: handle chatdeepinfra jsondecode error (#27603) Fixes #27602 Added error handling to return empty dict if args is empty string or None. Co-authored-by: Erick Friis --- libs/community/langchain_community/chat_models/deepinfra.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libs/community/langchain_community/chat_models/deepinfra.py b/libs/community/langchain_community/chat_models/deepinfra.py index 71389cc2c901d..bb5309a6c4319 100644 --- a/libs/community/langchain_community/chat_models/deepinfra.py +++ b/libs/community/langchain_community/chat_models/deepinfra.py @@ -4,6 +4,7 @@ import json import logging +from json import JSONDecodeError from typing import ( Any, AsyncIterator, @@ -96,7 +97,10 @@ def _parse_tool_calling(tool_call: dict) -> ToolCall: """ name = tool_call["function"].get("name", "") - args = json.loads(tool_call["function"]["arguments"]) + try: + args = json.loads(tool_call["function"]["arguments"]) + except (JSONDecodeError, TypeError): + args = {} id = tool_call.get("id") return create_tool_call(name=name, args=args, id=id)