Skip to main content

Guardrailed Python execution runner with subprocess isolation, timeout, memory caps, and import/global controls

Project description

safe-py-runner

A lightweight Python code runner with guardrails for LLM agent workflows.

GitHub Repository

Why This Package

When building agents that execute generated Python code, you often choose between:

  1. Running code directly in your process (exec) - risky.
  2. Full container sandboxing - heavier and slower.
  3. External sandbox APIs - added cost and latency.

safe-py-runner provides a practical middle path:

  • subprocess isolation
  • timeout enforcement
  • memory limits (POSIX; macOS enforcement can be weaker than Linux)
  • import/builtin policy restrictions
  • JSON-safe input/output handling

Honest scope:

  • Good fit: LLM-generated scripts for your own team and other controlled internal workloads.
  • Not good alone: anonymous public code execution. Use Docker/VM/OS sandboxing as the primary boundary.

Where It Fits

Option Isolation Strength Operational Cost Typical Use
eval / exec in main process Low Low Local scripts, trusted experiments
safe-py-runner Medium Low to medium Internal tools, agent prototypes, controlled workloads
Docker / VM / E2B-style sandbox High Medium to high Production multi-tenant or hostile untrusted code

Production guidance:

  • For hostile public-user code, use Docker/VM/external sandboxing as the primary boundary.
  • safe-py-runner can be used as an extra inner guardrail layer.

It supports two policy modes:

  • restrict (default): block selected symbols.
  • allow: allow only selected symbols.

Backends

The API now uses explicit engine objects through run_code(..., engine=...).

  • LocalEngine: host subprocess execution in managed venv.
  • DockerEngine: managed Docker container execution.

If DockerEngine is used but Docker is unavailable, execution fails with a clear error. DockerEngine can also target remote daemons with docker_context or SSH options.

Docker installation:

For production workloads, prefer DockerEngine over LocalEngine.

Installation

pip install safe-py-runner

Quick Start

from safe_py_runner import LocalEngine, RunnerPolicy, run_code

policy = RunnerPolicy(
    timeout_seconds=5,
    memory_limit_mb=128,
    blocked_imports=["os", "subprocess", "socket"],
)

engine = LocalEngine(venv_dir="/tmp/safe_py_runner_pypi_demo")

result = run_code(
    code="import math\nresult = math.sqrt(input_data['x'])",
    input_data={"x": 81},
    policy=policy,
    engine=engine,
)

# Or load policy from a TOML config file
result = run_code(
    code="result = 1 + 1",
    policy_file="/absolute/path/to/policy.toml",
    engine=engine,
)

if result.ok:
    print(result.result)  # 9.0
else:
    print(result.error)

Security Note

This is not an OS-level sandbox.
For untrusted hostile code, use container/VM isolation in addition to this package.

Memory-limit caveat: RLIMIT_AS is platform-dependent. On macOS, address-space limits may not behave as strictly as Linux.

Common Gotchas

  1. engine is required for run_code(...); there is no implicit backend selection.
  2. policy and policy_file are mutually exclusive; pass only one.
  3. In allow mode, allowed_globals controls both input key injection and extra_globals.
  4. importlib is intentionally blocked in all modes.
  5. Package environments require pinned specs (name==version).

More Information

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safe_py_runner-0.1.7.tar.gz (58.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safe_py_runner-0.1.7-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file safe_py_runner-0.1.7.tar.gz.

File metadata

  • Download URL: safe_py_runner-0.1.7.tar.gz
  • Upload date:
  • Size: 58.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for safe_py_runner-0.1.7.tar.gz
Algorithm Hash digest
SHA256 1153384884a3e1e4bf0e59f0ab4dcbd19639eebbda550949c33dab6304293887
MD5 1004d8666338c371f98f5461b9b79e80
BLAKE2b-256 128932df97bb5de1d0f973a0ef6ff7289d81dc44f57ce9c0157d297abdcd9e0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for safe_py_runner-0.1.7.tar.gz:

Publisher: release.yml on adarsh9780/safe-py-runner

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file safe_py_runner-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: safe_py_runner-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for safe_py_runner-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 9699089926aafe058bbcb6a76cdacdae8fdfc3961ab130f27a964aa4e3528b02
MD5 9444535c093d979b798cddb1075a2174
BLAKE2b-256 ec7834911e39f66620ee496f5d8d2167d9005babedd984466b47d6aebb1547f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for safe_py_runner-0.1.7-py3-none-any.whl:

Publisher: release.yml on adarsh9780/safe-py-runner

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page