Skip to content

Commit 96bf826

Browse files
keenborder786mdrxy
andauthored
fix: fixing missing Docstring Bug if no Docstring is provided in BaseModel class (#31608)
- **Description:** Ensure that the tool description is an empty string when creating a Structured Tool from a Pydantic class in case no description is provided - **Issue:** Fixes #31606 --------- Co-authored-by: Mason Daugherty <[email protected]>
1 parent 15103b0 commit 96bf826

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

libs/core/langchain_core/tools/structured.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,14 @@ def add(a: int, b: int) -> int:
197197
description_ = source_function.__doc__ or None
198198
if description_ is None and args_schema:
199199
if isinstance(args_schema, type) and is_basemodel_subclass(args_schema):
200-
description_ = args_schema.__doc__ or None
200+
description_ = args_schema.__doc__
201+
if (
202+
description_
203+
and "A base class for creating Pydantic models" in description_
204+
):
205+
description_ = ""
206+
elif not description_:
207+
description_ = None
201208
elif isinstance(args_schema, dict):
202209
description_ = args_schema.get("description")
203210
else:

libs/core/tests/unit_tests/test_tools.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,12 @@ def test_missing_docstring() -> None:
701701
def search_api(query: str) -> str:
702702
return "API result"
703703

704+
@tool
705+
class MyTool(BaseModel):
706+
foo: str
707+
708+
assert MyTool.description == "" # type: ignore[attr-defined]
709+
704710

705711
def test_create_tool_positional_args() -> None:
706712
"""Test that positional arguments are allowed."""

0 commit comments

Comments
 (0)