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_modelsCommandHandle
Class●Since v0.7

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.
Copy
CommandHandle(
  self,
  message_stream: Iterator[dict],
  control: Optional[_WSStreamControl],
  sandbox: Sandbox,
  *,
  command_id: str = '',
  stdout_offset: int = 0,
  stderr_offset: int = 0
)

Example:

handle = sandbox.run("make build", timeout=600, wait=False)

for chunk in handle: # auto-reconnects on transient errors print(chunk.data, end="")

result = handle.result print(f"Exit code: {result.exit_code}")

Constructors

constructor
__init__
NameType
message_streamIterator[dict]
controlOptional[_WSStreamControl]
sandboxSandbox
command_idstr
stdout_offsetint
stderr_offsetint

Attributes

attribute
MAX_AUTO_RECONNECTS: int
attribute
command_id: Optional[str]

The server-assigned command ID. Available after construction.

attribute
pid: Optional[int]

The process ID on the sandbox. Available after construction.

attribute
result: ExecutionResult

The final execution result. Blocks until the command completes.

Drains the remaining stream if not already exhausted, then returns the ExecutionResult with aggregated stdout, stderr, and exit_code.

attribute
last_stdout_offset: int

Last known stdout byte offset (for manual reconnection).

attribute
last_stderr_offset: int

Last known stderr byte offset (for manual reconnection).

Methods

method
kill

Send a kill signal to the running command (SIGKILL).

The server kills the entire process group. The stream will subsequently yield an exit message with a non-zero exit code.

Has no effect if the command has already exited or the WebSocket connection is closed.

method
send_input

Write data to the command's stdin.

method
reconnect

Reconnect to this command from the last known offsets.

Returns a new handle that resumes output from where this one left off. Any output produced while disconnected is replayed from the server's ring buffer.

View source on GitHub