Registry / serialization / rule-engine

rule-engine

JSON →
library5.0.0pypiunverified

rule-engine is a lightweight, optionally typed expression language with a custom grammar for matching arbitrary Python objects. It provides a simple way to define and evaluate complex rules against data, supporting various data types and custom functions. The current stable version is 4.5.3, with frequent patch releases and a v5 alpha in active development.

pip install rule-engine
INSTALL
IMPORT
SIG · RULE-ENGINE
R
rule-engine
serializationenv5.0.0
Install
1.8s avg
Import
616ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v5.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.641s · 19.6MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.8s · import 0.590s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Rule
from rule_engine import Rule
Context
from rule_engine import Context
Used for advanced scenarios like custom functions or type definitions.
DataType
from rule_engine import DataType
Essential when defining custom data types or explicit type coercions.

This quickstart demonstrates how to create a `Rule` instance with an expression string and then evaluate it against a Python dictionary acting as the data context. The rule expression uses built-in functions like `sum` and logical operators.

from rule_engine import Rule # Define a rule with an expression string rule = Rule('sum(items) > 100 && "apple" in items') # Evaluate the rule against different data contexts print(f"Result 1: {rule.matches({'items': [10, 20, 30, 40, 50, "apple"]})})") print(f"Result 2: {rule.matches({'items': [10, 20, "orange"]})})")
Debug
Known issues
breakingVersion 5.0.0 is in alpha (v5.0.0a1 has been released) and introduces significant breaking changes and API refactors. Do not update to v5.x without thoroughly reviewing the changelog and migrating your code.
fix
Pin your dependency to `rule-engine<5.0.0` to avoid accidental upgrades. Consult the v5 changelog when you're ready to migrate.
affects: All versions prior to 5.0.0
gotchaFrom v4.5.0 onwards, rule-engine is stricter about invalid string literals and function return types. `StringSyntaxError` is now raised for malformed string literals and `FunctionCallError` for type-incompatible function returns, where previous versions might have had different behavior.
fix
Ensure string literals are correctly formatted and custom functions return values compatible with their declared `DataType`.
affects: >=4.5.0
gotchaThe backslash character (`\`) is used for escaping in rule strings. If you need a literal backslash in your rule (e.g., in a regex pattern or string literal), you must double-escape it (e.g., `\\`).
fix
Escape literal backslashes by doubling them: `"\\"` in the rule string to represent a single backslash.
affects: All versions
gotchaWhen defining custom functions or interacting with complex Python objects, ensure you correctly use `Context` and `DataType` objects. Incorrect type declarations or context setup can lead to unexpected `FunctionCallError` or evaluation issues.
fix
Refer to the official documentation on `Context` and `DataType` for defining custom functions and managing evaluation environments effectively.
affects: All versions
Upgrade
Version history
5.0.0latest on PyPI · released May 17, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
20
OpenAI (training)
1
Resources

No resource links recorded.

rule-engine — pip install rule-engine · libregistry