GuardX
A Python package for code analysis and sandbox.
This library can be used to create pipelines that filter code generated by GenAI code models, and for guarding the execution of generated code.
Quick start
Create a python virtual env
python -m venv .venv
source .venv/bin/activate
Note: Depending on your system, you may need to run as sudo .venv/bin/guardx init.
Podman: GuardX uses the docker python package to communicate with containers. Hence, if you are using Podman, you will need to set the env variable DOCKER_HOST to point to the unix socket used by Podman. See docs/container.rst.
podman machine inspect --format '{{.ConnectionInfo.PodmanSocket.Path}}'
export DOCKER_HOST=unix://<your_podman_socket_location>
Method 1: via CLI
pip install guardx
git+https (using a github personal access token):
pip install git+https://github.com/ibm/guardx.git@{branch/tag}
git+ssh:
pip install git+ssh://git@github.com/ibm/guardx.git@${branch/tag}
The library container images must be built before importing and using the library.
guardx init
Test using provided example
python example.py --file example_gen_code.py
Method 2: for development
Install pre-requisites
git clone git@github.com:ibm/guardx.git
cd guardx
make init
Note: This installs Poetry. Make sure to configure your PATH to access poetry.
Install dependencies
To install the dev dependencies (editable mode):
make install/dev
Note: To add additional dependencies, use poetry add "package". For help, poetry add -h.
Build the library container images
make containers/docker
OR
make containers/podman
Note: Fresh build takes 5-10 minutes. Make sure to update the GuardX config file in resources/config.yaml to match built image name and tag.
Testing
Test modules are created under the tests directory.
To run all tests, use the following command:
make test
Note: To enable logging, set log_cli = true in tests/pytest.ini.
Code Linting
Before checking in any code for the project, please lint the code. This can be done using:
make lint
Precommit Hooks
We are currently using detect-secrets in our precommit hooks. Refer to this repo for configuration instructions
Docs config & build
cd docs
make html
Library Usage
Here is an example of how to use this library in your code.
from guardx import Guardx
from guardx.analysis import AnalysisType
python_code = """<your code here>"""
g = Guardx(config_path="./resources/config.yaml")
# To analyze code
result = g.analyze(python_code, {AnalysisType.DETECT_SECRET, AnalysisType.UNSAFE_CODE})
print(result)
# To execute code in sandbox with a default security policy
result = g.execute(python_code).get_docker_result()
print(result)
# To execute code with global variables passed into the sandbox
globals_dict = {"x": 10, "y": 20}
result = g.execute(python_code, globals=globals_dict).get_docker_result()
print(result)
Passing Global Variables to Sandbox Execution
You can pass global variables into the sandbox execution environment using the globals parameter. This is useful for:
- Providing prior execution state
- Passing configuration or context data
- Simulating stateful execution across multiple code snippets
# Example: Using globals for stateful execution
code1 = "counter = 1"
result1 = g.execute(code1)
# Continue with prior state
code2 = "counter += 1; print(counter)"
result2 = g.execute(code2, globals={"counter": 1})
# Example: Passing complex data structures
code = "result = sum(numbers) * multiplier"
globals_dict = {
"numbers": [1, 2, 3, 4, 5],
"multiplier": 2
}
result = g.execute(code, globals=globals_dict)
# Example: Passing configuration
code = "result = data['value'] * config['multiplier']"
globals_dict = {
"data": {"value": 100},
"config": {"multiplier": 2.5, "threshold": 10}
}
result = g.execute(code, globals=globals_dict)
Important Notes:
- Global variables must be JSON-serializable (strings, numbers, lists, dicts, booleans, None)
- Do NOT include
__builtins__- it is automatically provided by the executor - Non-serializable objects (functions, classes, modules, file handles) will be automatically filtered out with a warning
- Only the serializable values will be passed to the sandbox
Release files for guardx 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| guardx-0.2.2.tar.gz | 27.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| guardx-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 61.3 kB
Release files / guardx-0.2.2.tar.gz
| Download URL | guardx-0.2.2.tar.gz |
|---|---|
| Size | 27.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
845492fe46bf693cd275f302b570e61dada88fd441a8db308fb67fc6eda5c6c3
|
|
BLAKE2b-256 checksum How to use checksums |
01bb97e57c976823ba7841d89d40b65d89144c4cdf583472d9884745ccbf57c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 7, 2026.
Transparency logRelease files / guardx-0.2.2-py3-none-any.whl
| Download URL | guardx-0.2.2-py3-none-any.whl |
|---|---|
| Size | 33.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5eefac7f1fe51586d9b840320d24ea3e455ff1f10efadf42ce43dd8fb13f9fb1
|
|
BLAKE2b-256 checksum How to use checksums |
0eeecee02724b95e503bf9dce82ec40c2e99c1b7760139c17a4b22302a49a4ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 7, 2026.
Transparency log