or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

charts.mdcontexts.mdexecution.mdindex.mdresults.mdsandbox.md
tile.json

index.mddocs/

E2B Code Interpreter

E2B Code Interpreter is a JavaScript/TypeScript SDK that provides secure, stateful code execution in cloud sandboxes. It enables AI agents and applications to run dynamic code safely in isolated environments, supporting multiple programming languages with real-time streaming and comprehensive result handling.

Package Information

  • Package Name: @e2b/code-interpreter
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @e2b/code-interpreter

Core Imports

import { Sandbox } from "@e2b/code-interpreter";

Import specific types and interfaces:

import { 
  Sandbox, 
  RunCodeOpts, 
  Context, 
  CreateCodeContextOpts, 
  Result, 
  Execution, 
  ExecutionError, 
  OutputMessage,
  Logs,
  MIMEType,
  RawData,
  ChartType,
  ScaleType,
  ChartTypes,
  Chart,
  BarChart,
  BarData,
  LineChart,
  ScatterChart,
  BoxAndWhiskerChart,
  BoxAndWhiskerData,
  PieChart,
  PieData,
  SuperChart,
  PointData,
  deserializeChart
} from "@e2b/code-interpreter";

Default import:

import Sandbox from "@e2b/code-interpreter";

For CommonJS:

const { Sandbox } = require("@e2b/code-interpreter");

Note: This package re-exports all functionality from the base e2b SDK, providing access to file system operations, process management, and other sandbox capabilities in addition to code interpretation features.

Basic Usage

import { Sandbox } from "@e2b/code-interpreter";

// Create a sandbox instance
const sandbox = await Sandbox.create();

// Execute Python code with state persistence
await sandbox.runCode('x = 1');
const result = await sandbox.runCode('x += 1; x');
console.log(result.text); // "2"

// Handle multiple output formats
const plotResult = await sandbox.runCode(`
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
`);

console.log(plotResult.results[0].formats()); // Available formats: ['png', 'text', ...]
console.log(plotResult.results[0].png); // Base64 encoded PNG image

Architecture

E2B Code Interpreter is built around several key components:

  • Sandbox Class: Main interface extending the base E2B sandbox with code execution capabilities
  • Execution System: Streaming execution engine with real-time stdout/stderr handling
  • Result Processing: Multi-format result handling supporting text, images, charts, and data structures
  • Context Management: Isolated execution contexts for different languages and environments
  • Chart Integration: Built-in support for visualization libraries with structured chart data
  • Error Handling: Comprehensive error reporting with stack traces and execution context

Capabilities

Sandbox Management

Core sandbox functionality for creating and managing secure code execution environments.

class Sandbox {
  static create(options?: any): Promise<Sandbox>;
}

Sandbox Management

Code Execution

Execute code in various programming languages with comprehensive result handling and real-time streaming.

interface RunCodeOpts {
  onStdout?: (output: OutputMessage) => Promise<any> | any;
  onStderr?: (output: OutputMessage) => Promise<any> | any;
  onResult?: (data: Result) => Promise<any> | any;
  onError?: (error: ExecutionError) => Promise<any> | any;
  envs?: Record<string, string>;
  timeoutMs?: number;
  requestTimeoutMs?: number;
}

runCode(
  code: string,
  opts?: RunCodeOpts & { language?: string }
): Promise<Execution>;

runCode(
  code: string,
  opts?: RunCodeOpts & { context?: Context }
): Promise<Execution>;

Code Execution

Context Management

Create and manage isolated execution contexts for different languages and working directories.

interface CreateCodeContextOpts {
  cwd?: string;
  language?: string;
  requestTimeoutMs?: number;
}

interface Context {
  id: string;
  language: string;
  cwd: string;
}

createCodeContext(opts?: CreateCodeContextOpts): Promise<Context>;

Context Management

Result Processing

Comprehensive result handling with support for multiple output formats and data types.

class Result {
  readonly text?: string;
  readonly html?: string;
  readonly markdown?: string;
  readonly svg?: string;
  readonly png?: string;
  readonly jpeg?: string;
  readonly pdf?: string;
  readonly latex?: string;
  readonly json?: string;
  readonly javascript?: string;
  readonly data?: Record<string, unknown>;
  readonly chart?: ChartTypes;
  readonly extra?: any;
  readonly raw: RawData;
  readonly isMainResult: boolean;
  formats(): string[];
  toJSON(): object;
}

class Execution {
  results: Result[];
  logs: Logs;
  error?: ExecutionError;
  executionCount?: number;
  get text(): string | undefined;
  toJSON(): object;
}

Result Processing

Chart Visualization

Built-in support for chart visualization with structured data types for various chart formats.

enum ChartType {
  LINE = 'line',
  SCATTER = 'scatter',
  BAR = 'bar',
  PIE = 'pie',
  BOX_AND_WHISKER = 'box_and_whisker',
  SUPERCHART = 'superchart',
  UNKNOWN = 'unknown'
}

enum ScaleType {
  LINEAR = "linear",
  DATETIME = "datetime",
  CATEGORICAL = "categorical",
  LOG = "log",
  SYMLOG = "symlog",
  LOGIT = "logit",
  FUNCTION = "function",
  FUNCTIONLOG = "functionlog",
  ASINH = "asinh"
}

type ChartTypes = LineChart | ScatterChart | BarChart | PieChart | BoxAndWhiskerChart | SuperChart;

function deserializeChart(data: any): Chart;

Chart Visualization

Base E2B SDK Integration

This package includes all functionality from the base e2b SDK through re-exports.

// Re-exported from 'e2b' package - all base sandbox functionality available
// Including file operations, process management, networking, etc.
// See e2b package documentation for complete base SDK API

Types

Error Handling Types

class ExecutionError {
  constructor(name: string, value: string, traceback: string);
  name: string;
  value: string;
  traceback: string;
}

class OutputMessage {
  constructor(line: string, timestamp: number, error: boolean);
  readonly line: string;
  readonly timestamp: number;
  readonly error: boolean;
  toString(): string;
}

Data Types

type MIMEType = string;

type RawData = {
  [key: MIMEType]: string;
} & {
  data: Record<string, unknown>;
  chart: ChartTypes;
};

type Logs = {
  stdout: string[];
  stderr: string[];
};

Base Chart Types

type Chart = {
  type: ChartType;
  title: string;
  elements: any[];
};

type PointData = {
  label: string;
  points: [number | string, number | string][];
};

type BarData = {
  label: string;
  value: string;
  group: string;
};

type PieData = {
  label: string;
  angle: number;
  radius: number;
};

type BoxAndWhiskerData = {
  label: string;
  min: number;
  first_quartile: number;
  median: number;
  third_quartile: number;
  max: number;
  outliers: number[];
};

type LineChart = Chart & {
  type: ChartType.LINE;
  x_label?: string;
  y_label?: string;
  x_unit?: string;
  y_unit?: string;
  x_ticks: (number | string)[];
  x_scale: ScaleType;
  x_tick_labels: string[];
  y_ticks: (number | string)[];
  y_scale: ScaleType;
  y_tick_labels: string[];
  elements: PointData[];
};

type ScatterChart = Chart & {
  type: ChartType.SCATTER;
  x_label?: string;
  y_label?: string;
  x_unit?: string;
  y_unit?: string;
  x_ticks: (number | string)[];
  x_scale: ScaleType;
  x_tick_labels: string[];
  y_ticks: (number | string)[];
  y_scale: ScaleType;
  y_tick_labels: string[];
  elements: PointData[];
};

type BarChart = Chart & {
  type: ChartType.BAR;
  x_label?: string;
  y_label?: string;
  x_unit?: string;
  y_unit?: string;
  elements: BarData[];
};

type PieChart = Chart & {
  type: ChartType.PIE;
  elements: PieData[];
};

type BoxAndWhiskerChart = Chart & {
  type: ChartType.BOX_AND_WHISKER;
  x_label?: string;
  y_label?: string;
  x_unit?: string;
  y_unit?: string;
  elements: BoxAndWhiskerData[];
};

type SuperChart = Chart & {
  type: ChartType.SUPERCHART;
  elements: Chart[];
};