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

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.
Copy
AsyncCommandHandle(
  self,
  message_stream: AsyncIterator[dict],
  control: Optional[_AsyncWSStreamControl],
  sandbox: AsyncSandbox,
  *,
  command_id: str = '',
  stdout_offset: int = 0,
  stderr_offset: int = 0
)

Example:

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

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

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

Constructors

constructor
__init__
NameType
message_streamAsyncIterator[dict]
controlOptional[_AsyncWSStreamControl]
sandboxAsyncSandbox
command_idstr
stdout_offsetint
stderr_offsetint

Attributes

attribute
MAX_AUTO_RECONNECTS: int
attribute
command_id: Optional[str]

The server-assigned command ID. Available after _ensure_started.

attribute
pid: Optional[int]

The process ID on the sandbox. Available after _ensure_started.

attribute
result: ExecutionResult

The final execution result. Awaitable.

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.

method
send_input

Write data to the command's stdin.

method
reconnect

Reconnect to this command from the last known offsets.

View source on GitHub