Skip to content

The agent engineering platform.

fix(core): handle tool descriptions for infer_schema=False (#39573)

Hunter Lovell624fd031c2d8
diff --git a/libs/core/langchain_core/tools/convert.py b/libs/core/langchain_core/tools/convert.py
index d353fc5250..a4e7e510d3 100644
--- a/libs/core/langchain_core/tools/convert.py
+++ b/libs/core/langchain_core/tools/convert.py
@@ -329,16 +329,17 @@ def tool(
)
# If someone doesn't want a schema applied, we must treat it as
# a simple string->string function
- if dec_func.__doc__ is None:
+ tool_description = tool_description or dec_func.__doc__
+ if tool_description is None:
msg = (
- "Function must have a docstring if "
- "description not provided and infer_schema is False."
+ "Function must have either a docstring or description "
+ "when infer_schema is False."
)
raise ValueError(msg)
return Tool(
name=tool_name,
func=func,
- description=f"{tool_name} tool",
+ description=tool_description,
return_direct=return_direct,
coroutine=coroutine,
response_format=response_format,
diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py
index f23fd9052a..28ec11af51 100644
--- a/libs/core/tests/unit_tests/test_tools.py
+++ b/libs/core/tests/unit_tests/test_tools.py
@@ -365,9 +365,34 @@ def test_structured_single_str_decorator_no_infer_schema() -> None:
assert isinstance(unstructured_tool_input, BaseTool)
assert unstructured_tool_input.args_schema is None
+ assert unstructured_tool_input.description == "Return the arguments directly."
assert unstructured_tool_input.run("foo") == "foo"
+def test_simple_tool_decorator_no_infer_schema_uses_explicit_description() -> None:
+ """Test that a simple tool preserves an explicit description."""
+
+ @tool(infer_schema=False, description="Echo the supplied input.")
+ def echo(tool_input: str) -> str:
+ return tool_input
+
+ assert echo.description == "Echo the supplied input."
+
+
+def test_simple_tool_decorator_no_infer_schema_requires_description_or_docstring() -> (
+ None
+):
+ """Test that a simple tool requires an authored description."""
+ with pytest.raises(
+ ValueError,
+ match="Function must have either a docstring or description",
+ ):
+
+ @tool(infer_schema=False)
+ def echo(tool_input: str) -> str:
+ return tool_input
+
+
def test_structured_tool_types_parsed() -> None:
"""Test the non-primitive types are correctly passed to structured tools."""