Skip to main content
The Pty module provides interactive pseudo-terminal (PTY) sessions over WebSocket. Access it via sandbox.pty.

Creating a PTY Session

session = await sandbox.pty.create(
    cols=120,
    rows=40,
    on_output=lambda data: print(data.decode(), end=''),
)

await sandbox.pty.create(**kwargs)

Opens a new interactive terminal session.
cols
int
default:"80"
Terminal width in columns.
rows
int
default:"24"
Terminal height in rows.
on_output
Callable[[bytes], None] | None
default:"None"
Callback invoked when the terminal produces output.
Returns: PtySession

PtySession

PtySession is a dataclass with the following interface:
Property / MethodTypeDescription
session_idstrUnique session identifier
sandbox_idstrID of the parent sandbox
send(data)async (str | bytes) -> NoneSend input to the terminal
recv()async () -> bytesReceive output from the terminal
close()async () -> NoneClose the PTY session

Examples

Run an Interactive Command

session = await sandbox.pty.create(
    on_output=lambda data: print(data.decode(), end=''),
)

# Type a command
await session.send('ls -la\n')

# Wait for output
import asyncio
await asyncio.sleep(2)

await session.close()

Manual Receive Loop

session = await sandbox.pty.create()

await session.send('echo "hello"\n')

# Read output manually
data = await session.recv()
print(data.decode())

await session.close()
PTY sessions use WebSocket connections under the hood. Each session maintains a persistent connection to the sandbox worker.