Rules & decisions¶
A DecisionEngine (or Pipeline(rules=..., gate=...)) evaluates an extraction and returns a
Verdict with decision, confidence, field_confidence, outcomes and blockers.
Three ways to write a rule¶
from nirmala import rule
@rule(field="total", when="currency == 'INR'", severity="block")
def total_adds_up(ctx):
"Total must equal subtotal plus GST"
return abs(ctx["subtotal"] + ctx["gst"] - ctx["total"]) < 0.01
Return True/False, (bool, reason), None (not applicable) or a RuleOutcome.
ctx.values, ctx.document_text, ctx.data and ctx.printed(value) are available.
An exception counts as a failure.
Checked by the judge against the document text and the extracted values. All plain-English rules of one evaluation go out in a single judge call. An unanswered rule counts as failed.
rules:
- name: idv_for_motor
when: insurance_type == 'Motor'
require: present(idv) and idv > 0
message: Motor policies must print an IDV
- name: premium_math
judge: The total premium equals the base premium plus GST.
severity: reject
Load with nirmala.load_rules("rules.yaml") (needs nirmala[yaml]) or
nirmala.rules_from_config(list_of_dicts).
Expressions¶
when and require use a small, safe expression language: field names, literals, arithmetic,
comparisons, and / or / not, in, and the functions present, printed, diff,
matches, len, min, max, abs, round, lower, upper, int, float, str.
Attribute access, subscripts, lambdas and imports are rejected when the rule is loaded. Unknown
names are None, and comparisons against None are false.
Severity¶
| Severity | A failure... |
|---|---|
reject |
rejects the record |
block (default) |
sends it to review |
warn |
is recorded only |
Confidence gates¶
Each extracted value gets a score: 100 when it is visibly printed in the document (a string
verbatim, a number, or any common spelling of a date), otherwise the judge's 0-100 confidence.
Every critical field (from field(critical=True) or Gate(critical=[...])) must reach field,
and the mean must reach average. A missing critical field blocks. If the judge is unavailable,
every unscored field scores 0: the gate fails closed.
The judge¶
nirmala.Judge(llm=None, model=None) sends typed questions (boolean, number, choice,
text) with the evidence, and keeps only answers that match their declared type. Use it directly
for your own checks:
judge = nirmala.Judge(model="opencode-go/qwen4-max")
decision = judge.decide({"document_text": text}, {
"is_signed": {"type": "boolean", "prompt": "Is the document signed?"},
"kind": {"type": "choice", "choices": ["invoice", "receipt"], "prompt": "What is it?"},
})
decision.ok, decision.answers, decision.reasons