Skip to content

mirascope.core.base.tool

This module defines the base class for tools used in LLM calls.

Usage Documentation

Tools

ToolConfig

Bases: TypedDict

A base class for tool configurations.

BaseTool

Bases: BaseModel, Generic[_ToolSchemaT]

A class for defining tools for LLM calls.

Example:

from mirascope.core import BaseTool
from pydantic import Field


class FormatBook(BaseTool):
    """Returns a nicely formatted book recommendation."""

    title: str = Field(..., description="The title of the book.")
    author: str = Field(..., description="The author of the book.")

    def call(self) -> str:
        return f"{self.title} by {self.author}"

args property

args: dict[str, Any]

The arguments of the tool as a dictionary.

call abstractmethod

call() -> Any

The method to call the tool.

Source code in mirascope/core/base/tool.py
@abstractmethod
def call(self) -> Any:  # noqa: ANN401
    """The method to call the tool."""
    ...

type_from_fn classmethod

type_from_fn(fn: Callable) -> type[_BaseToolT]

Returns this tool type converted from a function.

Parameters:

Name Type Description Default
fn Callable

The function to convert into this tool type.

required
Source code in mirascope/core/base/tool.py
@classmethod
def type_from_fn(cls: type[_BaseToolT], fn: Callable) -> type[_BaseToolT]:
    """Returns this tool type converted from a function.

    Args:
        fn: The function to convert into this tool type.
    """
    return _utils.convert_function_to_base_tool(fn, cls)

type_from_base_model_type classmethod

type_from_base_model_type(
    tool_type: type[BaseModel],
) -> type[_BaseToolT]

Returns this tool type converted from a given base tool type.

Parameters:

Name Type Description Default
tool_type type[BaseModel]

The tool type to convert into this tool type. This can be a custom BaseTool or BaseModel definition.

required
Source code in mirascope/core/base/tool.py
@classmethod
def type_from_base_model_type(
    cls: type[_BaseToolT], tool_type: type[BaseModel]
) -> type[_BaseToolT]:
    """Returns this tool type converted from a given base tool type.

    Args:
        tool_type: The tool type to convert into this tool type. This can be a
            custom `BaseTool` or `BaseModel` definition.
    """
    return _utils.convert_base_model_to_base_tool(tool_type, cls)

type_from_base_type classmethod

type_from_base_type(
    base_type: type[BaseType],
) -> type[_BaseToolT]

Returns this tool type converted from a base type.

Parameters:

Name Type Description Default
base_type type[BaseType]

The base type (e.g. int) to convert into this tool type.

required
Source code in mirascope/core/base/tool.py
@classmethod
def type_from_base_type(
    cls: type[_BaseToolT], base_type: type[_utils.BaseType]
) -> type[_BaseToolT]:
    """Returns this tool type converted from a base type.

    Args:
        base_type: The base type (e.g. `int`) to convert into this tool type.
    """
    return _utils.convert_base_type_to_base_tool(base_type, cls)

model_json_schema classmethod

model_json_schema(
    by_alias: bool = True,
    ref_template: str = DEFAULT_REF_TEMPLATE,
    schema_generator: type[
        GenerateJsonSchema
    ] = GenerateJsonSchemaNoTitles,
    mode: JsonSchemaMode = "validation",
) -> dict[str, Any]

Returns the generated JSON schema for the class.

Source code in mirascope/core/base/tool.py
@classmethod
def model_json_schema(
    cls,
    by_alias: bool = True,
    ref_template: str = DEFAULT_REF_TEMPLATE,
    schema_generator: type[GenerateJsonSchema] = GenerateJsonSchemaNoTitles,
    mode: JsonSchemaMode = "validation",
) -> dict[str, Any]:
    """Returns the generated JSON schema for the class."""
    cls.warn_for_unsupported_configurations()
    return super().model_json_schema(
        by_alias=by_alias,
        ref_template=ref_template,
        schema_generator=schema_generator,
        mode=mode,
    )

warn_for_unsupported_configurations classmethod

warn_for_unsupported_configurations() -> None

Warns when a specific provider does not support provided config options.

Source code in mirascope/core/base/tool.py
@classmethod
def warn_for_unsupported_configurations(cls) -> None:
    """Warns when a specific provider does not support provided config options."""
    unsupported_tool_keys = _utils.get_unsupported_tool_config_keys(
        cls.tool_config, cls.__tool_config_type__
    )
    if unsupported_tool_keys:
        warnings.warn(
            f"{cls.__provider__} does not support the following tool "
            f"configurations, so they will be ignored: {unsupported_tool_keys}",
            UserWarning,
        )

    if "strict" in cls.model_config and cls.__provider__ not in ["openai", "azure"]:
        warnings.warn(
            f"{cls.__provider__} does not support strict structured outputs, but "
            "you have configured `strict=True` in your `ResponseModelConfigDict`. "
            "Ignoring `strict` as this feature is only supported by OpenAI.",
            UserWarning,
        )