Skip to content

SandboxCodeTool

autogen.beta.tools.code.tool.SandboxCodeTool #

SandboxCodeTool(environment, name='run_code', *, description='Execute code in a sandboxed environment. Supported languages: {languages}.', middleware=())

Bases: Tool

Exposes a single run_code(code, language) function backed by a :class:CodeEnvironment — Daytona, Docker, or any other implementation of the protocol.

Unlike :class:CodeExecutionTool (which delegates execution to the LLM provider's built-in sandbox), SandboxCodeTool runs client-side, so it works on every provider regardless of native code-execution support.

There is no default backend: environment is required. The class name is a contract — it executes whatever the model writes, so it should only be wired to a backend that genuinely sandboxes execution. Use :class:~autogen.beta.extensions.daytona.DaytonaCodeEnvironment or :class:~autogen.beta.extensions.docker.DockerCodeEnvironment (or your own implementation of :class:CodeEnvironment).

PARAMETER DESCRIPTION
environment

The execution backend.

TYPE: CodeEnvironment

name

Tool name shown to the model. Defaults to "run_code".

TYPE: str DEFAULT: 'run_code'

description

Tool description shown to the model. {languages} is substituted with the environment's supported languages.

TYPE: str DEFAULT: 'Execute code in a sandboxed environment. Supported languages: {languages}.'

middleware

Tool middleware applied around each invocation.

TYPE: Iterable[ToolMiddleware] DEFAULT: ()

Examples::

from autogen.beta.extensions.daytona import DaytonaCodeEnvironment
from autogen.beta.extensions.docker import DockerCodeEnvironment

# Hosted sandbox
code = SandboxCodeTool(DaytonaCodeEnvironment(image="python:3.12"))

# Local container
code = SandboxCodeTool(DockerCodeEnvironment(image="python:3.12-slim"))
Source code in autogen/beta/tools/code/tool.py
def __init__(
    self,
    environment: CodeEnvironment,
    name: str = "run_code",
    *,
    description: str = "Execute code in a sandboxed environment. Supported languages: {languages}.",
    middleware: Iterable[ToolMiddleware] = (),
) -> None:
    async def run_code(code: str, language: CodeLanguage, ctx: Context) -> str:
        result = await environment.run(code, language, context=ctx)
        if result.exit_code != 0:
            suffix = f"[exit code: {result.exit_code}]"
            return f"{result.output}\n{suffix}" if result.output else suffix
        return result.output

    self._env = environment
    self._tool: FunctionTool = tool(
        run_code,
        name=name,
        description=description.format(languages=", ".join(environment.supported_languages)),
        middleware=middleware,
    )
    self.name = name

name instance-attribute #

name = name

environment property #

environment

The underlying execution environment.

schemas async #

schemas(context)
Source code in autogen/beta/tools/code/tool.py
async def schemas(self, context: "Context") -> list:
    return await self._tool.schemas(context)

register #

register(stack, context, *, middleware=())
Source code in autogen/beta/tools/code/tool.py
def register(
    self,
    stack: "ExitStack",
    context: "Context",
    *,
    middleware: Iterable["BaseMiddleware"] = (),
) -> None:
    self._tool.register(stack, context, middleware=middleware)

set_provider #

set_provider(provider)
Source code in autogen/beta/tools/tool.py
def set_provider(self, provider: Provider) -> None:
    pass