The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
[Patch] Dedent docstring (#20959)
William FH5c63ac3dd7f9
diff --git a/libs/core/langchain_core/tools.py b/libs/core/langchain_core/tools.pyindex 38119ceb70..7ad7da84f3 100644--- a/libs/core/langchain_core/tools.py+++ b/libs/core/langchain_core/tools.py@@ -21,6 +21,7 @@ from __future__ import annotationsimport asyncioimport inspect+import textwrapimport uuidimport warningsfrom abc import ABC, abstractmethod@@ -825,16 +826,19 @@ class StructuredTool(BaseTool):else: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:+ description_ = description or source_function.__doc__+ if description_ is None:raise ValueError("Function must have a docstring if description not provided.")+ if description is None:+ # Only apply if using the function's docstring+ description_ = textwrap.dedent(description_).strip()# Description example:# search_api(query: str) - Searches the API for the query.sig = signature(source_function)- description = f"{name}{sig} - {description.strip()}"+ description_ = f"{name}{sig} - {description_.strip()}"_args_schema = args_schemaif _args_schema is None and infer_schema:# schema name is appended within function@@ -844,7 +848,7 @@ class StructuredTool(BaseTool):func=func,coroutine=coroutine,args_schema=_args_schema, # type: ignore[arg-type]- description=description,+ description=description_,return_direct=return_direct,**kwargs,)diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.pyindex a424d6ecf6..f17e77fa01 100644--- a/libs/core/tests/unit_tests/test_tools.py+++ b/libs/core/tests/unit_tests/test_tools.py@@ -3,6 +3,7 @@import asyncioimport jsonimport sys+import textwrapfrom datetime import datetimefrom enum import Enumfrom functools import partial@@ -333,7 +334,7 @@ def test_structured_tool_from_function_docstring() -> None:prefix = "foo(bar: int, baz: str) -> str - "assert foo.__doc__ is not None- assert structured_tool.description == prefix + foo.__doc__.strip()+ assert structured_tool.description == prefix + textwrap.dedent(foo.__doc__.strip())def test_structured_tool_from_function_docstring_complex_args() -> None:@@ -366,7 +367,7 @@ def test_structured_tool_from_function_docstring_complex_args() -> None:prefix = "foo(bar: int, baz: List[str]) -> str - "assert foo.__doc__ is not None- assert structured_tool.description == prefix + foo.__doc__.strip()+ assert structured_tool.description == prefix + textwrap.dedent(foo.__doc__).strip()def test_structured_tool_lambda_multi_args_schema() -> None:@@ -701,7 +702,7 @@ def test_structured_tool_from_function() -> None:prefix = "foo(bar: int, baz: str) -> str - "assert foo.__doc__ is not None- assert structured_tool.description == prefix + foo.__doc__.strip()+ assert structured_tool.description == prefix + textwrap.dedent(foo.__doc__.strip())def test_validation_error_handling_bool() -> None: