motleycrew.tools.code.python_repl

Functions

warn_once()

Warn once about the dangers of PythonREPL with untrusted LLMs.

Classes

PythonREPLTool([return_direct, ...])

Python REPL tool.

REPLToolInput(*, command)

Input for the REPL tool.

Exceptions

MissingPrintStatementError(command)

Exception raised when a print statement is missing from the command.

motleycrew.tools.code.python_repl.warn_once() None

Warn once about the dangers of PythonREPL with untrusted LLMs.

exception motleycrew.tools.code.python_repl.MissingPrintStatementError(command: str)

Bases: Exception

Exception raised when a print statement is missing from the command.

__init__(command: str)
class motleycrew.tools.code.python_repl.PythonREPLTool(return_direct: bool = False, handle_exceptions: bool | List[Type[Exception]] = False)

Bases: MotleyTool

Python REPL tool. Use this to execute python commands.

Note that the tool’s output is the content printed to stdout by the executed code. Because of this, any data you want to be in the output should be printed using print(…).

__init__(return_direct: bool = False, handle_exceptions: bool | List[Type[Exception]] = False)

Initialize the MotleyTool.

Parameters:
  • name – Name of the tool (required if tool is None).

  • description – Description of the tool (required if tool is None).

  • args_schema – Schema of the tool arguments (required if tool is None).

  • return_direct – If True, the tool’s output will be returned directly to the user.

  • handle_exceptions

    Whether to handle exceptions (return their message as output).

    If True, the tool will return any raised exception’s message as its output.

    If a list of exceptions is provided, only these exceptions will be handled.

    If False, the tool will raise the exception.

    If return_direct is True, the tool will always handle InvalidOutput exceptions, as the tool is considered an output handler.

  • retry_config – Configuration for retry behavior. If None, exceptions will not be retried.

  • tool – Langchain BaseTool to wrap. Usually not needed, as the tool is created from the run method.

static sanitize_input(query: str) str

Sanitize input to the python REPL.

Remove whitespace, backtick & python (if llm mistakes python console as terminal)

Parameters:

query – The query to sanitize

Returns:

The sanitized query

Return type:

str

run(command: str) str
validate_input(command: str)
class motleycrew.tools.code.python_repl.REPLToolInput(*, command: str)

Bases: BaseModel

Input for the REPL tool.

command: str
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].