Executor¶
Rule execution engine. Rules run deterministically at inference time with no LLM calls.
When several rules produce overlapping or conflicting matches, the executor
orders them by priority, then validated precision (falling back to
confidence when a rule has no validated estimate), so the higher-trust rule
wins. Each rule also runs under a wall-clock budget (RULE_TIMEOUT_S): a
catastrophically-backtracking regex is bounded via the regex module's
matching timeout, and a runaway code rule is interrupted with a signal alarm,
so a single bad LLM-written rule cannot freeze evaluation.
RuleExecutor¶
RuleExecutor(use_spacy_ner=False)
¶
Executes rules against input data
Initialize the rule executor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_spacy_ner
|
bool
|
If True, run spaCy's NER pipeline during rule execution so that ENT_TYPE/ENT_ID patterns are available. |
False
|
apply_rules(rules, input_data, task_type=None, text_field=None)
¶
Apply rules to input and return aggregated output.
Rules are sorted by priority and applied sequentially. Results are deduplicated by span position. If a rule has no output_key, it is inferred from task_type using DEFAULT_OUTPUT_KEYS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rules
|
list[Rule]
|
List of rules to apply. |
required |
input_data
|
dict
|
Input data dict. |
required |
task_type
|
TaskType | None
|
Task type for inferring the default output_key. |
None
|
text_field
|
str | None
|
Input key to use for regex/spaCy matching. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Output dict with results keyed by output_key (e.g. 'entities', |
dict
|
'spans', 'label'). Empty dict if no rules matched. |
execute_rule(rule, input_data, text_field=None)
¶
Execute a single rule against input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
Rule
|
The rule to execute. |
required |
input_data
|
dict
|
Input data dict. |
required |
text_field
|
str | None
|
Input key to use for regex/spaCy matching. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
List of Span or dict results for regex/spaCy rules, or |
Any
|
arbitrary return value from code rules. Empty list on no match. |
Functions¶
substitute_template¶
substitute_template(template, match_text, start, end, groups=(), ent_type=None, ent_label=None, token_spans=None)
¶
Substitute template variables with actual values from a match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
dict[str, Any]
|
Output template dict with variable placeholders. |
required |
match_text
|
str
|
The full matched text ($0). |
required |
start
|
int
|
Start character offset ($start). |
required |
end
|
int
|
End character offset ($end). |
required |
groups
|
tuple
|
Regex capture groups ($1, $2, ...). |
()
|
ent_type
|
str | None
|
spaCy entity type string ($ent_type). |
None
|
ent_label
|
str | None
|
spaCy entity label string ($ent_label). |
None
|
token_spans
|
list[dict[str, Any]] | None
|
Per-token span dicts for spaCy dependency matches ($1.text, $1.start, $1.end, etc.). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with all template variables replaced by their actual values. |
Variables: - $0: Full match text - $1, $2, ...: Capture groups - $start: Start character offset - $end: End character offset - $ent_type: Entity type (spaCy only) - $ent_label: Entity label (spaCy only)