diff --git a/libs/core/langchain_core/tools/structured.py b/libs/core/langchain_core/tools/structured.py index 248678d3801..a30defdd77b 100644 --- a/libs/core/langchain_core/tools/structured.py +++ b/libs/core/langchain_core/tools/structured.py @@ -40,7 +40,7 @@ class StructuredTool(BaseTool): ..., description="The tool schema." ) """The input arguments' schema.""" - func: Optional[Callable[..., Any]] + func: Optional[Callable[..., Any]] = None """The function to run when the tool is called.""" coroutine: Optional[Callable[..., Awaitable[Any]]] = None """The asynchronous version of the function.""" @@ -98,8 +98,8 @@ class StructuredTool(BaseTool): kwargs[config_param] = config return await self.coroutine(*args, **kwargs) - # NOTE: this code is unreachable since _arun is only called if coroutine is not - # None. + # If self.coroutine is None, then this will delegate to the default + # implementation which is expected to delegate to _run on a separate thread. return await super()._arun( *args, config=config, run_manager=run_manager, **kwargs ) diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py index c1d07762cf9..e2dabc1c125 100644 --- a/libs/core/tests/unit_tests/test_tools.py +++ b/libs/core/tests/unit_tests/test_tools.py @@ -54,7 +54,6 @@ from langchain_core.tools.base import ( ) from langchain_core.utils.function_calling import convert_to_openai_function from langchain_core.utils.pydantic import PYDANTIC_MAJOR_VERSION, _create_subset_model -from langchain_core.utils.pydantic import TypeBaseModel as TypeBaseModel from tests.unit_tests.fake.callbacks import FakeCallbackHandler from tests.unit_tests.pydantic_utils import _schema @@ -1978,3 +1977,19 @@ def test_imports() -> None: ] for module_name in expected_all: assert hasattr(tools, module_name) and getattr(tools, module_name) is not None + + +def test_structured_tool_direct_init() -> None: + def foo(bar: str) -> str: + return bar + + async def asyncFoo(bar: str) -> str: + return bar + + class fooSchema(BaseModel): + bar: str = Field(..., description="The bar") + + tool = StructuredTool(name="foo", args_schema=fooSchema, coroutine=asyncFoo) + + with pytest.raises(NotImplementedError): + assert tool.invoke("hello") == "hello"