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 parsedresults: without hosting your own container runtime.
What you accomplish
You create a sandbox, run interpreter code (not raw shell injection) throughrunCode / 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 returnsCodeExecutionResultwithstdout,stderr,success,exitCode/exit_code,results,logsat the top level (no extra.datawrapper). Shellexecreturns a different shape; see Code execution. - Languages: You typically pass
python,javascript,bash, orsh(confirm what your deployment allows). - Timeout: You set
timeoutin 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 instantiateVoidRun 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 alanguage. You treat success as your primary pass/fail signal; you still log stderr when success is false.
Step 3: Stream output (optional, TypeScript)
You attachonStdout 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
stdoutdefensively; models sometimes print extra lines: you often take the last JSON line or useresultswhen the platform fills it. - You log
exitCode/exit_codeandstderrtogether when you debug failed generations.
Related guides
Code execution
Interpreter vs exec and response shapes
Untrusted user code
Validation and isolation checklist

