Skip to content

Tool

Waldiez Tool model.

WaldiezTool

Bases: WaldiezBase

Waldiez Tool.

Attributes:

NameTypeDescription
idstr

The ID of the tool.

typeLiteral['tool']

The type of the "node" in a graph: "tool".

namestr

The name of the tool.

descriptionstr

The description of the tool.

tagslist[str]

The tags of the tool.

requirementslist[str]

The requirements of the tool.

created_atstr

The date and time when the tool was created.

updated_atstr

The date and time when the tool was last updated.

dataWaldiezToolData

The data of the tool. See WaldiezToolData.

content property

content: str

Get the content (source) of the tool.

get_content

get_content() -> str

Get the content of the tool.

Returns:

TypeDescription
str

The content of the tool.

Source code in waldiez/models/tool/tool.py
def get_content(self) -> str:
    """Get the content of the tool.

    Returns
    -------
    str
        The content of the tool.
    """
    if self.is_shared or self.is_interop:
        return self.data.content
    # if custom, only the function content
    return get_function(self.data.content, self.name)

get_imports

get_imports() -> tuple[list[str], list[str]]

Get the tool imports.

Returns:

TypeDescription
tuple[list[str], list[str]]

The builtin and external imports.

Source code in waldiez/models/tool/tool.py
def get_imports(self) -> tuple[list[str], list[str]]:
    """Get the tool imports.

    Returns
    -------
    tuple[list[str], list[str]]
        The builtin and external imports.
    """
    return self._tool_imports

is_interop property

is_interop: bool

Check if the tool is interoperability.

Returns:

TypeDescription
bool

True if the tool is interoperability, False otherwise.

is_shared property

is_shared: bool

Check if the tool is shared.

Returns:

TypeDescription
bool

True if the tool is shared, False otherwise.

load staticmethod

load(
    data_or_path: Union[str, Path, dict[str, Any]],
) -> WaldiezTool

Load a tool from a read-only file.

Parameters:

NameTypeDescriptionDefault
data_or_pathUnion[str, Path, dict[str, Any]]

The path to the read-only file or the loaded data.

required

Returns:

TypeDescription
WaldiezTool

The tool.

Raises:

TypeDescription
FileNotFoundError

If the file is not found.

ValueError

If the JSON is invalid or the data is invalid.

Source code in waldiez/models/tool/tool.py
@staticmethod
def load(data_or_path: Union[str, Path, dict[str, Any]]) -> "WaldiezTool":
    """Load a tool from a read-only file.

    Parameters
    ----------
    data_or_path : Union[str, Path, dict[str, Any]]
        The path to the read-only file or the loaded data.

    Returns
    -------
    WaldiezTool
        The tool.

    Raises
    ------
    FileNotFoundError
        If the file is not found.
    ValueError
        If the JSON is invalid or the data is invalid.
    """
    if isinstance(data_or_path, dict):
        return WaldiezTool.model_validate(data_or_path)
    if not isinstance(data_or_path, Path):
        data_or_path = Path(data_or_path)
    resolved = data_or_path.resolve()
    if not resolved.is_file():
        raise FileNotFoundError(f"File not found: {resolved}")
    with resolved.open("r", encoding="utf-8") as file:
        data_string = file.read()
        # pylint: disable=broad-exception-caught
        try:
            data_dict = json.loads(data_string)
        except BaseException as exc:
            raise ValueError(f"Invalid WaldiezTool/JSON: {exc}") from exc
        return WaldiezTool.model_validate(data_dict)

secrets property

secrets: dict[str, str]

Get the secrets (environment variables) of the tool.

tool_type property

tool_type: WaldiezToolType

Get the tool type.

Returns:

TypeDescription
WaldiezToolType

The type of the tool: [shared, custom, langchain, crewai].

validate_data

validate_data() -> Self

Validate the data.

Returns:

TypeDescription
WaldiezTool

The tool.

Raises:

TypeDescription
ValueError

If the tool name is not in the content. If the tool content is invalid.

Source code in waldiez/models/tool/tool.py
@model_validator(mode="after")
def validate_data(self) -> Self:
    """Validate the data.

    Returns
    -------
    WaldiezTool
        The tool.

    Raises
    ------
    ValueError
        If the tool name is not in the content.
        If the tool content is invalid.
    """
    self._validate_custom_tool()
    self._validate_interop_tool()
    self._tool_imports = gather_code_imports(
        self.data.content, self.is_interop
    )
    # remove the imports from the content
    # we will place them at the top of the file
    all_imports = self._tool_imports[0] + self._tool_imports[1]
    code_lines = self.data.content.splitlines()
    valid_lines = [
        line
        for line in code_lines
        if not any(line.startswith(imp) for imp in all_imports)
    ]
    # remove empty lines at the beginning and end
    # of the content
    while valid_lines and not valid_lines[0].strip():
        valid_lines.pop(0)
    while valid_lines and not valid_lines[-1].strip():
        valid_lines.pop()
    self.data.content = "\n".join(valid_lines)
    return self

Waldiez Tool model.

WaldiezToolData

Bases: WaldiezBase

Waldiez Tool Data.

Attributes:

NameTypeDescription
tool_typeWaldiezToolType

The type of the tool: shared, custom, langchain, crewai.

contentstr

The content (source code) of the tool.

secretsdict[str, str]

The secrets (environment variables) of the tool.

Waldiez Tool types.

WaldiezToolType module-attribute

WaldiezToolType = Literal[
    "shared", "custom", "langchain", "crewai"
]

Possible types of a Waldiez Tool.