Skip to main content
sandbox.code_interpreter runs notebook-style code in persistent kernel contexts. Use it when the workload is cells, rich outputs, packages, or artifacts rather than shell commands.

Execution

MethodUse
run_code(code, language="python", context=None, envs=None, timeout=None, on_stdout=None, on_stderr=None, on_result=None, on_artifact=None)Execute a code cell and collect logs, results, artifacts, and errors.
install_packages(packages)Install packages into the code interpreter environment.
execution = sandbox.code_interpreter.run_code(
    "import pandas as pd\nprint(pd.__version__)",
    language="python",
)
print(execution.logs.stdout)

Contexts

MethodUse
create_code_context(language="python")Create a persistent kernel context.
list_code_contexts()List contexts.
restart_code_context(context)Restart a kernel context.
interrupt_code_context(context)Interrupt running code.
remove_code_context(context)Delete a context.

Runs And Artifacts

MethodUse
list_runs(...)Inspect async run records.
list_artifacts(run_id=None, context_id=None)List produced artifacts.
get_artifact(id)Inspect artifact metadata.
download_artifact(id) / preview_artifact(id)Fetch artifact bytes.
delete_artifact(id)Delete an artifact.
Queued run mutation helpers exist in the SDK but are not part of the hosted private-beta launch surface until remote host-agent routing for queued code runs is enabled.