Skip to content

dspy.Tool

dspy.Tool(func: Callable, name: Optional[str] = None, desc: Optional[str] = None, args: Optional[dict[str, Any]] = None, arg_types: Optional[dict[str, Any]] = None, arg_desc: Optional[dict[str, str]] = None)

Tool class.

This class is used to simplify the creation of tools for tool calling (function calling) in LLMs. Only supports functions for now.

Initialize the Tool class.

Parameters:

Name Type Description Default
func Callable

The actual function that is being wrapped by the tool.

required
name Optional[str]

The name of the tool. Defaults to None.

None
desc Optional[str]

The description of the tool. Defaults to None.

None
args Optional[dict[str, Any]]

The args of the tool, represented as a dictionary from arg name to arg's json schema. Defaults to None.

None
arg_types Optional[dict[str, Any]]

The argument types of the tool, represented as a dictionary from arg name to the type of the argument. Defaults to None.

None
arg_desc Optional[dict[str, str]]

Descriptions for each arg, represented as a dictionary from arg name to description string. Defaults to None.

None
Source code in dspy/primitives/tool.py
def __init__(
    self,
    func: Callable,
    name: Optional[str] = None,
    desc: Optional[str] = None,
    args: Optional[dict[str, Any]] = None,
    arg_types: Optional[dict[str, Any]] = None,
    arg_desc: Optional[dict[str, str]] = None,
):
    """Initialize the Tool class.

    Args:
        func (Callable): The actual function that is being wrapped by the tool.
        name (Optional[str], optional): The name of the tool. Defaults to None.
        desc (Optional[str], optional): The description of the tool. Defaults to None.
        args (Optional[dict[str, Any]], optional): The args of the tool, represented as a dictionary
            from arg name to arg's json schema. Defaults to None.
        arg_types (Optional[dict[str, Any]], optional): The argument types of the tool, represented as a dictionary
            from arg name to the type of the argument. Defaults to None.
        arg_desc (Optional[dict[str, str]], optional): Descriptions for each arg, represented as a
            dictionary from arg name to description string. Defaults to None.
    """
    self.func = func
    self.name = name
    self.desc = desc
    self.args = args
    self.arg_types = arg_types
    self.arg_desc = arg_desc

    self._parse_function(func, arg_desc)

Functions

__call__(**kwargs)

Source code in dspy/primitives/tool.py
@with_callbacks
def __call__(self, **kwargs):
    for k, v in kwargs.items():
        if k not in self.args:
            raise ValueError(f"Arg {k} is not in the tool's args.")
        try:
            instance = v.model_dump() if hasattr(v, "model_dump") else v
            if not self.args[k] == "Any":
                validate(instance=instance, schema=self.args[k])
        except ValidationError as e:
            raise ValueError(f"Arg {k} is invalid: {e.message}")
    return self.func(**kwargs)