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.
Why This Package
When building agents that execute generated Python code, you often choose between:
- Running code directly in your process (
exec) - risky. - Full container sandboxing - heavier and slower.
- 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
- Full documentation and contributor workflow: README.md
- Security policy: SECURITY.md
- Issue tracker: GitHub Issues
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f849199a94c285753624590a846855018254d93a24dbfe8140a229cf2238d004
|
|
| MD5 |
7b632454e3e86a067f235cd7735336ee
|
|
| BLAKE2b-256 |
b1f1172ecc0417e226a61d589f80a4dda1b1e256c975b6871235034a810e16ad
|
Provenance
The following attestation bundles were made for safe_py_runner-0.1.6.tar.gz:
Publisher:
release.yml on adarsh9780/safe-py-runner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
safe_py_runner-0.1.6.tar.gz -
Subject digest:
f849199a94c285753624590a846855018254d93a24dbfe8140a229cf2238d004 - Sigstore transparency entry: 982826649
- Sigstore integration time:
-
Permalink:
adarsh9780/safe-py-runner@d9514db4368e4cc00feeed2e79f63d486ad15e89 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/adarsh9780
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d9514db4368e4cc00feeed2e79f63d486ad15e89 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39e232d1bb20c8f55187b67971256fa8ccdedf05d852f5973f5e41c4bd9fffef
|
|
| MD5 |
6319f68381273c07b8f9edb41142d9e5
|
|
| BLAKE2b-256 |
fac84ae4973f7a22acbcf61eb3bc5a4a0b1f3bd6e12d951f7d1a44c3849bd2a9
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
safe_py_runner-0.1.6-py3-none-any.whl -
Subject digest:
39e232d1bb20c8f55187b67971256fa8ccdedf05d852f5973f5e41c4bd9fffef - Sigstore transparency entry: 982826663
- Sigstore integration time:
-
Permalink:
adarsh9780/safe-py-runner@d9514db4368e4cc00feeed2e79f63d486ad15e89 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/adarsh9780
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d9514db4368e4cc00feeed2e79f63d486ad15e89 -
Trigger Event:
push
-
Statement type: