| title | E2B |
|---|---|
| id | integrations-e2b |
| description | E2B integration for Haystack |
| slug | /integrations-e2b |
Bases: Tool
A :class:~haystack.tools.Tool that executes bash commands inside an E2B sandbox.
Pass the same :class:E2BSandbox instance to multiple tool classes so they
all operate in the same live sandbox environment.
from haystack_integrations.tools.e2b import E2BSandbox, RunBashCommandTool, ReadFileTool
sandbox = E2BSandbox()
agent = Agent(
chat_generator=...,
tools=[
RunBashCommandTool(sandbox=sandbox),
ReadFileTool(sandbox=sandbox),
],
)__init__(sandbox: E2BSandbox) -> NoneCreate a RunBashCommandTool.
Parameters:
- sandbox (
E2BSandbox) – The :class:E2BSandboxinstance that will execute commands.
to_dict() -> dict[str, Any]Serialize this tool to a dictionary.
from_dict(data: dict[str, Any]) -> RunBashCommandToolDeserialize a RunBashCommandTool from a dictionary.
Manages the lifecycle of an E2B cloud sandbox.
Instantiate this class and pass it to one or more E2B tool classes
(RunBashCommandTool, ReadFileTool, WriteFileTool,
ListDirectoryTool) to share a single sandbox environment across all
tools. All tools that receive the same E2BSandbox instance operate
inside the same live sandbox process.
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.agents import Agent
from haystack_integrations.tools.e2b import (
E2BSandbox,
RunBashCommandTool,
ReadFileTool,
WriteFileTool,
ListDirectoryTool,
)
sandbox = E2BSandbox()
agent = Agent(
chat_generator=OpenAIChatGenerator(model="gpt-4o"),
tools=[
RunBashCommandTool(sandbox=sandbox),
ReadFileTool(sandbox=sandbox),
WriteFileTool(sandbox=sandbox),
ListDirectoryTool(sandbox=sandbox),
],
)Lifecycle is handled automatically by the Agent's pipeline. If you use the
tools standalone, call :meth:warm_up before the first tool invocation:
sandbox.warm_up()
# ... use tools ...
sandbox.close()__init__(
api_key: Secret = Secret.from_env_var("E2B_API_KEY", strict=True),
sandbox_template: str = "base",
timeout: int = 120,
environment_vars: dict[str, str] | None = None,
instance_id: str | None = None,
) -> NoneCreate an E2BSandbox instance.
Parameters:
- api_key (
Secret) – E2B API key. - sandbox_template (
str) – E2B sandbox template name. - timeout (
int) – Sandbox inactivity timeout in seconds. - environment_vars (
dict[str, str] | None) – Optional environment variables to inject into the sandbox. - instance_id (
str | None) – Stable identifier preserved acrossto_dict/from_dict. When omitted, a fresh UUID is generated. Tools that share the sameE2BSandboxinstance inherit this id, which is what lets them re-share the instance after a serialization round-trip. Distinct from the cloud-side sandbox id assigned by E2B at warm-up.
warm_up() -> NoneEstablish the connection to the E2B sandbox.
Idempotent -- calling it multiple times has no effect if the sandbox is already running.
Raises:
RuntimeError– If the E2B sandbox cannot be created.
close() -> NoneShut down the E2B sandbox and release all associated resources.
Call this when you are done to avoid leaving idle sandboxes running.
to_dict() -> dict[str, Any]Serialize the sandbox configuration to a dictionary.
Returns:
dict[str, Any]– Dictionary containing the serialised configuration.
from_dict(data: dict[str, Any]) -> E2BSandboxDeserialize an :class:E2BSandbox from a dictionary.
Multiple tools that shared a single :class:E2BSandbox before serialization
will share the same restored instance: each tool's from_dict consults a
process-wide cache keyed on instance_id. A cache hit is only honored when
the full serialized config (api_key, template, timeout, environment_vars)
matches the cached entry — a crafted YAML with a guessed id but a different
config falls through to a fresh instance and never observes the cached one.
Parameters:
- data (
dict[str, Any]) – Dictionary created by :meth:to_dict.
Returns:
E2BSandbox– An :class:E2BSandboxinstance ready to be warmed up. May be a previously-restored instance if the id and config match.
Bases: Tool
A :class:~haystack.tools.Tool that lists directory contents in an E2B sandbox.
Pass the same :class:E2BSandbox instance to multiple tool classes so they
all operate in the same live sandbox environment.
from haystack_integrations.tools.e2b import E2BSandbox, ListDirectoryTool
sandbox = E2BSandbox()
agent = Agent(chat_generator=..., tools=[ListDirectoryTool(sandbox=sandbox)])__init__(sandbox: E2BSandbox) -> NoneCreate a ListDirectoryTool.
Parameters:
- sandbox (
E2BSandbox) – The :class:E2BSandboxinstance to list directories from.
to_dict() -> dict[str, Any]Serialize this tool to a dictionary.
from_dict(data: dict[str, Any]) -> ListDirectoryToolDeserialize a ListDirectoryTool from a dictionary.
Bases: Tool
A :class:~haystack.tools.Tool that reads files from an E2B sandbox filesystem.
Pass the same :class:E2BSandbox instance to multiple tool classes so they
all operate in the same live sandbox environment.
from haystack_integrations.tools.e2b import E2BSandbox, ReadFileTool
sandbox = E2BSandbox()
agent = Agent(chat_generator=..., tools=[ReadFileTool(sandbox=sandbox)])__init__(sandbox: E2BSandbox) -> NoneCreate a ReadFileTool.
Parameters:
- sandbox (
E2BSandbox) – The :class:E2BSandboxinstance to read files from.
to_dict() -> dict[str, Any]Serialize this tool to a dictionary.
from_dict(data: dict[str, Any]) -> ReadFileToolDeserialize a ReadFileTool from a dictionary.
Bases: Toolset
A :class:~haystack.tools.Toolset that bundles all E2B sandbox tools.
All tools in the set share a single :class:E2BSandbox instance so they
operate inside the same live sandbox process. The toolset owns the sandbox
lifecycle: calling :meth:warm_up starts the sandbox, and serialisation
round-trips preserve the shared-sandbox relationship.
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.agents import Agent
from haystack_integrations.tools.e2b import E2BToolset
agent = Agent(
chat_generator=OpenAIChatGenerator(model="gpt-4o"),
tools=E2BToolset(),
)__init__(
api_key: Secret = Secret.from_env_var("E2B_API_KEY", strict=True),
sandbox_template: str = "base",
timeout: int = 120,
environment_vars: dict[str, str] | None = None,
) -> NoneCreate an E2BToolset.
Parameters:
- api_key (
Secret) – E2B API key. Defaults toSecret.from_env_var("E2B_API_KEY"). - sandbox_template (
str) – E2B sandbox template name. Defaults to"base". - timeout (
int) – Sandbox inactivity timeout in seconds. Defaults to120. - environment_vars (
dict[str, str] | None) – Optional environment variables to inject into the sandbox.
warm_up() -> NoneStart the shared E2B sandbox (idempotent).
close() -> NoneShut down the shared E2B sandbox and release cloud resources.
to_dict() -> dict[str, Any]Serialize this toolset to a dictionary.
from_dict(data: dict[str, Any]) -> E2BToolsetDeserialize an E2BToolset from a dictionary.
Bases: Tool
A :class:~haystack.tools.Tool that writes files to an E2B sandbox filesystem.
Pass the same :class:E2BSandbox instance to multiple tool classes so they
all operate in the same live sandbox environment.
from haystack_integrations.tools.e2b import E2BSandbox, WriteFileTool
sandbox = E2BSandbox()
agent = Agent(chat_generator=..., tools=[WriteFileTool(sandbox=sandbox)])__init__(sandbox: E2BSandbox) -> NoneCreate a WriteFileTool.
Parameters:
- sandbox (
E2BSandbox) – The :class:E2BSandboxinstance to write files to.
to_dict() -> dict[str, Any]Serialize this tool to a dictionary.
from_dict(data: dict[str, Any]) -> WriteFileToolDeserialize a WriteFileTool from a dictionary.