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_sandboxSandboxrun
Methodā—Since v0.6

run

Execute a command in the sandbox.

Copy
run(
  self,
  command: str,
  *,
  timeout: int = 60,
  env: Optional[dict[str, str]] = None,
  cwd: Optional[str] = None,
  shell: str = '/bin/bash',
  on_stdout: Optional[Callable[[str], Any]] = None,
  on_stderr: Optional[Callable[[str], Any]] = None,
  idle_timeout: int = 300,
  kill_on_disconnect: bool = False,
  ttl_seconds: int = 600,
  pty: bool = False,
  headers: RequestHeaders = None,
  wait: bool = True
) -> Union[ExecutionResult, CommandHandle]

Parameters

NameTypeDescription
command*str

Shell command to execute.

timeoutint
Default:60

Command timeout in seconds.

envOptional[dict[str, str]]
Default:None

Environment variables to set for the command.

cwdOptional[str]
Default:None

Working directory for command execution. If None, uses sandbox default.

shellstr
Default:'/bin/bash'

Shell to use for command execution. Defaults to "/bin/bash".

on_stdoutOptional[Callable[[str], Any]]
Default:None

Callback invoked with each stdout chunk as it arrives. Blocks until the command completes and returns ExecutionResult. Cannot be combined with wait=False.

on_stderrOptional[Callable[[str], Any]]
Default:None

Callback invoked with each stderr chunk as it arrives. Blocks until the command completes and returns ExecutionResult. Cannot be combined with wait=False.

idle_timeoutint
Default:300

Idle timeout in seconds. If the command has no connected clients for this duration, it is killed. Defaults to 300 (5 minutes). Set to -1 for no idle timeout. Only applies to WebSocket execution.

kill_on_disconnectbool
Default:False

If True, kill the command immediately when the last client disconnects. Defaults to False (command continues running and can be reconnected to).

ttl_secondsint
Default:600

How long (in seconds) a finished command's session is kept for reconnection. Defaults to 600 (10 minutes). Set to -1 to keep indefinitely.

ptybool
Default:False

If True, allocate a pseudo-terminal for the command. Useful for commands that require a TTY (e.g., interactive programs, commands that use terminal control codes). Defaults to False.

waitbool
Default:True

If True (default), block until the command completes and return ExecutionResult. If False, return a CommandHandle immediately for streaming output, kill, stdin input, and reconnection. Cannot be combined with on_stdout/on_stderr callbacks.

View source on GitHub