Skip to main content

Create a context

ctx = sandbox.code_interpreter.create_code_context(
    cwd="/workspace",
    language="python",
)

Run in a context

sandbox.code_interpreter.run_code("value = 10", context=ctx)
result = sandbox.code_interpreter.run_code("value * 2", context=ctx)
print(result.results[0].text)

List, restart, and remove

contexts = sandbox.code_interpreter.list_code_contexts()
for item in contexts:
    print(item.id, item.language, item.cwd)

sandbox.code_interpreter.restart_code_context(ctx)
sandbox.code_interpreter.remove_code_context(ctx)

Interrupt long-running code

sandbox.code_interpreter.interrupt_code_context(ctx)