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 theopensandbox/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
- pip
- uv
Quick Start
- Async
- Sync
Runtime Configuration
Docker Image
The Code Interpreter SDK relies on theopensandbox/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 passlanguage 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
CodeInterpreter instanceproperty
Access to code execution operations.Returns:
CodesClientCodesClient
Handles code execution and context management.
Methods
async method
Creates a new execution context for a specific language.Parameters:
language(SupportedLanguage): The programming language
ExecutionContextasync method
Executes code in a context or with a specified language.Parameters:
code(str): The code to executecontext(ExecutionContext, optional): Existing contextlanguage(SupportedLanguage, optional): Language (creates/reuses default context)handlers(ExecutionHandlers, optional): Stream handlers
ExecutionSupportedLanguage
Enum of supported programming languages.
SupportedLanguage.PYTHONSupportedLanguage.JAVASupportedLanguage.GOSupportedLanguage.TYPESCRIPTSupportedLanguage.JAVASCRIPT
Comparison: Code Interpreter vs Sandbox SDK
Best Practices
1. Reuse Contexts for Related Code
2. Use Default Contexts for Simple Use Cases
3. Handle Errors Gracefully
4. Clean Up Resources
Examples
Building a REPL
Installing Packages
Notes
- Lifecycle:
CodeInterpreterwraps an existingSandboxinstance 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-interpreterimage 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