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, secure-by-default Python code runner designed for LLM agents.

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)
  • secure-by-default import/builtin restrictions
  • JSON-safe input/output handling

It supports two policy modes:

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

Installation

pip install safe-py-runner

Quick Start

from safe_py_runner import RunnerPolicy, run_code

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

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

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

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.

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.6.tar.gz (35.3 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.6-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: safe_py_runner-0.1.6.tar.gz
  • Upload date:
  • Size: 35.3 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.6.tar.gz
Algorithm Hash digest
SHA256 f849199a94c285753624590a846855018254d93a24dbfe8140a229cf2238d004
MD5 7b632454e3e86a067f235cd7735336ee
BLAKE2b-256 b1f1172ecc0417e226a61d589f80a4dda1b1e256c975b6871235034a810e16ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for safe_py_runner-0.1.6.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.6-py3-none-any.whl.

File metadata

  • Download URL: safe_py_runner-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 9.2 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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 39e232d1bb20c8f55187b67971256fa8ccdedf05d852f5973f5e41c4bd9fffef
MD5 6319f68381273c07b8f9edb41142d9e5
BLAKE2b-256 fac84ae4973f7a22acbcf61eb3bc5a4a0b1f3bd6e12d951f7d1a44c3849bd2a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for safe_py_runner-0.1.6-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