LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • Client
  • AsyncClient
  • Run Helpers
  • Run Trees
  • Evaluation
  • Schemas
  • Utilities
  • Wrappers
  • Anonymizer
  • Testing
  • Expect API
  • Middleware
  • Pytest Plugin
  • Deployment SDK
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

OverviewClientAsyncClientRun HelpersRun TreesEvaluationSchemasUtilitiesWrappersAnonymizerTestingExpect APIMiddlewarePytest PluginDeployment SDK
Language
Theme
Pythonlangsmithsandbox_sandboxSandbox
Class●Since v0.6

Sandbox

Represents an active sandbox for running commands and file operations.

This class is typically obtained from SandboxClient.sandbox() and supports the context manager protocol for automatic cleanup.

Copy
Sandbox(
  self,
  name: str,
  template_name: str,
  dataplane_url: Optional[str] = None,
  id: Optional[str] = None,
  status: str = 'ready',
  status_message: Optional[str] = None,
  created_at: Optional[str] = None,
  updated_at: Optional[str] = None,
  ttl_seconds: Optional[int] = None,
  idle_ttl_seconds: Optional[int] = None,
  expires_at: Optional[str] = None,
  _client: SandboxClient = None,
  _auto_delete: bool = True
)

Example:

with client.sandbox(template_name="python-sandbox") as sandbox: result = sandbox.run("python --version") print(result.stdout)

Constructors

constructor
__init__
NameType
namestr
template_namestr
dataplane_urlOptional[str]
idOptional[str]
statusstr
status_messageOptional[str]
created_atOptional[str]
updated_atOptional[str]
ttl_secondsOptional[int]
idle_ttl_secondsOptional[int]
expires_atOptional[str]
_clientSandboxClient
_auto_deletebool

Attributes

attribute
name: str
attribute
template_name: str
attribute
dataplane_url: Optional[str]
attribute
id: Optional[str]
attribute
status: str
attribute
status_message: Optional[str]
attribute
created_at: Optional[str]
attribute
updated_at: Optional[str]
attribute
ttl_seconds: Optional[int]
attribute
idle_ttl_seconds: Optional[int]
attribute
expires_at: Optional[str]

Methods

method
from_dict

Create a Sandbox from API response dict.

method
run

Execute a command in the sandbox.

method
reconnect

Reconnect to a running or recently-finished command.

Resumes output from the given byte offsets. Any output produced while the client was disconnected is replayed from the server's ring buffer.

method
write

Write content to a file in the sandbox.

method
read

Read a file from the sandbox.

method
tunnel

Open a TCP tunnel to a port inside the sandbox.

Creates a local TCP listener that forwards connections through a yamux-multiplexed WebSocket to the specified port inside the sandbox. Works with any TCP protocol (databases, Redis, HTTP, etc.).

Use as a context manager for automatic cleanup::

with sandbox.tunnel(remote_port=5432) as t:
    conn = psycopg2.connect(host="127.0.0.1", port=t.local_port)

Or manage the lifecycle explicitly::

t = sandbox.tunnel(remote_port=5432)
# ... use tunnel ...
t.close()
View source on GitHub