API Reference¶
Comprehensive API documentation for LLM Sandbox.
Core Classes¶
SandboxSession¶
ArtifactSandboxSession¶
ArtifactSandboxSession ¶
ArtifactSandboxSession(
backend: SandboxBackend = SandboxBackend.DOCKER,
image: str | None = None,
dockerfile: str | None = None,
lang: str = SupportedLanguage.PYTHON,
*,
keep_template: bool = False,
commit_container: bool = False,
verbose: bool = False,
runtime_configs: dict | None = None,
workdir: str | None = "/sandbox",
enable_plotting: bool = True,
security_policy: SecurityPolicy | None = None,
container_id: str | None = None,
**kwargs: Any,
)
Sandbox session with artifact extraction capabilities.
Create a new artifact sandbox session.
The ArtifactSandboxSession provides a secure environment for running code that generates artifacts like plots, images, or other files. It supports multiple container backends (Docker, Kubernetes, Podman) and can capture and extract artifacts from the execution.
| PARAMETER | DESCRIPTION |
|---|---|
backend | Container backend to use (Docker, Kubernetes or Podman) TYPE: |
image | Container image to use (e.g., "vndee/sandbox-python-311-bullseye") TYPE: |
dockerfile | Path to Dockerfile TYPE: |
lang | Programming language (e.g., "python") TYPE: |
keep_template | Whether to keep the container template TYPE: |
commit_container | Whether to commit container changes TYPE: |
verbose | Enable verbose logging TYPE: |
runtime_configs | Additional runtime configurations TYPE: |
workdir | Working directory inside the container TYPE: |
enable_plotting | Whether to enable plot extraction TYPE: |
security_policy | Security policy to enforce TYPE: |
container_id | ID of existing container/pod to connect to TYPE: |
**kwargs | Additional keyword arguments for specific backends (e.g., client for Podman) TYPE: |
| RAISES | DESCRIPTION |
|---|---|
MissingDependencyError | If the required dependency for the chosen backend is not installed |
UnsupportedBackendError | If the chosen backend is not supported |
Examples:
Connect to existing container for artifact generation:
from llm_sandbox import ArtifactSandboxSession, SandboxBackend
from pathlib import Path
import base64
# Connect to existing container
with ArtifactSandboxSession(
container_id='existing-container-id',
lang="python",
verbose=True,
backend=SandboxBackend.DOCKER
) as session:
# Code that generates plots in existing environment
code = '''
import matplotlib.pyplot as plt
import numpy as np
# Generate and plot data
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure()
plt.plot(x, y)
plt.title('Plot from Existing Container')
plt.show()
'''
result = session.run(code)
print(f"Captured {len(result.plots)} plots")
# Save captured plots
for i, plot in enumerate(result.plots):
plot_path = Path("plots") / f"existing_{i + 1:06d}.{plot.format.value}"
with plot_path.open("wb") as f:
f.write(base64.b64decode(plot.content_base64))
Basic usage with Docker backend:
from llm_sandbox import ArtifactSandboxSession, SandboxBackend
from pathlib import Path
import base64
# Create plots directory
Path("plots/docker").mkdir(parents=True, exist_ok=True)
# Run code that generates plots
with ArtifactSandboxSession(
lang="python",
verbose=True,
image="ghcr.io/vndee/sandbox-python-311-bullseye",
backend=SandboxBackend.DOCKER
) as session:
# Example code that generates matplotlib, seaborn, and plotly plots
code = '''
import matplotlib.pyplot as plt
import numpy as np
# Generate and plot data
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure()
plt.plot(x, y)
plt.title('Simple Sine Wave')
plt.show()
'''
result = session.run(code)
print(f"Captured {len(result.plots)} plots")
# Save captured plots
for i, plot in enumerate(result.plots):
plot_path = Path("plots/docker") / f"{i + 1:06d}.{plot.format.value}"
with plot_path.open("wb") as f:
f.write(base64.b64decode(plot.content_base64))
from podman import PodmanClient
# Initialize Podman client
podman_client = PodmanClient(base_url="unix:///path/to/podman.sock")
with ArtifactSandboxSession(
client=podman_client, # Podman specific
lang="python",
verbose=True,
image="ghcr.io/vndee/sandbox-python-311-bullseye",
backend=SandboxBackend.PODMAN
) as session:
result = session.run(code)
Using Kubernetes backend:
with ArtifactSandboxSession(
lang="python",
verbose=True,
image="ghcr.io/vndee/sandbox-python-311-bullseye",
backend=SandboxBackend.KUBERNETES
) as session:
result = session.run(code)
Source code in llm_sandbox/session.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
Functions¶
__enter__ ¶
__exit__ ¶
__exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Exit the context manager.
__getattr__ ¶
clear_plots ¶
Manually clear all plots and reset the plot counter.
This method can be called between runs to clear existing plots without creating a new session.
| RAISES | DESCRIPTION |
|---|---|
Exception | If plot clearing is not supported or fails |
Source code in llm_sandbox/session.py
run ¶
run(
code: str,
libraries: list | None = None,
timeout: float | None = None,
clear_plots: bool = False,
) -> ExecutionResult
Run code in the sandbox session and extract any generated artifacts.
This method executes the provided code in an isolated environment and captures any generated artifacts (e.g., plots, figures). When plotting is enabled, it delegates to the language handler's run_with_artifacts method for language-specific artifact extraction.
| PARAMETER | DESCRIPTION |
|---|---|
code | The code to execute. Can include plotting commands from matplotlib, seaborn, plotly, or other visualization libraries. TYPE: |
libraries | Additional libraries to install before running the code. Defaults to None. TYPE: |
timeout | Timeout in seconds for the code execution. Defaults to the configuration's execution_timeout (typically 60) or 60 if not configured. TYPE: |
clear_plots | Whether to clear existing plots before running the code. Defaults to False. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ExecutionResult | An object containing: - exit_code (int): The exit code of the execution - stdout (str): Standard output from the code execution - stderr (str): Standard error from the code execution - plots (list[Plot]): List of captured plots, each containing: - content_base64 (str): Base64 encoded plot data - format (PlotFormat): Format of the plot (e.g., 'png', 'svg') TYPE: |
| RAISES | DESCRIPTION |
|---|---|
LanguageNotSupportPlotError | If the language does not support plot detection |
Examples:
Basic plotting example:
with ArtifactSandboxSession(
lang="python",
verbose=True,
image="ghcr.io/vndee/sandbox-python-311-bullseye"
) as session:
code = '''
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title('Sine Wave')
plt.show()
'''
result = session.run(code)
print(f"Generated {len(result.plots)} plots")
Multiple plot types and libraries:
code = '''
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import pandas as pd
import numpy as np
# Matplotlib plot
plt.figure(figsize=(10, 6))
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.title('Matplotlib: Sine Wave')
plt.show()
# Seaborn plot
data = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100)
})
sns.scatterplot(data=data, x='x', y='y')
plt.title('Seaborn: Scatter Plot')
plt.show()
# Plotly plot
fig = px.line(data, x='x', y='y', title='Plotly: Line Plot')
fig.show()
'''
result = session.run(code, libraries=['plotly'])
# Save the generated plots
for i, plot in enumerate(result.plots):
with open(f'plot_{i}.{plot.format.value}', 'wb') as f:
f.write(base64.b64decode(plot.content_base64))
Installing additional libraries:
code = '''
import torch
import torch.nn as nn
print(f"PyTorch version: {torch.__version__}")
'''
result = session.run(code, libraries=['torch'])
print(result.stdout)
Clearing plots between runs:
with ArtifactSandboxSession(lang="python") as session:
# First run with plots
plot_code = '''
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 2])
plt.show()
'''
result1 = session.run(plot_code)
print(f"First run: {len(result1.plots)} plots")
# Clear plots and run again
result2 = session.run("print('hello world')", clear_plots=True)
print(f"Second run: {len(result2.plots)} plots")
# Manual plot clearing
session.clear_plots()
result3 = session.run(plot_code)
print(f"Third run: {len(result3.plots)} plots")
Source code in llm_sandbox/session.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
Plot Management¶
The ArtifactSandboxSession class provides built-in support for capturing and managing plots generated by your code. This section covers the plot management features:
Supported Languages: Python, R
Supported Libraries:
- Python: matplotlib, seaborn, plotly, bokeh
- R: base R graphics, ggplot2, plotly, lattice
Key Features:
- Automatic Plot Capture: Plots are automatically captured and returned in the execution result
- Plot Accumulation: By default, plots accumulate across multiple runs within the same session
- Manual Clearing: Use
session.clear_plots()to manually clear all plots - Automatic Clearing: Use
session.run(code, clear_plots=True)to clear plots before execution - Persistent Counter: Plot numbering persists across runs for proper ordering
Methods:
run(code, libraries=None, timeout=None, clear_plots=False): Execute code and capture artifactsclear_plots(): Manually clear all plots and reset the plot counter
Example - Plot Accumulation:
with ArtifactSandboxSession(lang="python", enable_plotting=True) as session:
# Run 1: Generate 2 plots
result1 = session.run("""
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.show()
plt.plot([4, 5, 6])
plt.show()
""")
print(len(result1.plots)) # Output: 2
# Run 2: Generate 1 more plot (accumulates)
result2 = session.run("""
import matplotlib.pyplot as plt
plt.plot([7, 8, 9])
plt.show()
""")
print(len(result2.plots)) # Output: 3 (accumulated)
Example - Manual Clearing:
with ArtifactSandboxSession(lang="python", enable_plotting=True) as session:
result1 = session.run("import matplotlib.pyplot as plt; plt.plot([1,2,3]); plt.show()")
print(len(result1.plots)) # Output: 1
# Manually clear plots
session.clear_plots()
result2 = session.run("import matplotlib.pyplot as plt; plt.plot([4,5,6]); plt.show()")
print(len(result2.plots)) # Output: 1 (reset after clear)
Example - Automatic Clearing:
with ArtifactSandboxSession(lang="r", enable_plotting=True) as session:
result1 = session.run("plot(1:10)")
print(len(result1.plots)) # Output: 1
# Clear automatically before running
result2 = session.run("hist(rnorm(100))", clear_plots=True)
print(len(result2.plots)) # Output: 1 (automatically cleared)
InteractiveSandboxSession¶
InteractiveSandboxSession ¶
InteractiveSandboxSession(
*,
backend: SandboxBackend = SandboxBackend.DOCKER,
lang: str | SupportedLanguage = SupportedLanguage.PYTHON,
kernel_type: KernelType | str = KernelType.IPYTHON,
max_memory: str | None = "1GB",
history_size: int = 1000,
timeout: float | None = 300.0,
runtime_configs: dict[str, Any] | None = None,
**kwargs: Any,
)
Bases: BaseSession
Interactive sandbox session that preserves interpreter state across runs.
This class provides a persistent Python execution environment using an IPython kernel that maintains state across multiple code executions. It supports Docker, Podman, and Kubernetes backends, allowing you to choose the backend that best fits your infrastructure.
Unlike standard SandboxSession which creates a fresh execution context for each run(), InteractiveSandboxSession maintains a persistent interpreter, making it ideal for notebook-style workflows, AI agent interactions, and multi-step data analysis.
Initialize the interactive session.
| PARAMETER | DESCRIPTION |
|---|---|
backend | The sandbox backend to use (docker, podman, or kubernetes) TYPE: |
lang | Programming language (currently only Python is supported for interactive sessions) TYPE: |
kernel_type | Kernel backend used for execution (default: ipython) TYPE: |
max_memory | Optional memory limit TYPE: |
history_size | Number of cached execution entries retained in the kernel TYPE: |
timeout | Default per-cell timeout in seconds TYPE: |
runtime_configs | Backend-specific runtime configurations TYPE: |
**kwargs | Additional keyword arguments to pass to the backend session TYPE: |
| RAISES | DESCRIPTION |
|---|---|
LanguageNotSupportedError | If language other than Python is specified |
UnsupportedBackendError | If the backend is not supported |
Source code in llm_sandbox/interactive.py
Functions¶
close ¶
Close the interactive session.
Source code in llm_sandbox/interactive.py
copy_from_runtime ¶
Copy a file from the backend container.
| PARAMETER | DESCRIPTION |
|---|---|
src | Source path in the container TYPE: |
dest | Destination path on the host TYPE: |
Source code in llm_sandbox/interactive.py
copy_to_runtime ¶
Copy a file to the backend container.
| PARAMETER | DESCRIPTION |
|---|---|
src | Source path on the host TYPE: |
dest | Destination path in the container TYPE: |
Source code in llm_sandbox/interactive.py
execute_command ¶
Execute a command in the backend container.
| PARAMETER | DESCRIPTION |
|---|---|
command | The command to execute TYPE: |
workdir | Optional working directory TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ConsoleOutput | The command output TYPE: |
Source code in llm_sandbox/interactive.py
execute_commands ¶
execute_commands(
commands: list[str | tuple[str, str | None]], workdir: str | None = None
) -> ConsoleOutput
Execute multiple commands in the backend container.
| PARAMETER | DESCRIPTION |
|---|---|
commands | List of commands to execute TYPE: |
workdir | Optional working directory TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ConsoleOutput | The output of the last command TYPE: |
Source code in llm_sandbox/interactive.py
open ¶
Open interactive session and prepare runtime assets.
| RAISES | DESCRIPTION |
|---|---|
ContainerError | If runtime dependencies cannot be installed or runner fails to start |
Source code in llm_sandbox/interactive.py
run ¶
Execute code in the persistent interpreter context.
| PARAMETER | DESCRIPTION |
|---|---|
code | The Python code to execute TYPE: |
libraries | Optional list of libraries to install before execution TYPE: |
timeout | Maximum execution time in seconds TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ConsoleOutput | ConsoleOutput containing stdout, stderr, and exit code |
| RAISES | DESCRIPTION |
|---|---|
NotOpenSessionError | If the session is not open |
ContainerError | If the interactive runtime is not ready |
SandboxTimeoutError | If execution exceeds timeout |
Source code in llm_sandbox/interactive.py
The InteractiveSandboxSession class provides a persistent Python execution environment where state is maintained across multiple run() calls. This is ideal for notebook-style workflows, AI agent interactions, and multi-step data analysis.
Key Features:
- State Persistence: Variables, functions, and imports persist across runs
- IPython Support: Full IPython kernel with magic commands
- Configurable Execution: Customizable timeout, memory limits, and history size
- File-based Communication: Uses a command/result queue system for reliability
Usage Example:
from llm_sandbox import InteractiveSandboxSession
with InteractiveSandboxSession(lang="python") as session:
# Define a variable
session.run("x = 42")
# Use the variable in next execution
result = session.run("print(f'The answer is {x}')")
print(result.stdout) # Output: The answer is 42
IPython Magic Commands:
with InteractiveSandboxSession(lang="python") as session:
# List variables
result = session.run("%who")
# Execute shell commands
result = session.run("!ls -la")
# Time code execution
result = session.run("%%timeit\nsum(range(1000))")
Limitations: - Python language only - IPython kernel only
For detailed usage examples, see the Interactive Sessions Guide.
InteractiveSettings¶
InteractiveSettings dataclass ¶
InteractiveSettings(
kernel_type: KernelType = KernelType.IPYTHON,
max_memory: str | None = None,
history_size: int = 1000,
timeout: float | None = 300.0,
poll_interval: float = 0.1,
)
Configuration for interactive execution.
| ATTRIBUTE | DESCRIPTION |
|---|---|
kernel_type | Kernel backend used for execution (default: TYPE: |
max_memory | Optional memory limit passed to the runtime backend. TYPE: |
history_size | Number of cached execution entries retained in the kernel (default: 1000). TYPE: |
timeout | Default per-cell timeout in seconds; TYPE: |
poll_interval | Interval in seconds for polling runner status files (default: 0.1). TYPE: |
Configuration class for interactive session behavior.
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
kernel_type | KernelType | IPYTHON | Type of kernel to use |
max_memory | str | "1GB" | Memory limit for the session |
history_size | int | 1000 | Number of execution results to cache |
timeout | int | 300 | Per-cell timeout in seconds |
poll_interval | float | 0.1 | Polling interval for results (seconds) |
Usage Example:
from llm_sandbox import InteractiveSandboxSession, InteractiveSettings, KernelType
settings = InteractiveSettings(
kernel_type=KernelType.IPYTHON,
max_memory="2GB",
history_size=500,
timeout=600,
poll_interval=0.1
)
with InteractiveSandboxSession(
lang="python",
interactive_settings=settings
) as session:
result = session.run("print('Hello with custom settings!')")
KernelType¶
KernelType ¶
Bases: StrEnum
Supported kernel types for interactive sessions.
Enumeration of supported kernel types for interactive sessions.
Values:
IPYTHON: IPython kernel (currently the only supported option)
Data Classes¶
ConsoleOutput¶
ConsoleOutput dataclass ¶
Represents the standard output and standard error from code execution or a command.
| ATTRIBUTE | DESCRIPTION |
|---|---|
exit_code | The exit code of the executed code or command. 0 typically indicates success. TYPE: |
stderr | The content written to the standard error stream. TYPE: |
stdout | The content written to the standard output stream. TYPE: |
Functions¶
success ¶
Check if the execution was successful (exit code is 0).
| RETURNS | DESCRIPTION |
|---|---|
bool | True if TYPE: |
text ¶
Get the text representation of the console output (stdout).
.. deprecated:: 0.1.0 The text property is deprecated and will be removed in a future version. Use the stdout attribute directly instead.
| RETURNS | DESCRIPTION |
|---|---|
str | The content of the standard output stream. TYPE: |
Source code in llm_sandbox/data.py
to_json ¶
Get the JSON representation of the execution result.
| PARAMETER | DESCRIPTION |
|---|---|
include_plots | Whether to include the plots in the JSON representation. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | The JSON representation of the execution result. TYPE: |
Source code in llm_sandbox/data.py
ExecutionResult¶
ExecutionResult dataclass ¶
ExecutionResult(
exit_code: int = 0, stderr: str = "", stdout: str = "", plots: list[PlotOutput] = list()
)
Bases: ConsoleOutput
Represents the comprehensive result of code execution within a sandbox session.
This class extends ConsoleOutput to include any plots or other file artifacts that were generated and captured during the execution.
| ATTRIBUTE | DESCRIPTION |
|---|---|
plots | A list of TYPE: |
PlotOutput¶
PlotOutput dataclass ¶
PlotOutput(
format: FileType,
content_base64: str,
width: int | None = None,
height: int | None = None,
dpi: int | None = None,
)
Represents a plot, chart, or other visual artifact output from code execution.
| ATTRIBUTE | DESCRIPTION |
|---|---|
format | The format of the plot (e.g., PNG, SVG, PDF). TYPE: |
content_base64 | The raw content of the plot, base64 encoded. TYPE: |
width | The width of the plot in pixels. Defaults to None. TYPE: |
height | The height of the plot in pixels. Defaults to None. TYPE: |
dpi | The dots per inch (resolution) of the plot. Defaults to None. TYPE: |
Security Classes¶
SecurityPolicy¶
SecurityPolicy ¶
SecurityPattern¶
SecurityPattern ¶
RestrictedModule¶
RestrictedModule ¶
Bases: BaseModel
A dangerous module.
SecurityIssueSeverity¶
SecurityIssueSeverity ¶
Bases: IntEnum
Severity of a security issue.
Enumerations¶
SandboxBackend¶
SandboxBackend ¶
Bases: StrEnum
Enumeration of supported sandbox backend technologies.
Each value represents a different containerization or virtualization technology that can be used to isolate code execution.
SupportedLanguage¶
SupportedLanguage ¶
Bases: StrEnum
Dataclass defining constants for supported programming languages.
Each attribute represents a language identifier string used by the sandbox to select appropriate language handlers and container images.
FileType¶
FileType ¶
Bases: Enum
Enumeration of file types supported by artifact extractors.
This enum lists common file formats that can be generated by code running in the sandbox and subsequently extracted, such as images, data files, and documents.
Functions¶
create_session¶
create_session ¶
create_session(
backend: SandboxBackend = SandboxBackend.DOCKER, *args: Any, **kwargs: Any
) -> BaseSession
Create a new sandbox session for executing code in an isolated environment.
This function creates a sandbox session that supports multiple programming languages and provides features like package installation, file operations, and secure code execution. For backward compatibility, we also keep a SandboxSession alias for this function.
| PARAMETER | DESCRIPTION |
|---|---|
backend | Container backend to use. Options: - SandboxBackend.DOCKER (default) - SandboxBackend.KUBERNETES - SandboxBackend.PODMAN - SandboxBackend.MICROMAMBA TYPE: |
*args | Additional positional arguments passed to the session constructor TYPE: |
**kwargs | Additional keyword arguments passed to the session constructor. Common options include: - lang (str): Programming language ("python", "java", "javascript", "cpp", "go") - verbose (bool): Enable verbose logging - keep_template (bool): Keep the container template - image (str): Custom container image to use - container_id (str): ID of existing container/pod to connect to TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Session | A sandbox session instance for the specified backend TYPE: |
| RAISES | DESCRIPTION |
|---|---|
MissingDependencyError | If the required dependency for the chosen backend is not installed |
UnsupportedBackendError | If the chosen backend is not supported |
Examples:
Connect to existing Docker container:
# Assumes you have a running container with ID 'abc123...'
with SandboxSession(container_id='abc123def456', lang="python") as session:
result = session.run("print('Hello from existing container!')")
print(result.stdout)
# Install libraries in existing container
session.install(["numpy"])
result = session.run("import numpy as np; print(np.random.rand())")
# Execute commands
result = session.execute_command("ls -la")
# Copy files
session.copy_to_runtime("local_file.py", "/container/path/file.py")
Connect to existing Kubernetes pod:
# Assumes you have a running pod with name 'my-pod-abc123'
with SandboxSession(
backend=SandboxBackend.KUBERNETES,
container_id='my-pod-abc123', # pod name
lang="python"
) as session:
result = session.run("print('Hello from existing pod!')")
Connect to existing Podman container:
from podman import PodmanClient
client = PodmanClient()
with SandboxSession(
backend=SandboxBackend.PODMAN,
client=client,
container_id='podman-container-id',
lang="python"
) as session:
result = session.run("print('Hello from existing Podman container!')")
Python session with package installation:
with SandboxSession(lang="python", keep_template=True, verbose=True) as session:
# Basic code execution
result = session.run("print('Hello, World!')")
print(result.stdout) # Output: Hello, World!
# Install and use packages
result = session.run(
"import numpy as np\nprint(np.random.rand())",
libraries=["numpy"]
)
# Install additional packages during session
session.install(["pandas"])
result = session.run("import pandas as pd\nprint(pd.__version__)")
# Copy files to runtime
session.copy_to_runtime("README.md", "/sandbox/data.csv")
Java session:
with SandboxSession(lang="java", keep_template=True, verbose=True) as session:
result = session.run(\"\"\"
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
\"\"\")
JavaScript session with npm packages:
with SandboxSession(lang="javascript", keep_template=True, verbose=True) as session:
# Basic code execution
result = session.run("console.log('Hello, World!')")
# Using npm packages
result = session.run(\"\"\"
const axios = require('axios');
axios.get('https://jsonplaceholder.typicode.com/posts/1')
.then(response => console.log(response.data));
\"\"\", libraries=["axios"])
C++ session:
with SandboxSession(lang="cpp", keep_template=True, verbose=True) as session:
result = session.run(\"\"\"
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::reverse(v.begin(), v.end());
for (int i : v) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
\"\"\", libraries=["libstdc++"])
Go session with external packages:
with SandboxSession(lang="go", keep_template=True, verbose=True) as session:
result = session.run(\"\"\"
package main
import (
"fmt"
"github.com/spyzhov/ajson"
)
func main() {
json := []byte(`{"price": 100}`)
root, _ := ajson.Unmarshal(json)
nodes, _ := root.JSONPath("$..price")
for _, node := range nodes {
node.SetNumeric(node.MustNumeric() * 1.25)
}
result, _ := ajson.Marshal(root)
fmt.Printf("%s", result)
}
\"\"\", libraries=["github.com/spyzhov/ajson"])
Source code in llm_sandbox/session.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
Exceptions¶
The library defines a base exception llm_sandbox.exceptions.SandboxError and various specific exceptions that inherit from it. Please refer to the llm_sandbox.exceptions module for a complete list.
SandboxTimeoutError¶
SandboxTimeoutError ¶
Bases: SandboxError
Raised when an operation times out.
Initialize the TimeoutError.
Source code in llm_sandbox/exceptions.py
Functions¶
Common exceptions include: - ContainerError - SecurityError - ResourceError - ValidationError - LanguageNotSupportedError - ImageNotFoundError - SandboxTimeoutError - Raised when operations exceed configured timeout limits
Language Handlers¶
AbstractLanguageHandler¶
AbstractLanguageHandler ¶
Bases: ABC
Abstract base class for language-specific handlers.
Initialize the language handler.
Source code in llm_sandbox/language_handlers/base.py
Attributes¶
is_support_library_installation property ¶
Get if the language supports library installation.
is_support_plot_detection property ¶
Get if the language supports plot detection.
supported_plot_libraries property ¶
Get supported plotting libraries.
Functions¶
extract_plots ¶
Extract plots from the code.
Base implementation that searches for plot files and extracts them. Languages can override this method to provide custom plot extraction logic.
| PARAMETER | DESCRIPTION |
|---|---|
container | The container protocol instance to run code in TYPE: |
output_dir | Directory where plots should be saved TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list[PlotOutput] | list[PlotOutput]: List of plot outputs |
Source code in llm_sandbox/language_handlers/base.py
filter_comments ¶
Filter out comments from code in a language-specific way.
| PARAMETER | DESCRIPTION |
|---|---|
code | The code to filter comments from. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | The code with comments removed. TYPE: |
Source code in llm_sandbox/language_handlers/base.py
get_execution_commands ¶
Get commands to execute code file.
Source code in llm_sandbox/language_handlers/base.py
get_import_patterns abstractmethod ¶
get_inline_comment_patterns abstractmethod staticmethod ¶
get_library_installation_command ¶
Get command to install library.
get_multiline_comment_patterns abstractmethod staticmethod ¶
inject_plot_detection_code ¶
Inject code to detect and capture plots.
Subclasses should override this method to provide custom plot detection code.
| PARAMETER | DESCRIPTION |
|---|---|
code | The code to inject plot detection code into. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | The code with plot detection code injected. |
Source code in llm_sandbox/language_handlers/base.py
run_with_artifacts ¶
run_with_artifacts(
container: ContainerProtocol,
code: str,
libraries: list | None = None,
enable_plotting: bool = True,
output_dir: str = "/tmp/sandbox_plots",
timeout: int = 30,
) -> tuple[Any, list[PlotOutput]]
Run code and extract artifacts (plots) in a language-specific manner.
This method provides a language-specific implementation for running code with artifact extraction. Languages that support plot detection can override this method to provide custom artifact extraction logic.
| PARAMETER | DESCRIPTION |
|---|---|
container | The container protocol instance to run code in TYPE: |
code | The code to execute TYPE: |
libraries | Optional list of libraries to install before running TYPE: |
enable_plotting | Whether to enable plot detection and extraction TYPE: |
output_dir | Directory where plots should be saved TYPE: |
timeout | Timeout for the code execution TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
tuple | (execution_result, list_of_plots) TYPE: |
Source code in llm_sandbox/language_handlers/base.py
LanguageConfig¶
LanguageConfig dataclass ¶
LanguageConfig(
name: str,
file_extension: str,
execution_commands: list[str],
package_manager: str | None,
is_support_library_installation: bool = True,
plot_detection: PlotDetectionConfig | None = None,
)
Language-specific configuration.
Backend-Specific APIs¶
Docker Backend¶
SandboxDockerSession ¶
SandboxDockerSession(
client: DockerClient | None = None,
image: str | None = None,
dockerfile: str | None = None,
lang: str = SupportedLanguage.PYTHON,
keep_template: bool = False,
commit_container: bool = False,
verbose: bool = False,
stream: bool = False,
runtime_configs: dict | None = None,
workdir: str = "/sandbox",
security_policy: SecurityPolicy | None = None,
default_timeout: float | None = None,
execution_timeout: float | None = None,
session_timeout: float | None = None,
container_id: str | None = None,
skip_environment_setup: bool = False,
**kwargs: Any,
)
Bases: BaseSession
Sandbox session implemented using Docker containers.
This class provides a sandboxed environment for code execution by leveraging Docker. It handles Docker image management (pulling, building from Dockerfile), container creation and lifecycle, code execution, library installation, and file operations within the Docker container.
Initialize Docker session.
| PARAMETER | DESCRIPTION |
|---|---|
client | The Docker client to use. TYPE: |
image | The image to use. TYPE: |
dockerfile | The Dockerfile to use. TYPE: |
lang | The language to use. TYPE: |
keep_template | Whether to keep the template image. TYPE: |
commit_container | Whether to commit the container to a new image. TYPE: |
verbose | Whether to enable verbose output. TYPE: |
stream | Whether to stream the output. TYPE: |
runtime_configs | The runtime configurations to use. TYPE: |
workdir | The working directory to use. TYPE: |
security_policy | The security policy to use. TYPE: |
default_timeout | The default timeout to use. TYPE: |
execution_timeout | The execution timeout to use. TYPE: |
session_timeout | The session timeout to use. TYPE: |
container_id | ID of existing container to connect to. TYPE: |
skip_environment_setup | Skip language-specific environment setup. TYPE: |
**kwargs | Additional keyword arguments. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
None | None |
Source code in llm_sandbox/docker.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
Functions¶
close ¶
Close the Docker sandbox session.
This method cleans up Docker resources by: 1. Committing the container to a new image if commit_container is True. 2. Stopping and removing the running Docker container (only if we created it). 3. Removing the Docker image if is_create_template is True (image was built or pulled during this session), keep_template is False, and the image is not in use by other containers.
Note: When using existing containers, we only disconnect but don't stop/remove the container.
| RAISES | DESCRIPTION |
|---|---|
ImageNotFoundError | If the image to be removed is not found (should not typically occur). |
Source code in llm_sandbox/docker.py
get_archive ¶
Get archive from container.
open ¶
Open Docker session.
This method prepares the Docker environment for code execution by: - Building or pulling the Docker image (if not using existing container) - Creating a container or connecting to existing one - Setting up the environment (if not using existing container)
| RAISES | DESCRIPTION |
|---|---|
ImagePullError | If the image cannot be pulled. |
ImageNotFoundError | If the image cannot be found. |
ContainerError | If existing container cannot be found or accessed. |
Source code in llm_sandbox/docker.py
Kubernetes Backend¶
SandboxKubernetesSession ¶
SandboxKubernetesSession(
client: CoreV1Api | None = None,
image: str | None = None,
lang: str = SupportedLanguage.PYTHON,
verbose: bool = False,
kube_namespace: str = "default",
env_vars: dict[str, str] | None = None,
pod_manifest: dict | None = None,
workdir: str = "/sandbox",
security_policy: SecurityPolicy | None = None,
default_timeout: float | None = None,
execution_timeout: float | None = None,
session_timeout: float | None = None,
container_id: str | None = None,
skip_environment_setup: bool = False,
**kwargs: Any,
)
Bases: BaseSession
Sandbox session implemented using Kubernetes Pods.
This class provides a sandboxed environment for code execution by leveraging Kubernetes. It handles Pod creation and lifecycle based on a provided or default manifest, code execution, library installation, and file operations within the Kubernetes Pod.
Initialize Kubernetes session.
| PARAMETER | DESCRIPTION |
|---|---|
client | The Kubernetes client to use. TYPE: |
image | The image to use. TYPE: |
lang | The language to use. TYPE: |
verbose | Whether to enable verbose output. TYPE: |
kube_namespace | The Kubernetes namespace to use. TYPE: |
env_vars | The environment variables to use. TYPE: |
pod_manifest | The Kubernetes pod manifest to use. TYPE: |
workdir | The working directory to use. TYPE: |
security_policy | The security policy to use. TYPE: |
default_timeout | The default timeout to use. TYPE: |
execution_timeout | The execution timeout to use. TYPE: |
session_timeout | The session timeout to use. TYPE: |
container_id | ID of existing pod to connect to. TYPE: |
skip_environment_setup | Skip language-specific environment setup. TYPE: |
**kwargs | Additional keyword arguments. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
None | None |
Source code in llm_sandbox/kubernetes.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
Functions¶
close ¶
Close Kubernetes session.
Source code in llm_sandbox/kubernetes.py
copy_from_runtime ¶
Override to pass container name for Kubernetes.
Source code in llm_sandbox/kubernetes.py
copy_to_runtime ¶
Override to pass container name for Kubernetes.
Source code in llm_sandbox/kubernetes.py
execute_command ¶
Override to pass container name for Kubernetes.
Source code in llm_sandbox/kubernetes.py
get_archive ¶
Get archive from Kubernetes pod.
Source code in llm_sandbox/kubernetes.py
open ¶
Open Kubernetes session.
Source code in llm_sandbox/kubernetes.py
Podman Backend¶
SandboxPodmanSession ¶
SandboxPodmanSession(
client: PodmanClient | None = None,
image: str | None = None,
dockerfile: str | None = None,
lang: str = SupportedLanguage.PYTHON,
keep_template: bool = False,
commit_container: bool = False,
verbose: bool = False,
mounts: list | None = None,
stream: bool = False,
runtime_configs: dict | None = None,
workdir: str | None = "/sandbox",
security_policy: SecurityPolicy | None = None,
default_timeout: float | None = None,
execution_timeout: float | None = None,
session_timeout: float | None = None,
container_id: str | None = None,
skip_environment_setup: bool = False,
**kwargs: dict[str, Any],
)
Bases: SandboxDockerSession
Sandbox session implemented using Podman containers.
This class provides a sandboxed environment for code execution by leveraging Podman. It inherits from SandboxDockerSession since Podman is designed to be Docker-compatible, only overriding the differences in client initialization and API behavior.
Initialize Podman session.
| PARAMETER | DESCRIPTION |
|---|---|
client | The Podman client to use. TYPE: |
image | The image to use. TYPE: |
dockerfile | The Dockerfile to use. TYPE: |
lang | The language to use. TYPE: |
keep_template | Whether to keep the template image. TYPE: |
commit_container | Whether to commit the container to a new image. TYPE: |
verbose | Whether to enable verbose output. TYPE: |
mounts | The mounts to use. TYPE: |
stream | Whether to stream the output. TYPE: |
runtime_configs | The runtime configurations to use. TYPE: |
workdir | The working directory to use. TYPE: |
security_policy | The security policy to use. TYPE: |
default_timeout | The default timeout to use. TYPE: |
execution_timeout | The execution timeout to use. TYPE: |
session_timeout | The session timeout to use. TYPE: |
container_id | ID of existing container to connect to. TYPE: |
skip_environment_setup | Skip language-specific environment setup. TYPE: |
**kwargs | Additional keyword arguments. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
None | None |
Source code in llm_sandbox/podman.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
Functions¶
open ¶
Open the Podman session with normalized runtime configs.
This method overrides the Docker implementation to normalize runtime configurations for Podman compatibility before opening.
Source code in llm_sandbox/podman.py
Micromamba Backend¶
MicromambaSession ¶
MicromambaSession(
client: DockerClient | None = None,
image: str = "mambaorg/micromamba:latest",
dockerfile: str | None = None,
lang: str = SupportedLanguage.PYTHON,
keep_template: bool = False,
verbose: bool = False,
mounts: list[Mount] | None = None,
environment: str = "base",
commit_container: bool = False,
stream: bool = True,
runtime_configs: dict | None = None,
workdir: str = "/sandbox",
security_policy: SecurityPolicy | None = None,
default_timeout: float | None = None,
execution_timeout: float | None = None,
session_timeout: float | None = None,
container_id: str | None = None,
skip_environment_setup: bool = False,
**kwargs: Any,
)
Bases: SandboxDockerSession
Extends BaseSession to execute commands within a Micromamba environment.
This session leverages a Docker container (typically one with Micromamba pre-installed, like "mambaorg/micromamba:latest") and wraps executed commands with micromamba run to ensure they operate within a specified Micromamba environment.
Reference: https://github.com/vndee/llm-sandbox/pull/3
Initialize a new Micromamba-enabled sandbox session.
| PARAMETER | DESCRIPTION |
|---|---|
client | An existing Docker client instance. If None, a new client is created from the local Docker environment. Defaults to None. TYPE: |
image | The Docker image to use, which should have Micromamba installed. Defaults to "mambaorg/micromamba:latest". TYPE: |
dockerfile | Path to a Dockerfile to build a custom image. The resulting image should have Micromamba. Defaults to None. TYPE: |
lang | The primary programming language. This mainly influences default file extensions for code execution. Defaults to SupportedLanguage.PYTHON. TYPE: |
keep_template | If True, the Docker image will not be removed after the session ends. Defaults to False. TYPE: |
verbose | If True, print detailed log messages. Defaults to False. TYPE: |
mounts | A list of Docker TYPE: |
environment | The name of the Micromamba environment to activate and run commands within (e.g., "base", "my_env"). Defaults to "base". TYPE: |
commit_container | If True, the Docker container's state will be committed to a new image after the session ends. Defaults to False. TYPE: |
stream | If True, the output from TYPE: |
runtime_configs | Additional configurations for the container runtime. Defaults to None. TYPE: |
workdir | The working directory inside the container. Defaults to "/sandbox". TYPE: |
security_policy | The security policy to use for the session. Defaults to None. TYPE: |
default_timeout | The default timeout for the session. Defaults to None. TYPE: |
execution_timeout | The execution timeout for the session. Defaults to None. TYPE: |
session_timeout | The session timeout for the session. Defaults to None. TYPE: |
container_id | ID of existing container to connect to. Defaults to None. TYPE: |
skip_environment_setup | Skip language-specific environment setup. Defaults to False. TYPE: |
**kwargs | Additional keyword arguments. TYPE: |
Source code in llm_sandbox/micromamba.py
Functions¶
Type Hints¶
Protocol Types¶
class ContainerProtocol(Protocol):
"""Protocol for container objects"""
def execute_command(self, command: str, workdir: str | None = None) -> Any:
...
def get_archive(self, path: str) -> tuple:
...
def run(self, code: str, libraries: list | None = None) -> Any:
...
Complete Example¶
from llm_sandbox import (
SandboxSession,
SandboxBackend,
ArtifactSandboxSession,
get_security_policy,
SecurityPolicy,
SecurityPattern,
SecurityIssueSeverity
)
from llm_sandbox.exceptions import SandboxTimeoutError
import base64
# Basic usage
with SandboxSession(lang="python") as session:
result = session.run("print('Hello, World!')")
print(result.stdout)
# With timeout configuration
with SandboxSession(
lang="python",
execution_timeout=30.0, # 30 seconds for code execution
session_timeout=300.0, # 5 minutes session lifetime
default_timeout=10.0 # Default timeout for operations
) as session:
try:
# This will use the execution_timeout (30s)
result = session.run("print('Normal execution')")
# Override timeout for specific execution
result = session.run("""
import time
time.sleep(5)
print('Long operation completed')
""", timeout=15.0) # Override with 15 seconds
except SandboxTimeoutError as e:
print(f"Operation timed out: {e}")
# With security policy
policy = get_security_policy("production")
policy.add_pattern(SecurityPattern(
pattern=r"requests\.get\(.*internal\.company",
description="Internal network access",
severity=SecurityIssueSeverity.HIGH
))
with SandboxSession(
lang="python",
security_policy=policy,
runtime_configs={
"cpu_count": 2,
"mem_limit": "512m",
"timeout": 30
}
) as session:
# Check code safety
code = "import requests; requests.get('https://api.example.com')"
is_safe, violations = session.is_safe(code)
if is_safe:
result = session.run(code, libraries=["requests"])
print(result.stdout)
else:
print("Code failed security check")
# With artifact extraction
with ArtifactSandboxSession(
lang="python",
backend=SandboxBackend.DOCKER
) as session:
result = session.run("""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title('Sine Wave')
plt.show()
""", libraries=["matplotlib", "numpy"])
# Save plots
for i, plot in enumerate(result.plots):
with open(f"plot_{i}.{plot.format.value}", "wb") as f:
f.write(base64.b64decode(plot.content_base64))
# Kubernetes backend
with SandboxSession(
backend=SandboxBackend.KUBERNETES,
lang="python",
kube_namespace="default",
pod_manifest={
"spec": {
"containers": [{
"resources": {
"limits": {
"memory": "512Mi",
"cpu": "1"
}
}
}]
}
}
) as session:
result = session.run("print('Running in Kubernetes!')")
print(result.stdout)