SandboxCodeTool
autogen.beta.tools.code.tool.SandboxCodeTool #
SandboxCodeTool(environment, *, languages=('python', 'bash'), runners=None, name='run_code', description='Execute code in a sandboxed environment. Supported languages: {languages}.', middleware=())
Bases: Tool
Exposes a single run_code(code, language) function that runs code inside an environment you choose — Docker, Daytona, or any custom backend.
The environment decides where code runs and carries all backend configuration; the tool decides which languages are accepted and how each maps to a runner. The same environment can back both a :class:SandboxCodeTool and a :class:~autogen.beta.tools.SandboxShellTool.
Unlike :class:~autogen.beta.tools.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 must be wired to a backend that genuinely sandboxes execution. A :class:~autogen.beta.tools.LocalEnvironment is accepted but only when passed explicitly (it offers no isolation).
Examples::
from autogen.beta.tools import SandboxCodeTool
from autogen.beta.extensions.docker import DockerEnvironment
docker = DockerEnvironment(image="python:3.12-slim")
code = SandboxCodeTool(docker, languages=("python", "bash"))
# Advanced: pass a pre-built CodeAdapter (custom runners):
from autogen.beta.tools.sandbox import CodeAdapter, LocalEnvironment, LanguageRunner
code = SandboxCodeTool(
CodeAdapter(
LocalEnvironment(),
languages=("typescript",),
runners={"typescript": LanguageRunner(file_extension="ts", file_runner_argv=("tsx",))},
)
)
| PARAMETER | DESCRIPTION |
|---|---|
environment | What runs the code. Either a backend — a :class: TYPE: |
languages | Languages this tool accepts (backend form only). TYPE: |
runners | Override / extend the default language→runner mapping (backend form only). TYPE: |
name / description / middleware | Tool wiring.
|