Skip to content

WaldiezRunner

Run a waldiez flow. The flow is first converted to an autogen flow with agents, chats and skills. We then chown to temporary directory, call the flow's main() and return the results. Before running the flow, any additional environment variables specified in the waldiez file are set.

WaldiezRunner(waldiez: Waldiez, file_path: Optional[Union[str, Path]] = None)

Waldiez runner class.

Source code in waldiez/runner.py
def __init__(
    self, waldiez: Waldiez, file_path: Optional[Union[str, Path]] = None
) -> None:
    """Initialize the Waldiez manager."""
    self._waldiez = waldiez
    self._running = False
    self._file_path = file_path
    self._exporter = WaldiezExporter(waldiez)
    self._called_install_requirements = False

a_install_requirements() -> None async

Install the requirements for the flow asynchronously.

Source code in waldiez/runner.py
async def a_install_requirements(self) -> None:
    """Install the requirements for the flow asynchronously."""
    self._called_install_requirements = True
    printer = get_printer()
    extra_requirements = self.gather_requirements()
    if extra_requirements:
        await a_install_requirements(extra_requirements, printer)
        refresh_environment()

a_run(output_path: Optional[Union[str, Path]] = None, uploads_root: Optional[Union[str, Path]] = None) -> Union[ChatResult, List[ChatResult]] async

Run the Waldiez workflow asynchronously.

Parameters:

NameTypeDescriptionDefault
output_pathOptional[Union[str, Path]]

The output path, by default None.

None
uploads_rootOptional[Union[str, Path]]

The uploads root, to get user-uploaded files, by default None.

None

Returns:

TypeDescription
Union[ChatResult, List[ChatResult]]

The result(s) of the chat(s).

Raises:

TypeDescription
RuntimeError

If the workflow is already running.

Source code in waldiez/runner.py
async def a_run(
    self,
    output_path: Optional[Union[str, Path]] = None,
    uploads_root: Optional[Union[str, Path]] = None,
) -> Union["ChatResult", List["ChatResult"]]:
    """Run the Waldiez workflow asynchronously.

    Parameters
    ----------
    output_path : Optional[Union[str, Path]], optional
        The output path, by default None.
    uploads_root : Optional[Union[str, Path]], optional
        The uploads root, to get user-uploaded files, by default None.

    Returns
    -------
    Union[ChatResult, List[ChatResult]]
        The result(s) of the chat(s).

    Raises
    ------
    RuntimeError
        If the workflow is already running.
    """
    if self._running is True:
        raise RuntimeError("Workflow already running")
    self._running = True
    file_path = output_path or self._file_path
    try:
        return await self._a_run(file_path, uploads_root)
    finally:
        self._running = False

gather_requirements() -> Set[str]

Gather extra requirements to install before running the flow.

Returns:

TypeDescription
Set[str]

The extra requirements.

Source code in waldiez/runner.py
def gather_requirements(self) -> Set[str]:
    """Gather extra requirements to install before running the flow.

    Returns
    -------
    Set[str]
        The extra requirements.
    """
    extra_requirements = set(
        req for req in self.waldiez.requirements if req not in sys.modules
    )
    if self.waldiez.has_captain_agents:
        check_pysqlite3()
    return extra_requirements

install_requirements() -> None

Install the requirements for the flow.

Source code in waldiez/runner.py
def install_requirements(self) -> None:
    """Install the requirements for the flow."""
    self._called_install_requirements = True
    printer = get_printer()
    extra_requirements = self.gather_requirements()
    if extra_requirements:
        install_requirements(extra_requirements, printer)
        refresh_environment()

load(waldiez_file: Union[str, Path], name: Optional[str] = None, description: Optional[str] = None, tags: Optional[List[str]] = None, requirements: Optional[List[str]] = None) -> WaldiezRunner classmethod

Create a WaldiezRunner instance from a file.

Parameters:

NameTypeDescriptionDefault
waldiez_fileUnion[str, Path]

The file path.

required
nameOptional[str]

The name of the Waldiez, by default None.

None
descriptionOptional[str]

The description of the Waldiez, by default None.

None
tagsOptional[List[str]]

The tags of the Waldiez, by default None.

None
requirementsOptional[List[str]]

The requirements of the Waldiez, by default None.

None

Returns:

TypeDescription
WaldiezRunner

The Waldiez runner instance.

Raises:

TypeDescription
FileNotFoundError

If the file is not found.

RuntimeError

If the file is not a valid Waldiez file.

Source code in waldiez/runner.py
@classmethod
def load(
    cls,
    waldiez_file: Union[str, Path],
    name: Optional[str] = None,
    description: Optional[str] = None,
    tags: Optional[List[str]] = None,
    requirements: Optional[List[str]] = None,
) -> "WaldiezRunner":
    """Create a WaldiezRunner instance from a file.

    Parameters
    ----------
    waldiez_file : Union[str, Path]
        The file path.
    name : Optional[str], optional
        The name of the Waldiez, by default None.
    description : Optional[str], optional
        The description of the Waldiez, by default None.
    tags : Optional[List[str]], optional
        The tags of the Waldiez, by default None.
    requirements : Optional[List[str]], optional
        The requirements of the Waldiez, by default None.

    Returns
    -------
    WaldiezRunner
        The Waldiez runner instance.

    Raises
    ------
    FileNotFoundError
        If the file is not found.
    RuntimeError
        If the file is not a valid Waldiez file.
    """
    waldiez = Waldiez.load(
        waldiez_file,
        name=name,
        description=description,
        tags=tags,
        requirements=requirements,
    )
    return cls(waldiez, file_path=waldiez_file)

run(output_path: Optional[Union[str, Path]] = None, uploads_root: Optional[Union[str, Path]] = None, skip_mmd: bool = False) -> Union[ChatResult, List[ChatResult]]

Run the Waldiez workflow.

Parameters:

NameTypeDescriptionDefault
output_pathOptional[Union[str, Path]]

The output path, by default None.

None
uploads_rootOptional[Union[str, Path]]

The uploads root, to get user-uploaded files, by default None.

None
skip_mmdbool

Whether to skip the Mermaid diagram generation, by default False.

False

Returns:

TypeDescription
Union[ChatResult, List[ChatResult]]

The result(s) of the chat(s).

Raises:

TypeDescription
RuntimeError

If the workflow is already running.

Source code in waldiez/runner.py
def run(
    self,
    output_path: Optional[Union[str, Path]] = None,
    uploads_root: Optional[Union[str, Path]] = None,
    skip_mmd: bool = False,
) -> Union["ChatResult", List["ChatResult"]]:
    """Run the Waldiez workflow.

    Parameters
    ----------
    output_path : Optional[Union[str, Path]], optional
        The output path, by default None.
    uploads_root : Optional[Union[str, Path]], optional
        The uploads root, to get user-uploaded files, by default None.
    skip_mmd : bool, optional
        Whether to skip the Mermaid diagram generation, by default False.

    Returns
    -------
    Union[ChatResult, List[ChatResult]]
        The result(s) of the chat(s).

    Raises
    ------
    RuntimeError
        If the workflow is already running.
    """
    if self.waldiez.is_async:
        # pylint: disable=import-outside-toplevel
        from anyio.from_thread import start_blocking_portal

        with start_blocking_portal(backend="asyncio") as portal:
            return portal.call(
                self._a_run,
                output_path,
                uploads_root,
                skip_mmd,
            )
    if self._running is True:
        raise RuntimeError("Workflow already running")
    self._running = True
    file_path = output_path or self._file_path
    try:
        return self._run(file_path, uploads_root, skip_mmd)
    finally:
        self._running = False

running: bool property

Get the running status.

waldiez: Waldiez property

Get the Waldiez instance.