# Streaming (/docs/code-interpreting/streaming)

<!-- agent-signals: reading_time_min: 2 · est_tokens: 1383 · updated: 2026-07-30 -->
Related: [Analyze data with AI](/docs/code-interpreting/analyze-data-with-ai.md), [Code contexts](/docs/code-interpreting/contexts.md), [Create charts & visualizations](/docs/code-interpreting/create-charts-visualizations.md), [Customize the template](/docs/code-interpreting/customize-template.md), [Supported languages](/docs/code-interpreting/supported-languages.md)

E2B Code Interpreter SDK allows you to stream the output, and results when executing code in the sandbox.

## Stream `stdout` and `stderr` [#stream-stdout-and-stderr]

When using the `runCode()` method in JavaScript or `run_code()` in Python you can pass `onStdout`/`on_stdout` and `onStderr`/`on_stderr` callbacks to handle the output.

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js highlight={15-17}  
      import { Sandbox } from '@e2b/code-interpreter'

      const codeToRun = `
        import time
        import sys
        print("This goes first to stdout")
        time.sleep(3)
        print("This goes later to stderr", file=sys.stderr)
        time.sleep(3)
        print("This goes last")
      `
      const sandbox = await Sandbox.create()
      sandbox.runCode(codeToRun, {
        // Use `onError` to handle runtime code errors
        onError: error => console.error('error:', error),
        onStdout: data => console.log('stdout:', data),
        onStderr: data => console.error('stderr:', data),
      })
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python highlight={17-19}  
      from e2b_code_interpreter import Sandbox

      code_to_run = """
        import time
        import sys
        print("This goes first to stdout")
        time.sleep(3)
        print("This goes later to stderr", file=sys.stderr)
        time.sleep(3)
        print("This goes last")
      """

      sandbox = Sandbox.create()
      sandbox.run_code(
        code_to_run,
        # Use `on_error` to handle runtime code errors
        on_error=lambda error: print('error:', error),
        on_stdout=lambda data: print('stdout:', data),
        on_stderr=lambda data: print('stderr:', data),
      )
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

The code above will print the following:

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```javascript  
      stdout: {
        error: false,
        line: "This goes first to stdout\n",
        timestamp: 1729049666861000,
      }
      stderr: {
        error: true,
        line: "This goes later to stderr\n",
        timestamp: 1729049669924000,
      }
      stdout: {
        error: false,
        line: "This goes last\n",
        timestamp: 1729049672664000,
      }
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```bash  
      stdout: This goes first to stdout

      stderr: This goes later to stderr

      stdout: This goes last
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

## Stream `results` [#stream-results]

When using the `runCode()` method in JavaScript or `run_code()` in Python you can pass `onResults`/`on_results` callback
to receive results from the sandbox like charts, tables, text, and more.

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js highlight={20}  
      const codeToRun = `
      import matplotlib.pyplot as plt

      # Prepare data
      categories = ['Category A', 'Category B', 'Category C', 'Category D']
      values = [10, 20, 15, 25]

      # Create and customize the bar chart
      plt.figure(figsize=(10, 6))
      plt.bar(categories, values, color='green')
      plt.xlabel('Categories')
      plt.ylabel('Values')
      plt.title('Values by Category')

      # Display the chart
      plt.show()
      `
      const sandbox = await Sandbox.create()
      await sandbox.runCode(codeToRun, {
        onResult: result => console.log('result:', result),
      })
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python highlight={24}  
      from e2b_code_interpreter import Sandbox

      code_to_run = """
      import matplotlib.pyplot as plt

      # Prepare data
      categories = ['Category A', 'Category B', 'Category C', 'Category D']
      values = [10, 20, 15, 25]

      # Create and customize the bar chart
      plt.figure(figsize=(10, 6))
      plt.bar(categories, values, color='green')
      plt.xlabel('Categories')
      plt.ylabel('Values')
      plt.title('Values by Category')

      # Display the chart
      plt.show()
      """

      sandbox = Sandbox.create()
      sandbox.run_code(
        code_to_run,
        on_result=lambda result: print('result:', result),
      )
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>
