Skip to content

Commit

Permalink
update tool_call function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lycnkd committed Dec 25, 2024
1 parent 5991b45 commit 99aa194
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions libs/core/langchain_core/messages/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from langchain_core.messages.base import BaseMessage, BaseMessageChunk, merge_content
from langchain_core.utils._merge import merge_dicts, merge_obj
import re


class ToolOutputMixin:
Expand Down Expand Up @@ -209,6 +210,16 @@ 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:
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")


Expand Down

0 comments on commit 99aa194

Please sign in to comment.