Skip to content

ToolRegistry

Base classes

Name Children Inherits
BaseRegistry
llmling.core.baseregistry
Base class for registries providing item storage and change notifications.

â‹” Inheritance diagram

graph TD
  812480784["registry.ToolRegistry"]
  1668955754["baseregistry.BaseRegistry"]
  1799060090["abc.MutableMapping"]
  4016029557["abc.Mapping"]
  2852422536["abc.Collection"]
  4051874921["abc.Sized"]
  2751680898["builtins.object"]
  2021119437["abc.Iterable"]
  1031828782["abc.Container"]
  925645236["abc.ABC"]
  3481597255["typing.Generic"]
  1668955754 --> 812480784
  1799060090 --> 1668955754
  4016029557 --> 1799060090
  2852422536 --> 4016029557
  4051874921 --> 2852422536
  2751680898 --> 4051874921
  2021119437 --> 2852422536
  2751680898 --> 2021119437
  1031828782 --> 2852422536
  2751680898 --> 1031828782
  925645236 --> 1668955754
  2751680898 --> 925645236
  3481597255 --> 1668955754
  2751680898 --> 3481597255

🛈 DocStrings

Bases: BaseRegistry[str, LLMCallableTool]

Registry for functions that can be called by LLMs.

Source code in src/llmling/tools/registry.py
class ToolRegistry(BaseRegistry[str, LLMCallableTool]):
    """Registry for functions that can be called by LLMs."""

    @property
    def _error_class(self) -> type[ToolError]:
        """Error class to use for this registry."""
        return ToolError

    def _validate_item(self, item: Any) -> LLMCallableTool:  # noqa: PLR0911
        """Validate and transform item into a LLMCallableTool."""
        from llmling.config.models import ToolConfig

        match item:
            case LLMCallableTool():
                return item
            case ToolConfig():  # Handle Pydantic models
                try:
                    obj_class = importing.import_class(item.import_path)

                    # Check for crewai tools
                    if importlib.util.find_spec("crewai"):
                        from crewai.tools import BaseTool as CrewAiBaseTool

                        if issubclass(obj_class, CrewAiBaseTool):
                            return LLMCallableTool.from_crewai_tool(
                                obj_class(
                                    name=obj_class.__name__,
                                    description=obj_class.description,
                                ),
                                name_override=item.name,
                                description_override=item.description,
                            )

                    # Check for langchain tools
                    if importlib.util.find_spec("langchain_core"):
                        from langchain_core.tools import BaseTool as LangChainBaseTool

                        if issubclass(obj_class, LangChainBaseTool):
                            return LLMCallableTool.from_langchain_tool(
                                obj_class(),
                                name_override=item.name,
                                description_override=item.description,
                            )
                    # Either it wasn't a class or wasn't a crewai tool - treat as callable
                    return LLMCallableTool.from_callable(
                        item.import_path,
                        name_override=item.name,
                        description_override=item.description,
                    )
                except Exception:  # noqa: BLE001
                    fn = importing.import_callable(item.import_path)
                    return LLMCallableTool.from_callable(fn)
            case dict() if "import_path" in item:  # Config dict
                return LLMCallableTool.from_callable(
                    item["import_path"],
                    name_override=item.get("name"),
                    description_override=item.get("description"),
                )
            case str():  # Import path
                return LLMCallableTool.from_callable(item)
            # Add new support for callables
            case _ if callable(item):
                return LLMCallableTool.from_callable(item)
            case _:
                # Check for crewai tool instances
                if importlib.util.find_spec("crewai"):
                    from crewai.tools import BaseTool as CrewAiBaseTool

                    if isinstance(item, CrewAiBaseTool):
                        return LLMCallableTool.from_crewai_tool(item)

                # Check for langchain tool instances
                if importlib.util.find_spec("langchain_core"):
                    from langchain_core.tools import BaseTool as LangChainBaseTool

                    if isinstance(item, LangChainBaseTool):
                        return LLMCallableTool.from_langchain_tool(item)

                msg = f"Invalid tool type: {type(item)}"
                raise ToolError(msg)

    def add_container(
        self,
        obj: type | ModuleType | Any,
        *,
        prefix: str = "",
        include_imported: bool = False,
    ) -> None:
        """Register all public callable members from a Python object.

        Args:
            obj: Any Python object to inspect (module, class, instance)
            prefix: Optional prefix for registered function names
            include_imported: Whether to include imported/inherited callables
        """
        for name, func in importing.get_pyobject_members(
            obj,
            include_imported=include_imported,
        ):
            self.register(f"{prefix}{name}", func)
            logger.debug("Registered callable %s as %s", name, f"{prefix}{name}")

    def get_schemas(self) -> list[schemez.OpenAIFunctionTool]:
        """Get schemas for all registered functions.

        Returns:
            List of OpenAI function schemas
        """
        return [tool.get_schema() for tool in self._items.values()]

    async def execute(self, _name: str, **params: Any) -> Any:
        """Execute a registered function.

        Args:
            _name: Name of the function to execute
            **params: Parameters to pass to the function

        Returns:
            Function result

        Raises:
            ToolNotFoundError: If function not found
            ToolError: If execution fails
        """
        try:
            tool = self.get(_name)
        except KeyError as exc:
            msg = f"Function {_name} not found"
            raise ToolNotFoundError(msg) from exc

        # Let the original exception propagate
        return await tool.execute(**params)

add_container

add_container(
    obj: type | ModuleType | Any, *, prefix: str = "", include_imported: bool = False
) -> None

Register all public callable members from a Python object.

Parameters:

Name Type Description Default
obj type | ModuleType | Any

Any Python object to inspect (module, class, instance)

required
prefix str

Optional prefix for registered function names

''
include_imported bool

Whether to include imported/inherited callables

False
Source code in src/llmling/tools/registry.py
def add_container(
    self,
    obj: type | ModuleType | Any,
    *,
    prefix: str = "",
    include_imported: bool = False,
) -> None:
    """Register all public callable members from a Python object.

    Args:
        obj: Any Python object to inspect (module, class, instance)
        prefix: Optional prefix for registered function names
        include_imported: Whether to include imported/inherited callables
    """
    for name, func in importing.get_pyobject_members(
        obj,
        include_imported=include_imported,
    ):
        self.register(f"{prefix}{name}", func)
        logger.debug("Registered callable %s as %s", name, f"{prefix}{name}")

execute async

execute(_name: str, **params: Any) -> Any

Execute a registered function.

Parameters:

Name Type Description Default
_name str

Name of the function to execute

required
**params Any

Parameters to pass to the function

{}

Returns:

Type Description
Any

Function result

Raises:

Type Description
ToolNotFoundError

If function not found

ToolError

If execution fails

Source code in src/llmling/tools/registry.py
async def execute(self, _name: str, **params: Any) -> Any:
    """Execute a registered function.

    Args:
        _name: Name of the function to execute
        **params: Parameters to pass to the function

    Returns:
        Function result

    Raises:
        ToolNotFoundError: If function not found
        ToolError: If execution fails
    """
    try:
        tool = self.get(_name)
    except KeyError as exc:
        msg = f"Function {_name} not found"
        raise ToolNotFoundError(msg) from exc

    # Let the original exception propagate
    return await tool.execute(**params)

get_schemas

get_schemas() -> list[OpenAIFunctionTool]

Get schemas for all registered functions.

Returns:

Type Description
list[OpenAIFunctionTool]

List of OpenAI function schemas

Source code in src/llmling/tools/registry.py
def get_schemas(self) -> list[schemez.OpenAIFunctionTool]:
    """Get schemas for all registered functions.

    Returns:
        List of OpenAI function schemas
    """
    return [tool.get_schema() for tool in self._items.values()]

:fa-brands-github: Show source on GitHub