Home / Class/ BaseFunctionTool Class — anthropic-sdk-python Architecture

BaseFunctionTool Class — anthropic-sdk-python Architecture

Architecture documentation for the BaseFunctionTool class in _beta_functions.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  001b1f9e_045c_b009_1c1f_58964c925505["BaseFunctionTool"]
  2f4a4b61_2541_5c7b_c70e_93f4b752f987["_beta_functions.py"]
  001b1f9e_045c_b009_1c1f_58964c925505 -->|defined in| 2f4a4b61_2541_5c7b_c70e_93f4b752f987
  d9fcf820_8cac_9db2_dcbc_d9b37c4e6ce0["__init__()"]
  001b1f9e_045c_b009_1c1f_58964c925505 -->|method| d9fcf820_8cac_9db2_dcbc_d9b37c4e6ce0
  cdf1b1ea_3618_e41e_01cd_c1942073a16d["__call__()"]
  001b1f9e_045c_b009_1c1f_58964c925505 -->|method| cdf1b1ea_3618_e41e_01cd_c1942073a16d
  7d765acb_aa92_f159_4509_90b5193d17c1["to_dict()"]
  001b1f9e_045c_b009_1c1f_58964c925505 -->|method| 7d765acb_aa92_f159_4509_90b5193d17c1
  245f6d38_1627_409d_0469_d677924794b6["_parsed_docstring()"]
  001b1f9e_045c_b009_1c1f_58964c925505 -->|method| 245f6d38_1627_409d_0469_d677924794b6
  e4ad5562_f752_5b4b_a011_4481b765ea4c["_get_description_from_docstring()"]
  001b1f9e_045c_b009_1c1f_58964c925505 -->|method| e4ad5562_f752_5b4b_a011_4481b765ea4c
  3bf0e655_8383_b5ca_7c90_6b1d977bf9c8["_create_schema_from_function()"]
  001b1f9e_045c_b009_1c1f_58964c925505 -->|method| 3bf0e655_8383_b5ca_7c90_6b1d977bf9c8
  422c3bcb_219b_1831_4b80_2e28efe670ff["_adapter()"]
  001b1f9e_045c_b009_1c1f_58964c925505 -->|method| 422c3bcb_219b_1831_4b80_2e28efe670ff

Relationship Graph

Source Code

src/anthropic/lib/tools/_beta_functions.py lines 63–174

class BaseFunctionTool(Generic[CallableT]):
    func: CallableT
    """The function this tool is wrapping"""

    name: str
    """The name of the tool that will be sent to the API"""

    description: str

    input_schema: InputSchema

    def __init__(
        self,
        func: CallableT,
        *,
        name: str | None = None,
        description: str | None = None,
        input_schema: InputSchema | type[BaseModel] | None = None,
        defer_loading: bool | None = None,
    ) -> None:
        if _compat.PYDANTIC_V1:
            raise RuntimeError("Tool functions are only supported with Pydantic v2")

        self.func = func
        self._func_with_validate = pydantic.validate_call(func)
        self.name = name or func.__name__
        self._defer_loading = defer_loading

        self.description = description or self._get_description_from_docstring()

        if input_schema is not None:
            if isinstance(input_schema, type):
                self.input_schema: InputSchema = input_schema.model_json_schema()
            else:
                self.input_schema = input_schema
        else:
            self.input_schema = self._create_schema_from_function()

    @property
    def __call__(self) -> CallableT:
        return self.func

    def to_dict(self) -> BetaToolParam:
        defn: BetaToolParam = {
            "name": self.name,
            "description": self.description,
            "input_schema": self.input_schema,
        }
        if self._defer_loading is not None:
            defn["defer_loading"] = self._defer_loading
        return defn

    @cached_property
    def _parsed_docstring(self) -> docstring_parser.Docstring:
        return docstring_parser.parse(self.func.__doc__ or "")

    def _get_description_from_docstring(self) -> str:
        """Extract description from parsed docstring."""
        if self._parsed_docstring.short_description:
            description = self._parsed_docstring.short_description
            if self._parsed_docstring.long_description:
                description += f"\n\n{self._parsed_docstring.long_description}"
            return description
        return ""

    def _create_schema_from_function(self) -> InputSchema:
        """Create JSON schema from function signature using pydantic."""

        from pydantic_core import CoreSchema
        from pydantic.json_schema import JsonSchemaValue, GenerateJsonSchema
        from pydantic_core.core_schema import ArgumentsParameter

        class CustomGenerateJsonSchema(GenerateJsonSchema):
            def __init__(self, *, func: Callable[..., Any], parsed_docstring: Any) -> None:
                super().__init__()
                self._func = func
                self._parsed_docstring = parsed_docstring

            def __call__(self, *_args: Any, **_kwds: Any) -> "CustomGenerateJsonSchema":  # noqa: ARG002
                return self

Frequently Asked Questions

What is the BaseFunctionTool class?
BaseFunctionTool is a class in the anthropic-sdk-python codebase, defined in src/anthropic/lib/tools/_beta_functions.py.
Where is BaseFunctionTool defined?
BaseFunctionTool is defined in src/anthropic/lib/tools/_beta_functions.py at line 63.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free