Skip to content

feat: add python e2b sandbox example#29

Merged
xxchan merged 6 commits intomainfrom
feat/python-e2b-sandbox
Jan 27, 2026
Merged

feat: add python e2b sandbox example#29
xxchan merged 6 commits intomainfrom
feat/python-e2b-sandbox

Conversation

@xxchan
Copy link
Collaborator

@xxchan xxchan commented Jan 21, 2026

Summary

  • Add a self-contained Python example that runs Kimi Agent SDK inside an E2B sandbox.
  • Vendor the E2BKaos (KAOS backend) implementation into the example as a reference for extending KAOS: examples/python/e2b-sandbox/e2b_kaos.py.
  • Wire the example to use the vendored backend (no dependency on kaos.contrib.e2b / no kimi-cli dev-branch pin).

Files

  • examples/python/e2b-sandbox/e2b_kaos.py: vendored E2BKaos backend
  • examples/python/e2b-sandbox/main.py: set_current_kaos(E2BKaos(...))
  • examples/python/e2b-sandbox/README.md: setup & run
  • README.md: list Python examples

Reference

Testing

  • cd python && uv sync --frozen && uv run ruff check && uv run pyright && uv run pytest -q
  • E2B runtime not exercised locally (requires E2B_API_KEY + a live sandbox)

@xxchan xxchan force-pushed the feat/python-e2b-sandbox branch from 5272a96 to 2c6f8c4 Compare January 26, 2026 10:07
Signed-off-by: tianxiao <shentianxiao@moonshot.cn>
@xxchan xxchan marked this pull request as ready for review January 26, 2026 10:43
@xxchan xxchan requested review from RealKai42 and stdrc January 26, 2026 13:44
@RealKai42 RealKai42 requested a review from Copilot January 26, 2026 15:17
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a self-contained Python example showing how to run the Kimi Agent SDK inside an E2B sandbox by vendoring an E2BKaos KAOS backend and wiring it into the example.

Changes:

  • Add an E2BKaos implementation that adapts KAOS filesystem/process APIs to an E2B AsyncSandbox.
  • Add an example driver script, agent configuration, and example-local pyproject.toml to run the SDK in an E2B sandbox.
  • Update the top-level README to list the new Python example alongside existing Go examples.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
examples/python/e2b-sandbox/pyproject.toml Defines an example-local project, Python version, and dependencies (including e2b and the local kimi-agent-sdk source) to make the sandbox example self-contained.
examples/python/e2b-sandbox/main.py Implements the async entrypoint that validates E2B_API_KEY, configures workdir, creates an E2B sandbox, installs E2BKaos as the current KAOS backend, and runs the SDK prompt loop.
examples/python/e2b-sandbox/e2b_kaos.py Provides the E2BKaos KAOS backend, mapping KAOS operations (paths, stat, file IO, mkdir, exec) onto the E2B sandbox APIs, with async process/stdin/stdout handling.
examples/python/e2b-sandbox/agent.yaml Configures an e2b agent profile extending the default and disables the Grep tool for non-local KAOS usage.
examples/python/e2b-sandbox/README.md Documents KAOS, explains how E2B integration works, provides a code snippet, and adds end-to-end run instructions for the example.
README.md Adds a concise “Examples” section linking to existing Go examples and the new Python E2B sandbox example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +39 to +52
```python
from e2b import AsyncSandbox
from kaos import set_current_kaos
from kaos.path import KaosPath
from kimi_agent_sdk import prompt

# Create an E2B sandbox and install as KAOS backend
sandbox = await AsyncSandbox.create()
set_current_kaos(E2BKaos(sandbox, cwd="/home/user"))

# Agent tools now operate inside the sandbox
async for msg in prompt("Create a Python project", work_dir=KaosPath("/home/user")):
print(msg.extract_text(), end="")
```
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the "How It Works" code snippet, E2BKaos is used but never imported, so readers copying this snippet as-is will get a NameError. Please add the missing import (e.g., importing E2BKaos from e2b_kaos) to keep the example self-contained and runnable.

Copilot uses AI. Check for mistakes.
uv run main.py
```

If `E2B_SANDBOX_ID` is not set, the script creates a sandbox and prints the ID. The sandbox lifecycle is managed outside of the SDK.
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The README suggests that setting E2B_SANDBOX_ID will cause the script to connect to an existing sandbox ("If E2B_SANDBOX_ID is not set, the script creates a sandbox..."), but main.py's _get_sandbox currently ignores E2B_SANDBOX_ID and always calls AsyncSandbox.create. Please either implement the E2B_SANDBOX_ID connect flow in _get_sandbox (as hinted in the commented-out code) or update this section of the README to reflect the current behavior so users are not misled.

Suggested change
If `E2B_SANDBOX_ID` is not set, the script creates a sandbox and prints the ID. The sandbox lifecycle is managed outside of the SDK.
This example currently always creates a new sandbox instance and prints its ID; `E2B_SANDBOX_ID` is reserved for future use, and the sandbox lifecycle is managed outside of the SDK.

Copilot uses AI. Check for mistakes.
@xxchan xxchan merged commit 4174109 into main Jan 27, 2026
8 checks passed
@xxchan xxchan deleted the feat/python-e2b-sandbox branch January 27, 2026 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants