From f8d8830134bf07729e8fdda591631e2f3ea07e0b Mon Sep 17 00:00:00 2001 From: root <12112723@mail.sustech.edu.cn> Date: Wed, 25 Dec 2024 10:04:39 +0000 Subject: [PATCH] change format --- libs/core/langchain_core/messages/tool.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/libs/core/langchain_core/messages/tool.py b/libs/core/langchain_core/messages/tool.py index 25e6d368d8bd8..651c16026d0b6 100644 --- a/libs/core/langchain_core/messages/tool.py +++ b/libs/core/langchain_core/messages/tool.py @@ -210,17 +210,18 @@ class ToolCall(TypedDict): def tool_call(*, name: str, args: dict[str, Any], id: Optional[str]) -> ToolCall: - if isinstance(args, str): - try: - # Extract JSON-like dictionary from string using regex - match = re.search(r'\{.*\}', args) - if match: + try: + return ToolCall(name=name, args=args, id=id, type="tool_call") + except: + # Attempt to extract JSON-like dictionary from string using regex + match = re.search(r'\{.*\}', args) + if match: + try: args = json.loads(match.group()) - else: - raise ValueError("No valid JSON object found in args string") - except json.JSONDecodeError: - raise ValueError("Invalid JSON string for args") - return ToolCall(name=name, args=args, id=id, type="tool_call") + except json.JSONDecodeError: + pass + return ToolCall(name=name, args=args, id=id, type="tool_call") + class ToolCallChunk(TypedDict):