---
description: Reference for the code interpreter extension on @cloudflare/sandbox@next.
title: Interpreter
image: https://developers.cloudflare.com/og-docs.png
---

[Skip to content](#main-content)

> Documentation Index  
> Fetch the complete documentation index at: https://developers.cloudflare.com/sandbox/llms.txt  
> Use this file to discover all available pages before exploring further.

# Interpreter

Last updated Aug 24, 2026|Copy as Markdown|[View as Markdown](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/sandbox/1-0-preview/api/interpreter/index.md)|[Agent setup](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/agent-setup/)

Path to Sandbox SDK 1.0

This page documents `@cloudflare/sandbox/interpreter` on `@cloudflare/sandbox@next`. For the current stable package, refer to [Interpreter API](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/sandbox/api/interpreter/).

Methods live on `sandbox.interpreter` after you attach `withInterpreter` on your `Sandbox` subclass. Method names match the stable interpreter; `runCode` returns plain serializable data. Attach and first run: [Code interpreter](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/sandbox/1-0-preview/interpreter/).

## `createCodeContext()`

```ts
createCodeContext(options?: CreateContextOptions): Promise<CodeContext>
```

### `CreateContextOptions`

`createCodeContext` accepts the following options:

| Field    | Type                     | Description                             |                                        |
| -------- | ------------------------ | --------------------------------------- | -------------------------------------- |
| language | "python" \| "javascript" | "typescript"                            | Interpreter language. Default: python. |
| cwd      | string                   | Working directory. Default: /workspace. |                                        |

### `CodeContext`

A created context has the following fields:

| Field     | Type   | Description                         |
| --------- | ------ | ----------------------------------- |
| id        | string | Context id in the current container |
| language  | string | Language of the context             |
| cwd       | string | Working directory                   |
| createdAt | Date   | Created time                        |
| lastUsed  | Date   | Last used time                      |

## `runCode()`

```ts
runCode(code: string, options?: RunCodeOptions): Promise<ExecutionResult>
```

### `RunCodeOptions`

`runCode` accepts the following options. The callback fields apply to `runCode` only.

| Field    | Type                                             | Description                                                         |                                                                     |
| -------- | ------------------------------------------------ | ------------------------------------------------------------------- | ------------------------------------------------------------------- |
| context  | CodeContext                                      | Context to use. If omitted, a default context for language is used. |                                                                     |
| language | "python" \| "javascript"                         | "typescript"                                                        | Used when creating or selecting a default context. Default: python. |
| onStdout | (output: OutputMessage) => void \| Promise<void> | Called for stdout chunks while running                              |                                                                     |
| onStderr | (output: OutputMessage) => void \| Promise<void> | Called for stderr chunks while running                              |                                                                     |
| onResult | (result: ResultData) => void \| Promise<void>    | Called for rich results (plain data)                                |                                                                     |
| onError  | (error: ExecutionError) => void \| Promise<void> | Called when the run reports an execution error                      |                                                                     |

### `OutputMessage`

```ts
interface OutputMessage {
	text: string;
	timestamp: number;
}
```

### `ExecutionResult`

```ts
interface ExecutionResult {
	code: string;
	logs: {
		stdout: string[];
		stderr: string[];
	};
	error?: ExecutionError;
	executionCount?: number;
	results: ResultData[];
}
```

`ResultData` may include plain fields such as `text`, `html`, `png`, `jpeg`, `svg`, `latex`, `markdown`, `json`, and `chart` when the runtime produces them.

### `ExecutionError`

```ts
interface ExecutionError {
	name: string;
	message: string;
	traceback: string[];
	lineNumber?: number;
}
```

## `runCodeStream()`

```ts
runCodeStream(
	code: string,
	options?: RunCodeOptions,
): Promise<ReadableStream<Uint8Array>>
```

Returns an SSE byte stream of execution events. The TypeScript type reuses `RunCodeOptions` for `context` and `language`, but the stream path does **not** invoke `onStdout`, `onStderr`, `onResult`, or `onError` — consume the SSE body instead. Canceling the stream may interrupt the in-flight run.

## `listCodeContexts()`

```ts
listCodeContexts(): Promise<CodeContext[]>
```

## `deleteCodeContext()`

```ts
deleteCodeContext(contextId: string): Promise<void>
```

## Errors

Interpreter failures may surface as `InterpreterNotReadyError`, `ContextNotFoundError`, or `CodeExecutionError`. Refer to [Errors API](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/sandbox/1-0-preview/api/errors/) and [Errors and recovery](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/sandbox/1-0-preview/errors/).

Python requires the **`-python`** container image variant. Deploy the Worker package and container image from the same preview line.

## Related

* [Code interpreter](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/sandbox/1-0-preview/interpreter/)
* [Extensions](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/sandbox/1-0-preview/extensions/)
* [Errors API](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/sandbox/1-0-preview/api/errors/)
* Stable: [Interpreter API](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/sandbox/api/interpreter/)

Was this helpful?

YesNo

## On this page

[![](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/_astro/logo.te5VL_aD.svg)Docs](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/)

```json
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/sandbox/1-0-preview/api/interpreter/#page","headline":"Interpreter · Cloudflare Sandbox SDK docs","description":"Reference for the code interpreter extension on @cloudflare/sandbox@next.","url":"https://developers.cloudflare.com/sandbox/1-0-preview/api/interpreter/","inLanguage":"en","image":"https://developers.cloudflare.com/og-docs.png","dateModified":"2026-08-24","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
```
