Skip to main content

opa-py-wasm

Supported Python versions CI Coverage PyPI License: MIT

opa-py-wasm is an in-process, thread-safe Python SDK for evaluating Open Policy Agent policies compiled to WebAssembly, built on wasmtime.py. It requires no OPA server, no OPA binary, no subprocess, and no network at runtime. It is functionally comparable to StyraOSS opa-java-wasm, with a Pythonic API and explicit thread-safety guarantees.

Import name: the package installs as opa-py-wasm and is imported as opapywasm.

Installation

pip install opa-py-wasm

Or with uv:

uv add opa-py-wasm

Requires Python 3.10 or newer. The only runtime dependency is wasmtime.

Usage

from opapywasm import UNDEFINED, OpaWasmPolicy, PolicyConfig

policy = OpaWasmPolicy.from_wasm_file(
    "policy.wasm",
    config=PolicyConfig(
        pool_size=8,
        default_entrypoint="authz/allow",
        eval_timeout_seconds=30.0,   # per-eval deadline (epoch interruption)
        max_memory_pages=4096,       # per-instance linear-memory cap (256 MiB)
    ),
)

policy.set_data({"roles": {"alice": "admin", "bob": "viewer"}})

decision = policy.evaluate({"user": "alice", "action": "delete"})   # simplified decision
raw = policy.evaluate_raw({"user": "alice", "action": "delete"})    # full OPA result set

# `evaluate` distinguishes an undefined decision from a defined JSON null:
if decision is UNDEFINED:
    ...          # policy produced no result
elif decision is None:
    ...          # policy result was JSON null

# Custom host builtins (receive decoded Python args, return JSON-compatible values):
policy.register_builtin("my.custom", lambda x: {"ok": True})

OpaWasmPolicy is a bounded, thread-safe pool — construct it once and call evaluate from many threads. Runnable scripts are in examples/: basic_authz.py, pooled_eval.py, custom_builtin.py, yaml_builtin.py.

For the YAML default builtins, install the optional extra:

uv add "opa-py-wasm[yaml]"

Start with the user guide — an end-to-end walkthrough covering compiling a policy, result semantics, concurrency, data updates, memory behaviour, host builtins, and a production adoption checklist.

See docs/ for the focused references: architecture, thread-safety, the OPA Wasm ABI, the opa-java-wasm parity table, and capacity-planning guidance for sizing pool size and concurrency.

Test fixtures (dev only)

Compiled .wasm fixtures are committed under tests/fixtures/wasm, so the test suite needs no OPA CLI. To regenerate them from the Rego sources in tests/fixtures/rego, install the OPA CLI and run:

python scripts/build_fixtures.py           # all fixtures
python scripts/build_fixtures.py allow_true # a single fixture

Local Development

uv

This library uses uv to manage Python dependencies.
brew is The easiest way to install on macOS:

brew install uv

For additional installation options (e.g. setting the PATH, installing a specific version, etc), see the installation docs:
https://docs.astral.sh/uv/getting-started/installation/

Python Versions Supported

Python 3.10 through 3.14 are supported and tested in CI.

To change which versions are supported and tested:

Virtual Environment

Create by running:

uv sync --all-extras

Run a command from the virtual environment, like code formatting:

uv run black .

To activate the virtual environment:

source .venv/bin/activate

For more information, refer to uv's documentation.

Type checking (mypy)

This library supports Python type annotation. Types will be checked as part of the test suite (see below). For more information, see the mypy documentation.

Testing

To run the test suite locally:

uv run tox

Coverage is enforced on every CI run by fail_under = 90 in tool.coverage.report; the suite currently sits at 99%. The coverage badge at the top of this file is static — if you change coverage materially, update the percentage in that badge along with your change.

Releasing

Releases are cut by maintainers by pushing a vX.Y.Z tag, which triggers an automated build and publish to PyPI. See RELEASING.md for the process and the versioning policy. Published versions are listed on PyPI, and each release's changes are in CHANGELOG.md.

Contributing

Contributions are welcome — see the Contribution Guidelines and our Code of Conduct.

Support

Please open a GitHub issue for bugs and feature requests, or start a discussion for questions.

License

Released under the MIT License.

Release files for opa-py-wasm 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opa-py-wasm 1.2.1
File Size Uploaded
opa_py_wasm-1.2.1.tar.gz 40.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for opa-py-wasm 1.2.1
File Interpreter ABI Platform
opa_py_wasm-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 88.6 kB

Release files / opa_py_wasm-1.2.1.tar.gz

Download URL opa_py_wasm-1.2.1.tar.gz
Size 40.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6748e7ab9371cef7a905d763416b506e65b3d4685094b0a7571f40f2f6d81fa9
BLAKE2b-256 checksum
How to use checksums
51747af80a9cb001c7fbbbf7bf7cfea1847172d1f7b7b15d275fb27863d1f701
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 16, 2026.

Transparency log

Release files / opa_py_wasm-1.2.1-py3-none-any.whl

Download URL opa_py_wasm-1.2.1-py3-none-any.whl
Size 48.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c9093ee3de8adb8bcc78191a48df76ba5bb2478c0c8ef2accccb94baea807d9
BLAKE2b-256 checksum
How to use checksums
00ffc57cb653f26015d99b59c7edf53ec2946341088101e8dcaf45ef08f001b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 16, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page