E2B Code Interpreter

repository·main·Indexed 25 days ago

https://github.com/e2b-dev/code-interpreter

Open-source infrastructure for running AI-generated code in secure, isolated cloud sandboxes. Provides SDKs for JavaScript/TypeScript (@e2b/code-interpreter) and Python (e2b_code_interpreter) to programmatically execute code via the Sandbox class. Includes utilities like @e2b/data-extractor for retrieving structured data from pandas DataFrames and Matplotlib plots, and tools for building and debugging custom sandbox templates.

Tokens
7.4K
Snippets
22
Records
55
Agent score
78%

What's inside e2b-code-interpreter

  1. Use @e2b/data-extractor to extract data for the Code Interpreter SDK

    main
    The @e2b/data-extractor package is a utility designed to extract structured data from objects within the Code Interpreter SDK environment. It is specifically useful for retrieving data from complex objects like pandas DataFrames or matplotlib plots, making them accessible for further processing or analysis outside the sandbox.
  2. Create a custom sandbox template

    main

    You can create a customized version of the Code Interpreter sandbox (e.g., to add preinstalled packages) by following these steps:

    1. Install dependencies:

      pip install e2b dotenv
    2. Define the template in a file (e.g., template.py) using the from_template method to inherit from the base code-interpreter-v1:

      from e2b import Template
      template = Template().from_template("code-interpreter-v1")
    3. Create a build script (e.g., build.py) to execute the build process. You can specify an alias, cpu_count, memory_mb, and a logger:

      from dotenv import load_dotenv
      from .template import template
      from e2b import Template, default_build_logger
      
      load_dotenv()
      
      Template.build(
          template,
          alias="code-interpreter-custom",
          cpu_count=2,
          memory_mb=2048,
          on_build_logs=default_build_logger(),
      )
    4. Configure environment variables in a .env file:

      E2B_API_KEY=e2b_***
    5. Run the build:

      python build.py
    6. Use the custom template in your application by referencing the alias provided during the build:

      from e2b import Sandbox
      
      sbx = Sandbox.create(template="code-interpreter-custom")
      execution = sbx.run_code("print('Hello, World!')")
      print(execution.logs.stdout)
    from e2b import Sandbox
    
    sbx = Sandbox.create(template="code-interpreter-custom")
    execution = sbx.run_code("print('Hello, World!')")
    print(execution.logs.stdout)
  3. Build the official code-interpreter-v1 production template

    main

    To build the official code-interpreter-v1 template from this repository, use the build_prod.py script. This process requires development dependencies and an E2B API key.

    1. Install build dependencies:
      pip install -r requirements-dev.txt
    2. Configure your .env file with your E2B_API_KEY.
    3. Run the build script:
      python build_prod.py

    To force a clean rebuild that ignores the layer cache, set the SKIP_CACHE environment variable to true.

    SKIP_CACHE=true python build_prod.py
  4. Handle rich data with the Result class

    main

    The Result class captures multi-modal output from a cell, similar to Jupyter/IPython execution semantics. It can hold various data formats including:

    • Textual: text, html, markdown, latex, javascript
    • Visual: svg, png, jpeg, pdf, chart
    • Structured: json, data (dict)

    Use the formats() method to see which MIME types are available in a given result. The is_main_result flag distinguishes the primary output of a cell from secondary display calls.

  5. How chart types are detected from Matplotlib Axes

    main

    The library identifies the type of chart by inspecting the _children of a Matplotlib Axes object and filtering out Text objects and grid lines. The following mapping is used:

    Detected ArtistsResulting ChartType
    All Line2D (excluding grid lines)ChartType.LINE
    All PathCollectionChartType.SCATTER
    All WedgeChartType.PIE
    All RectangleChartType.BAR
    All PathPatch or Line2DChartType.BOX_AND_WHISKER
    OtherChartType.UNKNOWN
  6. Understand the Execution result structure

    main

    The Execution object represents the complete outcome of a code cell execution. It aggregates several components:

    • results: A list of Result objects containing rich data (text, images, plots, etc.) produced by the cell.
    • logs: A Logs object containing stdout and stderr strings.
    • error: An optional ExecutionError if the cell failed.
    • execution_count: An optional integer representing the cell's execution order.

    You can access a quick text representation of the execution via the execution.text property, which returns the text of the is_main_result.

  7. Handle execution and request timeouts

    main

    The run_code method distinguishes between two types of timeouts:

    1. Execution Timeout (timeout): The maximum time allowed for the code itself to run (in seconds).
    2. Request Timeout (request_timeout): The maximum time allowed for the network request to the sandbox (in seconds).

    If an execution timeout occurs, it raises a formatted error via format_execution_timeout_error(). If a network request times out, it raises format_request_timeout_error().