Skip to main content

Example: AI code interpreter

You build this pattern when your product executes model-generated code and you need reliable stdout, stderr, success flags, and optional parsed results: without hosting your own container runtime.

What you accomplish

You create a sandbox, run interpreter code (not raw shell injection) through runCode / run_code, inspect CodeExecutionResult, and remove the sandbox when the session ends. You keep timeouts tight so a bad generation cannot hang your workers.

Concepts you use

  • Interpreter vs exec: The interpreter path returns CodeExecutionResult with stdout, stderr, success, exitCode / exit_code, results, logs at the top level (no extra .data wrapper). Shell exec returns a different shape; see Code execution.
  • Languages: You typically pass python, javascript, bash, or sh (confirm what your deployment allows).
  • Timeout: You set timeout in seconds on the interpreter call so runaway loops stop.

Prerequisites

You completed Examples overview (API key, base URL, SDK install).

Step 1: Create the client and sandbox

You instantiate VoidRun once per process (or per request if you prefer strict isolation at the client layer). You create a sandbox with enough CPU and memory for typical snippets.

Step 2: Run interpreter code

You pass source text and a language. You treat success as your primary pass/fail signal; you still log stderr when success is false.

Step 3: Stream output (optional, TypeScript)

You attach onStdout and onStderr on runCode when you want incremental logs while the interpreter runs. You still await the promise; the final object remains a CodeExecutionResult.
The Python run_code path builds a single exec under the hood and returns one CodeExecutionResult when the run finishes. When you need line-by-line output in Python, you use exec_stream on a normal shell command instead, or you consult the current voidrun README for interpreter streaming support in your version.

Step 4: Tear down

You always remove the sandbox when you finish the user session (unless you maintain a pool: then you return the slot to the pool per your lifecycle rules).

Practices you should follow

  • You validate language and maximum code length before you call the API (see Untrusted user code).
  • You use one sandbox per untrusted user turn unless you operate a hardened pool with strict hygiene.
  • You parse stdout defensively; models sometimes print extra lines: you often take the last JSON line or use results when the platform fills it.
  • You log exitCode / exit_code and stderr together when you debug failed generations.

Code execution

Interpreter vs exec and response shapes

Untrusted user code

Validation and isolation checklist