Code Interpreter 用於在 Sandbox 中執行代碼並返回結構化結果。它適合資料分析、Notebook 類體驗、Agent 產生代碼後的即時運行,以及把表格、文本、錯誤資訊和檔案產物回傳給業務系統。
SDK 入口
Code Interpreter 使用獨立 SDK 包:
import { Sandbox } from "@e2b/code-interpreter";Python 使用:
from e2b_code_interpreter import Sandbox串連參數仍然使用 E2B_API_KEY、E2B_API_URL 和 E2B_DOMAIN。雲沙箱內建 Code Interpreter 模板為 code-interpreter-v1,樣本中建議顯式指定該模板。
最小樣本
import os
from e2b_code_interpreter import Sandbox
sandbox = Sandbox.create(
template="code-interpreter-v1",
api_key=os.environ["E2B_API_KEY"],
api_url=os.environ["E2B_API_URL"],
domain=os.environ["E2B_DOMAIN"],
)
try:
execution = sandbox.run_code("print('hello')")
print("".join(execution.logs.stdout or []).strip())
finally:
sandbox.kill()核心對象
sandbox.run_code()/sandbox.runCode():執行代碼並返回執行結果。execution.logs.stdout/execution.logs.stderr:標準輸出和標準錯誤。execution.text:最後一個裸運算式的文本結果。execution.results:富結果清單,可包含文本、表格等結構化結果。execution.error:運行時報錯,包含錯誤類型、值和 traceback。Code Context:在多次執行之間保留變數、匯入和工作目錄。