Skip to main content

Overview

The Code Interpreter SDK provides a high-level API for executing code in secure, isolated sandboxes. It supports multiple programming languages with state persistence across executions, making it ideal for building interactive coding environments, notebooks, and AI coding assistants.

Key Features

  • Multi-language support: Python, Java, Go, TypeScript/Node.js
  • State persistence: Variables and imports retained across executions
  • Isolated contexts: Separate execution contexts for different languages
  • Streaming output: Real-time stdout/stderr handling
  • Pre-configured runtimes: Ready-to-use language environments

Prerequisites

This SDK requires the opensandbox/code-interpreter Docker image, which includes pre-installed runtimes for supported languages. For detailed information about supported languages and versions, see the Environment Documentation.

Installation

Quick Start

Runtime Configuration

Docker Image

The Code Interpreter SDK relies on the opensandbox/code-interpreter image. Ensure your sandbox provider has this image available.

Language Version Selection

Specify the desired version of a programming language by setting environment variables when creating the Sandbox.

Usage Examples

Default Language Context

You can pass language directly (recommended: SupportedLanguage.*) and skip create_context. When context.id is omitted, execd will create/reuse a default session for that language, so state can persist across runs:

State Persistence Example

Java Code Execution

Python with State Persistence

Variables defined in one execution are available in subsequent executions within the same context.

Streaming Output Handling

Handle stdout/stderr and execution events in real-time.

Multi-Language Context Isolation

Different languages run in isolated environments.

Go Code Execution

TypeScript/Node.js Code Execution

API Reference

CodeInterpreter

Main class for code execution operations.

Methods

classmethod
Creates a CodeInterpreter instance.Parameters:
  • sandbox (Sandbox): An existing Sandbox instance with code-interpreter image
Returns: CodeInterpreter instance
property
Access to code execution operations.Returns: CodesClient

CodesClient

Handles code execution and context management.

Methods

async method
Creates a new execution context for a specific language.Parameters:
  • language (SupportedLanguage): The programming language
Returns: ExecutionContext
async method
Executes code in a context or with a specified language.Parameters:
  • code (str): The code to execute
  • context (ExecutionContext, optional): Existing context
  • language (SupportedLanguage, optional): Language (creates/reuses default context)
  • handlers (ExecutionHandlers, optional): Stream handlers
Returns: Execution

SupportedLanguage

Enum of supported programming languages.
  • SupportedLanguage.PYTHON
  • SupportedLanguage.JAVA
  • SupportedLanguage.GO
  • SupportedLanguage.TYPESCRIPT
  • SupportedLanguage.JAVASCRIPT

Comparison: Code Interpreter vs Sandbox SDK

Best Practices

2. Use Default Contexts for Simple Use Cases

3. Handle Errors Gracefully

4. Clean Up Resources

Examples

Building a REPL

Installing Packages

Notes

  • Lifecycle: CodeInterpreter wraps an existing Sandbox instance and reuses its connection configuration.
  • Asyncio/event loop: Avoid sharing long-lived clients across multiple event loops (e.g. pytest-asyncio defaults).
  • Image requirement: Must use opensandbox/code-interpreter image or a compatible derivative.

Next Steps

Sandbox SDK

Low-level SDK for direct sandbox control

Supported Languages

View language versions and capabilities

Examples

Browse more code examples

API Reference

Detailed API documentation