Last Updated: 3/7/2026
Code Interpreting with Streaming
Execute code with real-time output streaming for interactive experiences.
Overview
Streaming allows you to receive output from code execution in real-time, rather than waiting for the entire execution to complete. This is essential for:
- Long-running computations
- Progress tracking
- Interactive applications
- Live feedback to users
Basic Streaming
import { Sandbox } from 'e2b'
const sandbox = await Sandbox.create()
// Execute with streaming
const execution = await sandbox.commands.run('python script.py', {
onStdout: (output) => {
console.log('Output:', output)
},
onStderr: (error) => {
console.error('Error:', error)
}
})
console.log('Final exit code:', execution.exitCode)
await sandbox.close()Streaming Patterns
Progress Tracking
let currentProgress = 0
const code = `
import time
for i in range(100):
print(f"PROGRESS:{i+1}")
time.sleep(0.1)
`
await sandbox.files.write('/home/user/task.py', code)
await sandbox.commands.run('python /home/user/task.py', {
onStdout: (line) => {
const match = line.match(/PROGRESS:(\d+)/)
if (match) {
currentProgress = parseInt(match[1])
console.log(`Progress: ${currentProgress}%`)
}
}
})