Skip to main content

Mini Rule Engine (rule-engine-mini)

Deterministic AST business rules evaluator in pure Python.
Zero eval() • Zero dependencies • Sub-microsecond latency • Bulletproof JSON Logic.

PyPI Version Python Versions 100% Core Coverage Zero Dependencies Zero eval Sub-microsecond Speed License: MIT


⚡ The Problem: Business Rules Shouldn't Risk Remote Code Execution

Every backend eventually needs dynamic rules:

  • Fintech: "Flag transactions over $5,000 originating outside MX or US."
  • E-commerce: "Apply VIP discount if cart > $100 and loyalty tier is Gold."
  • Healthtech: "Trigger urgent triage if oxygen saturation < 90% or heart rate > 120."

Most teams face a frustrating dilemma:

  1. The Naive Trap (eval() or exec()): You store Python expressions in the database. One unescaped input later, an attacker gets a shell on your production cluster (RCE).
  2. The Heavyweight Trap (Enterprise Rule Engines): You pull in massive DSLs or bloated frameworks with 50 transitive dependencies and a steep learning curve.

The Solution: rule-engine-mini

A lightweight, auditable, pure Python evaluator that parses declarative JSON Abstract Syntax Trees (ASTs).

  • 🛡️ Zero eval(), Zero exec(): Immune to code injection by mathematical construction.
  • 🚀 Sub-microsecond performance: Evaluates flat rules in ~0.8 µs (>1,180,000 ops/sec) and composite nested rules in ~3.3 µs (>295,000 ops/sec).
  • 📦 Zero dependencies: Built strictly on the Python standard library. No supply-chain vulnerabilities.
  • 🗄️ Database & API Ready: Store rules natively in PostgreSQL JSONB, MongoDB, Redis, or send them directly from frontend forms.
  • 🧵 Stateless & Thread-Safe: Drop it into FastAPI, Celery, Django, or AWS Lambda without concurrency locks.
  • 🧯 DoS & Recursion Bomb Shield: Configurable max_depth protects against maliciously nested payloads.
  • 🧩 Extensible: Register custom domain operators in one line without forking.
  • 🏷️ PEP 561 Typed: Full py.typed compliance for mypy, Pyright, and VS Code.

📊 Quick Comparison

Feature eval() / exec() Heavy Rule Engines rule-engine-mini
RCE Safe ❌ Critical Risk ⚠️ Complex Sandbox ✅ 100% Deterministic
External Dependencies 0 ❌ 10+ packages ✅ 0 (Pure Standard Library)
Audit Time Impossible to secure Hours / Days ✅ 5 Minutes (Single File)
JSON-Native ❌ No (Raw strings) ⚠️ Proprietary DSL ✅ Standard JSON-Logic AST
Execution Speed Medium Slow startup ✅ 0.8 µs – 3.3 µs per rule
Recursion Protection ❌ Stack overflow ⚠️ Complex setup ✅ Built-in max_depth limit
Custom Operators Dangerous Complex plugins ✅ register_operator API
Graceful Typing 💥 Crashes on mismatch Configurable ✅ Safe fallback (False)

🚀 Installation

pip install rule-engine-mini

Requirements: Python 3.9+ (Zero external dependencies).


💡 Quickstart in 30 Seconds

from rule_engine_mini import RuleEvaluator

evaluator = RuleEvaluator()

# 1. Define a rule as a declarative JSON structure
fraud_prevention_rule = {
    "and": [
        {">": [{"var": "transaction.amount"}, 5000]},
        {"in": [{"var": "transaction.country"}, ["MX", "US", "CA"]]},
        {"==": [{"var": "user.is_flagged"}, False]}
    ]
}

# 2. Provide the runtime context (nested dicts, lists, booleans, etc.)
context = {
    "transaction": {
        "amount": 7500,
        "country": "MX"
    },
    "user": {
        "id": "usr_99",
        "is_flagged": False
    }
}

# 3. Evaluate safely in ~3.3 microseconds
is_approved = evaluator.evaluate(fraud_prevention_rule, context)
print(is_approved)  # 👉 True

🛠️ Key Capabilities

1. Deep Path Navigation (Dot Notation & Array Indices)

Traverse deeply nested dictionaries and array indexes without KeyError or IndexError:

rule = {">": [{"var": "cart.items.0.price"}, 100]}

context = {
    "cart": {
        "items": [
            {"sku": "A1", "price": 150},
            {"sku": "B2", "price": 80}
        ]
    }
}

evaluator.evaluate(rule, context)  # True

2. Safe Fallbacks for Missing Variables

Pass a default fallback value if a key might be missing in production context:

# {"var": ["path.to.variable", default_value]}
rule = {"==": [{"var": ["user.membership", "standard"]}, "vip"]}

# When context has no 'membership' key, it safely evaluates to "standard"
context = {"user": {"name": "Alice"}}
evaluator.evaluate(rule, context)  # False (no KeyError!)

3. Conditional Branching (if / Ternary)

Supports lazy evaluation: unexecuted branches are never calculated!

# {"if": [condition, then_value, else_value]}
tax_rule = {
    "if": [
        {"==": [{"var": "country"}, "MX"]},
        {"*": [{"var": "subtotal"}, 0.16]},
        {"*": [{"var": "subtotal"}, 0.08]}
    ]
}

evaluator.evaluate(tax_rule, {"country": "MX", "subtotal": 1000})  # 160.0

4. Custom Operators in One Line

Extend the engine with domain-specific logic safely:

# Register a custom 'between' operator
evaluator.register_operator(
    "between",
    lambda args, ctx, ev: ev.evaluate(args[1], ctx) <= ev.evaluate(args[0], ctx) <= ev.evaluate(args[2], ctx)
)

rule = {"between": [{"var": "score"}, 70, 100]}
evaluator.evaluate(rule, {"score": 85})  # True

5. Graceful Type Safety & Zero-Division Shield

If mismatched types are compared or division by zero occurs, rule-engine-mini handles it safely instead of crashing your service:

evaluator.evaluate({">": [{"var": "score"}, 50]}, {"score": "invalid"})  # False
evaluator.evaluate({"/": [100, 0]}, {})  # None (ZeroDivisionError protected)

6. Static Rule Validation & Error Inspection (validate)

Validate AST rules before storing them in your database or sending them to production:

from rule_engine_mini import RuleEvaluator, RuleValidationError, OperatorNotFoundError

evaluator = RuleEvaluator()

# Non-strict mode: returns boolean
is_valid = evaluator.validate({">": [{"var": "amount"}, 100]}, strict=False)  # True

# Strict mode: raises descriptive exceptions with exact AST error path
try:
    evaluator.validate({"and": [{">": [10]}, {"unknown": [1]}]}, strict=True)
except RuleValidationError as e:
    print(f"Error at {e.path}: {e}")

7. Forensic Auditing & Observability (explain)

For SOC2, HIPAA, and financial compliance, understand why a rule passed or failed with sub-microsecond precision and zero external APM tools:

rule = {
    "and": [
        {">": [{"var": "order.total"}, 500]},
        {"==": [{"var": "user.is_verified"}, True]},  # Fails here
        {"<": [{"var": "order.risk_score"}, 30]}
    ]
}

context = {
    "order": {"total": 1200, "risk_score": 10},
    "user": {"is_verified": False}
}

trace = evaluator.explain(rule, context)

print(trace.passed)        # False
print(trace.failed_path)   # "$.and[1]"
print(trace.failed_node)   # {"==": [{"var": "user.is_verified"}, True]}
print(trace.resolved_vars) # {"order.total": 1200, "user.is_verified": False}
print(trace.elapsed_us)    # 15.2 µs
# Notice: 'order.risk_score' was never evaluated due to short-circuiting!

8. Collection & Array Processing (filter, map, some, all, none)

Declaratively process arrays of nested objects in pure JSON:

# Check if all active sessions are secure
all_secure = evaluator.evaluate({
    "all": [
        {"var": "sessions"},
        {"==": [{"var": "is_secure"}, True]}
    ]
}, {"sessions": [{"is_secure": True}, {"is_secure": True}]})  # True

# Filter and calculate discounts on items >= $50
discounted = evaluator.evaluate({
    "map": [
        {"filter": [{"var": "items"}, {">=": [{"var": "price"}, 50]}]},
        {"*": [{"var": "price"}, 0.9]}
    ]
}, {"items": [{"price": 20}, {"price": 100}, {"price": 50}]})  # [90.0, 45.0]

📖 Supported Operators

Category Operator Syntax Example Description
Variables var {"var": "user.age"} Resolves nested path via dot notation
var (default) {"var": ["user.tier", "free"]} Fallback value if variable is missing
Logic and {"and": [c1, c2, ...]} Short-circuit True if all clauses are truthy
or {"or": [c1, c2, ...]} Short-circuit True if any clause is truthy
not / ! {"!": clause} Inverts boolean truthiness
!! {"!!": clause} Casts value to strict boolean
Branching if {"if": [cond, then, else]} Lazy ternary / multi-branch condition
Comparisons >, >= {">": [{"var": "total"}, 500]} Greater than / Greater than or equal
<, <= {"<": [{"var": "age"}, 18]} Less than / Less than or equal
==, === {"==": [{"var": "status"}, "active"]} Equality check
!=, !== {"!=": [{"var": "status"}, "banned"]} Inequality check
Membership in {"in": [{"var": "country"}, ["MX", "US"]]} Checks item in list or substring in string
Collections some {"some": [{"var": "items"}, predicate]} True if at least one item matches predicate
all {"all": [{"var": "items"}, predicate]} True if every item matches predicate
none {"none": [{"var": "items"}, predicate]} True if no item matches predicate
filter {"filter": [{"var": "items"}, predicate]} Returns array with elements matching predicate
map {"map": [{"var": "items"}, mapper]} Returns array transformed by mapper rule
merge {"merge": [[1, 2], [3], 4]} Shallow-flattens arrays into a single list
Strings cat {"cat": ["ID-", {"var": "id"}]} Concatenates strings (null-safe)
substr {"substr": ["hello", 0, 4]} Substring extraction with start/length
Missing missing {"missing": ["a", "b"]} Returns array of keys missing from context
missing_some {"missing_some": [1, ["a", "b"]]} Returns missing keys if minimum count unmet
Arithmetic + {"+": [10, 20, 5]} Addition / numeric sum
- {"-": [100, 30]} or {"-": 5} Subtraction / unary negation
* {"*": [5, 4]} Multiplication
/ {"/": [100, 4]} Safe division (returns None on 0)
% {"%": [10, 3]} Safe modulo (returns None on 0)
min, max {"min": [10, 5, 20]} Minimum / Maximum value

📚 Enterprise Resources

  • 📖 Enterprise Production Cookbook: 4 production blueprints with runnable code: Netflix Dynamic Entitlements, Cloud IAM ABAC, Fintech Fraud Shield, and HIPAA Clinical Sepsis Triage.
  • 📐 Formal JSON Schema AST: Draft 2020-12 JSON Schema for gateway validation and frontend UI rule builders.
  • 🛡️ Security Policy: Responsible disclosure, safe harbor, and vulnerability response SLA.

⏱️ Empirical Latency & Benchmarks

Audited on standard hardware using benchmarks/benchmark.py (100,000 iterations per workload, warmup phase included):

Workload Throughput (ops/sec) Mean Latency Median (p50) 99th Percentile (p99) Allocation Overhead
Micro Comparison (>) ~800,000 1.14 µs 1.10 µs 1.50 µs 0 bytes (zero-leak)
Short-Circuit Fast-Path ~700,000 1.32 µs 1.30 µs 1.70 µs 0 bytes (zero-leak)
Production Composite (and) ~206,000 4.75 µs 4.70 µs 7.20 µs 0 bytes (zero-leak)
Collection Projection (filter + map) ~125,000 7.91 µs 7.70 µs 13.70 µs Ephemeral result only
Audit Trace explain() Engine ~65,000 15.40 µs 15.00 µs 22.20 µs TraceStep tree only

🧪 Testing & Coverage

Run the full test suite with Python's standard unittest or coverage:

# Run tests
python -m unittest discover tests -v

# Run with coverage (enforces 100% core coverage)
coverage run -m unittest discover tests
coverage report -m

🦉 Built at Búho Zurdo Labs

Designed and battle-tested for high-throughput, mission-critical systems at Búho Zurdo Labs (buhozurdo.com).

Authors

  • Juan Rael Corrales Arellano (@buhozurdo) — rael@buhozurdo.com
  • Búho Zurdo Labs — dev@buhozurdo.com

License

MIT License © 2026–2027 Búho Zurdo Labs. Free to use in commercial and open-source projects.

Metadata

Release files for rule-engine-mini 0.5.0

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

Source distribution (sdist)

Source distribution for rule-engine-mini 0.5.0
File Size Uploaded Provenance Actions
rule_engine_mini-0.5.0.tar.gz 28.9 kB With provenance Details for rule_engine_mini-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rule-engine-mini 0.5.0
File Interpreter ABI Platform Actions
rule_engine_mini-0.5.0-py3-none-any.whl Python 3 none any Details for rule_engine_mini-0.5.0-py3-none-any.whl

Total release size: 43.3 kB

Release files / rule_engine_mini-0.5.0.tar.gz

Download URL rule_engine_mini-0.5.0.tar.gz
Size 28.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2978094b85e605c89e6c3712eab7b98743c0ad73dad4c1d495ca52b39e3916b9
BLAKE2b-256 checksum
How to use checksums
579b053aac91919442187445bcdfad37a5ae6862744e799fa2f72ca119466af3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Release files / rule_engine_mini-0.5.0-py3-none-any.whl

Download URL rule_engine_mini-0.5.0-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
39449209c09cf18a35f58b73ce7125a74d77971a3e73dd417b7d042a19c38851
BLAKE2b-256 checksum
How to use checksums
aafdec55d30c4408a93c0b57eea13a5423bc3442cc8091e18a5484c6d718e892
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 24, 2026.

Transparency log

Security for rule-engine-mini 0.5.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for rule_engine_mini-0.5.0.tar.gz

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 Nov 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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