litellm/tests/integration/sandbox/test_e2b_sandbox.py
Krrish Dholakia 53593f697d
feat(sandbox): e2b code execution primitive (#30898)
* feat(sandbox): add e2b code execution primitive

Add a provider-agnostic code execution primitive that runs model-generated
code in an isolated sandbox and returns the output, with e2b as the first
backend over raw httpx (no SDK dependency).

Public API: litellm.acode_interpreter_tool (ephemeral create -> run -> delete)
plus the low-level lifecycle litellm.acreate_sandbox / arun_code /
adelete_sandbox. Each is @client-decorated so operations are logged like
litellm.asearch. Backends implement BaseSandboxConfig; resolved via
ProviderConfigManager.get_provider_sandbox_config.

* fix(sandbox): address review feedback and CI gates

- document e2b provider in provider_endpoints_support.json and add a sandbox endpoint definition
- regenerate dashboard CallTypes after the sandbox call-type additions
- guard explicit timeout=0 instead of coercing it to the default
- require a ContainerHandle access token before running code; reject bare-id runs
- return False on a 404 delete now that the shared http handler raises for status
- skip non-JSON NDJSON lines and cap streamed output to bound memory
- move the real-network integration tests out of tests/test_litellm into tests/integration/sandbox

* fix(sandbox): satisfy strict ruff gate and scope star-exports

- modernize annotations in the new sandbox modules to PEP 585/604 (list/dict,
  X | None) and drop the now-unnecessary quoted forward refs so the strict-rule
  budget delta for UP006/UP037/UP045 returns to zero
- add __all__ to litellm/sandbox/main.py so 'import *' only re-exports the four
  public entrypoints instead of leaking module-level imports

* fix(sandbox): drop quotes on sandbox config return annotation

utils.py uses 'from __future__ import annotations', so the quoted forward ref
tripped UP037; the unquoted union is lazily evaluated and keeps the strict-rule
delta at zero

* chore(sandbox): re-trigger automated review after addressing feedback
2026-06-20 16:30:01 -07:00

39 lines
1.2 KiB
Python

"""
e2b code execution sandbox - end-to-end integration tests.
These tests make REAL HTTP calls to the e2b API and are skipped automatically
unless E2B_API_KEY is set. Mock-only unit tests live in
tests/test_litellm/sandbox/test_e2b_sandbox.py.
Run only these tests:
pytest tests/integration/sandbox/test_e2b_sandbox.py -v
"""
import os
import pytest
import litellm
@pytest.mark.skipif("E2B_API_KEY" not in os.environ, reason="needs a real E2B_API_KEY")
@pytest.mark.asyncio
async def test_integration_ephemeral_real_e2b():
result = await litellm.acode_interpreter_tool(
provider="e2b", code="print(sum(range(10)))"
)
assert result.stdout.strip() == "45"
assert result.error is None
@pytest.mark.skipif("E2B_API_KEY" not in os.environ, reason="needs a real E2B_API_KEY")
@pytest.mark.asyncio
async def test_integration_lifecycle_roundtrip_real_e2b():
container = await litellm.acreate_sandbox(provider="e2b")
try:
result = await litellm.arun_code(
provider="e2b", container=container, code="print(6*7)"
)
assert result.stdout.strip() == "42"
finally:
assert await litellm.adelete_sandbox(provider="e2b", container=container)