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
Module●Since v0.6

sandbox

LangSmith Sandbox Module.

This module provides sandboxed code execution capabilities through the LangSmith Sandbox API.

Example:

from langsmith.sandbox import SandboxClient

Uses LANGSMITH_ENDPOINT and LANGSMITH_API_KEY from environment

client = SandboxClient()

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

Or async:

from langsmith.sandbox import AsyncSandboxClient

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

Classes

class
AsyncSandboxClient

Async client for interacting with the Sandbox Server API.

This client provides an async interface for managing sandboxes and templates.

class
AsyncSandbox

Represents an active sandbox for running commands and file operations async.

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

class
SandboxClient

Client for interacting with the Sandbox Server API.

This client provides a simple interface for managing sandboxes and templates.

class
CommandTimeoutError

Raised when a command exceeds its timeout.

class
DataplaneNotConfiguredError

Raised when dataplane_url is not available for the sandbox.

This occurs when the sandbox-router URL is not configured for the cluster.

class
QuotaExceededError

Raised when organization quota limits are exceeded.

Users should contact support@langchain.dev to increase quotas.

class
ResourceAlreadyExistsError

Raised when creating a resource that already exists.

class
ResourceCreationError

Raised when resource provisioning fails.

class
ResourceInUseError

Raised when deleting a resource that is still in use.

class
ResourceNameConflictError

Raised when updating a resource name to one that already exists.

class
ResourceNotFoundError

Raised when a resource is not found.

class
ResourceTimeoutError

Raised when an operation times out.

class
SandboxAPIError

Raised when the API endpoint returns an unexpected error.

For example, this is raised for wrong URL or path.

class
SandboxAuthenticationError

Raised when authentication fails (invalid or missing API key).

class
SandboxClientError

Base exception for sandbox client errors.

class
SandboxConnectionError

Raised when connection to the sandbox server fails.

class
SandboxNotReadyError

Raised when attempting to interact with a sandbox that is not ready.

class
SandboxOperationError

Raised when a sandbox operation fails (run, read, write).

class
SandboxServerReloadError

Raised when the server sends a 1001 Going Away close frame.

This indicates a server hot-reload, not a true connection failure. The command is still running on the server.

This is a subclass of SandboxConnectionError, so the auto-reconnect logic in CommandHandle catches it along with all other connection errors. The distinction matters for retry strategy: SandboxServerReloadError triggers immediate reconnect (no backoff), while other SandboxConnectionError triggers exponential backoff.

Users typically never see this exception — it's handled internally.

class
TunnelConnectionRefusedError

Nothing is listening on the target port inside the sandbox.

class
TunnelError

Base exception for TCP tunnel errors.

class
TunnelPortNotAllowedError

The daemon rejected the port as not allowed.

class
TunnelUnsupportedVersionError

Protocol version mismatch between the tunnel client and the daemon.

class
ValidationError

Raised when request validation fails.

This includes:

  • Resource values exceeding server-defined limits (CPU, memory, storage)
  • Invalid resource units
  • Invalid name formats
  • Pool validation failures (e.g., template has volumes)
class
AsyncCommandHandle

Async handle to a running command with streaming output and auto-reconnect.

Async iterable, yielding OutputChunk objects (stdout and stderr interleaved in arrival order). Access .result after iteration to get the full ExecutionResult.

Auto-reconnect behavior:

  • Server hot-reload (1001 Going Away): reconnect immediately
  • Network error / unexpected close: reconnect with exponential backoff
  • User called kill(): do NOT reconnect (propagate error)

Construction modes (controlled by command_id):

  • New execution (command_id="", the default): call await handle._ensure_started() after construction to read the server's "started" message and populate command_id / pid.
  • Reconnection (command_id set): skips the started-message read, since reconnect streams don't emit one.
class
CommandHandle

Handle to a running command with streaming output and auto-reconnect.

Iterable, yielding OutputChunk objects (stdout and stderr interleaved in arrival order). Access .result after iteration to get the full ExecutionResult.

Auto-reconnect behavior:

  • Server hot-reload (1001 Going Away): reconnect immediately
  • Network error / unexpected close: reconnect with exponential backoff
  • User called kill(): do NOT reconnect (propagate error)

The auto-reconnect is transparent -- the iterator reconnects and continues yielding chunks without any user intervention. If all reconnect attempts are exhausted, SandboxConnectionError is raised.

Construction modes (controlled by command_id):

  • New execution (command_id="", the default): the constructor eagerly reads the server's "started" message to populate command_id and pid before returning.
  • Reconnection (command_id set): skips the started-message read, since reconnect streams don't emit one.
class
ExecutionResult

Result of executing a command in a sandbox.

class
OutputChunk

A single chunk of streaming output from command execution.

class
Pool

Represents a Sandbox Pool for pre-provisioned sandboxes.

Pools pre-provision sandboxes from a template for faster startup. Instead of waiting for a new sandbox to be created, sandboxes can be served from a pre-warmed pool.

Note: Templates with volume mounts cannot be used in pools.

class
ResourceSpec

Resource specification for a sandbox.

class
ResourceStatus

Lightweight provisioning status for any async-created resource.

class
SandboxTemplate

Represents a SandboxTemplate.

Templates define the image, resource limits, and volume mounts for sandboxes. All other container details are handled by the server with secure defaults.

class
Volume

Represents a persistent volume.

Volumes are persistent storage that can be mounted in sandboxes.

class
VolumeMountSpec

Specification for mounting a volume in a sandbox template.

class
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.

class
AsyncTunnel

Async wrapper around :class:Tunnel.

The underlying tunnel runs in background threads (TCP listener + bridges); async context-manager methods delegate to the sync tunnel via the event loop's executor.

Usage::

async with await sandbox.tunnel(remote_port=5432) as t: conn = await asyncpg.connect(host="127.0.0.1", port=t.local_port) 
class
Tunnel

TCP tunnel to a port inside a sandbox.

Opens a local TCP listener and forwards each accepted connection through a yamux-multiplexed WebSocket to the daemon, which dials the target port inside the sandbox.

Typically used as a context manager::

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

Or with explicit lifecycle::

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