Skip to content

core[bug]: Fixing Missing Docstring Bug if no Docstring is provided in BaseModel class #31608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion libs/core/langchain_core/tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
model_validator,
validate_arguments,
)
from pydantic._internal._model_construction import ModelMetaclass
from pydantic.v1 import BaseModel as BaseModelV1
from pydantic.v1 import ValidationError as ValidationErrorV1
from pydantic.v1 import validate_arguments as validate_arguments_v1
Expand Down Expand Up @@ -150,7 +151,10 @@ def _infer_arg_descriptions(
fn, annotations, error_on_invalid_docstring=error_on_invalid_docstring
)
else:
description = inspect.getdoc(fn) or ""
if isinstance(fn, ModelMetaclass):
description = fn.__doc__ or ""
else:
description = inspect.getdoc(fn) or ""
arg_descriptions = {}
if parse_docstring:
_validate_docstring_args_against_annotations(arg_descriptions, annotations)
Expand Down
6 changes: 5 additions & 1 deletion libs/core/langchain_core/tools/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)

from pydantic import Field, SkipValidation
from pydantic._internal._model_construction import ModelMetaclass
from typing_extensions import override

from langchain_core.callbacks import (
Expand Down Expand Up @@ -197,7 +198,10 @@ def add(a: int, b: int) -> int:
description_ = source_function.__doc__ or None
if description_ is None and args_schema:
if isinstance(args_schema, type) and is_basemodel_subclass(args_schema):
description_ = args_schema.__doc__ or None
if isinstance(source_function, ModelMetaclass):
description_ = args_schema.__doc__
else:
description_ = args_schema.__doc__ or None
elif isinstance(args_schema, dict):
description_ = args_schema.get("description")
else:
Expand Down
6 changes: 6 additions & 0 deletions libs/core/tests/unit_tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,12 @@ def test_missing_docstring() -> None:
def search_api(query: str) -> str:
return "API result"

@tool
class MyTool(BaseModel):
foo: str

assert MyTool.description == "" # type: ignore[attr-defined]


def test_create_tool_positional_args() -> None:
"""Test that positional arguments are allowed."""
Expand Down
Loading