Skip to main content
The code interpreter is the stateful execution layer for Python and other notebook-style kernels. Use template="code-interpreter" for the standard Python environment.

Quick start

from nullspace import Sandbox

with Sandbox.create(template="code-interpreter") as sandbox:
    execution = sandbox.run_code("x = 40 + 2\nx")
    print(execution.results[0].text)

Contexts

ctx = sandbox.code_interpreter.create_code_context(
    cwd="/workspace",
    language="python",
)
sandbox.code_interpreter.run_code("value = 10", context=ctx)
result = sandbox.code_interpreter.run_code("value * 2", context=ctx)
print(result.results[0].text)

Stream callbacks

def on_stdout(message):
    print(message.line, end="")

sandbox.run_code("print('streamed')", on_stdout=on_stdout)
Use sandbox.code_interpreter.run_code() directly when you need the full callback surface:
sandbox.code_interpreter.run_code(
    "open('/workspace/out.txt', 'w').write('done')",
    language="python",
    envs={"RUN_ID": "docs"},
    timeout=30,
    on_artifact=lambda artifact: print(artifact.id, artifact.kind),
)
list_runs is available for read-only hosted beta inspection. Queued async run mutation/wait helpers (create_run, get_run, cancel_run, and wait_for_run) are not part of the hosted private beta launch surface because remote host-agent routing for queued code runs is not implemented yet. Artifact helpers (list_artifacts, get_artifact, download_artifact, preview_artifact, and delete_artifact) are supported for hosted beta run_code executions. Concept: Exec. API reference: executeCode, createCodeContext, createCodeRun, and listCodeArtifacts.