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}")

📖 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
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

🏗️ Real-World Recipe: FastAPI Dynamic Policy Engine

Store rules in your database and let business operations update logic in real time without redeploying your backend:

from fastapi import FastAPI
from pydantic import BaseModel
from rule_engine_mini import RuleEvaluator

app = FastAPI()
evaluator = RuleEvaluator()

# Imagine this rule is fetched dynamically from PostgreSQL JSONB or Redis
DISCOUNT_RULE = {
    "or": [
        {">=": [{"var": "cart.total"}, 1000]},
        {"and": [
            {"==": [{"var": "user.is_subscriber"}, True]},
            {">=": [{"var": "cart.total"}, 500]}
        ]}
    ]
}

class CheckoutRequest(BaseModel):
    user: dict
    cart: dict

@app.post("/checkout/discount")
def check_discount(payload: CheckoutRequest):
    context = payload.model_dump()
    qualifies = evaluator.evaluate(DISCOUNT_RULE, context)
    return {"discount_applied": qualifies}

⏱️ Benchmark

Tested on x86-64 hardware with Python 3.12 (100,000 iterations):

Operation                               Latency           Throughput
-------------------------------------------------------------------------
Flat single rule (e.g. amount > 100)    0.84 µs / eval    ~1,180,000 ops/sec
Composite 3-clause rule                 3.37 µs / eval      ~296,000 ops/sec
Memory allocations per evaluation       0 (Stack frames 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.3.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.3.0
File Size Uploaded Provenance Actions
rule_engine_mini-0.3.0.tar.gz 15.4 kB With provenance Details for rule_engine_mini-0.3.0.tar.gz

Built distribution (wheel)

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

Total release size: 26.0 kB

Release files / rule_engine_mini-0.3.0.tar.gz

Download URL rule_engine_mini-0.3.0.tar.gz
Size 15.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2bcb7b612f651a89e2c04f6215c5e9e10f587d11f8dd297449e57adaeb23a75a
BLAKE2b-256 checksum
How to use checksums
2f86cb15768f1e29c1c7167ab277aa77b535bb7197c4b252aad40b20ad031849
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 20, 2026.

Transparency log

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

Download URL rule_engine_mini-0.3.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
551bf38afba42b71fb39af2cb12d22191fbcbd9fba8c4d7e9f4e76bbaca2c64d
BLAKE2b-256 checksum
How to use checksums
cf08c803dd616ab16df7b68cb4d19bd1b36fbe8d8f1c90c5c2aa0b7b7455d132
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 20, 2026.

Transparency log

Security for rule-engine-mini 0.3.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.3.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 20, 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