DockerCommandLineCodeExecutor

class DockerCommandLineCodeExecutor(CodeExecutor)

__init__

def __init__(image: str = "python:3-slim",
             container_name: str | None = None,
             timeout: int = 60,
             work_dir: Path | str = Path("."),
             bind_dir: Path | str | None = None,
             auto_remove: bool = True,
             stop_container: bool = True,
             execution_policies: dict[str, bool] | None = None)

(Experimental) A code executor class that executes code through a command line environment in a Docker container.

The executor first saves each code block in a file in the working directory, and then executes the code file in the container. The executor executes the code blocks in the order they are received. Currently, the executor only supports Python and shell scripts. For Python code, use the language “python” for the code block. For shell scripts, use the language “bash”, “shell”, or “sh” for the code block.

Arguments:

  • image type, optional - Docker image to use for code execution. Defaults to “python:3-slim”.
  • container_name Optional[str], optional - Name of the Docker container which is created. If None, will autogenerate a name. Defaults to None.
  • timeout int, optional - The timeout for code execution. Defaults to 60.
  • work_dir Union[Path, str], optional - The working directory for the code execution. Defaults to Path(”.”).
  • bind_dir Union[Path, str], optional - The directory that will be bound to the code executor container. Useful for cases where you want to spawn the container from within a container. Defaults to work_dir.
  • auto_remove bool, optional - If true, will automatically remove the Docker container when it is stopped. Defaults to True.
  • stop_container bool, optional - If true, will automatically stop the container when stop is called, when the context manager exits or when the Python process exits with atext. Defaults to True.

Raises:

  • ValueError - On argument error, or if the container fails to start.

timeout

@property
def timeout() -> int

(Experimental) The timeout for code execution.

work_dir

@property
def work_dir() -> Path

(Experimental) The working directory for the code execution.

bind_dir

@property
def bind_dir() -> Path

(Experimental) The binding directory for the code execution container.

code_extractor

@property
def code_extractor() -> CodeExtractor

(Experimental) Export a code extractor that can be used by an agent.

execute_code_blocks

def execute_code_blocks(code_blocks: list[CodeBlock]) -> CommandLineCodeResult

(Experimental) Execute the code blocks and return the result.

Arguments:

  • code_blocks List[CodeBlock] - The code blocks to execute.

Returns:

  • CommandlineCodeResult - The result of the code execution.

restart

def restart() -> None

(Experimental) Restart the code executor.

stop

def stop() -> None

(Experimental) Stop the code executor.