From 32f8f39974961b097fceab158f573174e0553450 Mon Sep 17 00:00:00 2001 From: Bagatur <22008038+baskaryan@users.noreply.github.com> Date: Tue, 25 Jun 2024 15:26:35 -0700 Subject: [PATCH] core[patch]: use args_schema doc for tool description (#23503) --- libs/core/langchain_core/tools.py | 2 ++ libs/core/tests/unit_tests/test_tools.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/core/langchain_core/tools.py b/libs/core/langchain_core/tools.py index 86cedd51aa9..cf168e784b2 100644 --- a/libs/core/langchain_core/tools.py +++ b/libs/core/langchain_core/tools.py @@ -827,6 +827,8 @@ class StructuredTool(BaseTool): raise ValueError("Function and/or coroutine must be provided") name = name or source_function.__name__ description_ = description or source_function.__doc__ + if description_ is None and args_schema: + description_ = args_schema.__doc__ if description_ is None: raise ValueError( "Function must have a docstring if description not provided." diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py index cc624f4cfd2..5e7b05dbff3 100644 --- a/libs/core/tests/unit_tests/test_tools.py +++ b/libs/core/tests/unit_tests/test_tools.py @@ -44,6 +44,8 @@ def test_unnamed_decorator() -> None: class _MockSchema(BaseModel): + """Return the arguments directly.""" + arg1: int arg2: bool arg3: Optional[dict] = None @@ -133,7 +135,6 @@ def test_decorator_with_specified_schema() -> None: @tool(args_schema=_MockSchema) def tool_func(arg1: int, arg2: bool, arg3: Optional[dict] = None) -> str: - """Return the arguments directly.""" return f"{arg1} {arg2} {arg3}" assert isinstance(tool_func, BaseTool)