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 <erick@langchain.dev>
This commit is contained in:
Shawn Lee 2024-11-08 05:47:19 +08:00 committed by GitHub
parent 0588bab33e
commit 6f368e9eab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,7 @@ from __future__ import annotations
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)