Stream code output
runCode and run_code can deliver output before execution finishes. Use separate callbacks for standard output, standard error, and structured results.
mjs
const sandbox = await Sandbox.create("code-interpreter");
try {
await sandbox.runCode(
"import sys\nprint('first')\nprint('warning', file=sys.stderr)\n6 * 7",
{
onStdout: (message) => console.log(`stdout: ${message.line}`),
onStderr: (message) => console.log(`stderr: ${message.line}`),
onResult: (result) => console.log(`result: ${result.text}`),
},
);
} finally {
await sandbox.kill();
}The returned execution object still contains all logs, results, and execution errors after the stream ends. Callbacks are useful when you want to show progress immediately; use the returned object when you need the complete record.
See the JavaScript reference or Python reference for all callback and timeout options.