PyRulesEngine - LLM Prompting & Schema Generation Guide
This guide provides instructions and few-shot examples designed for Large Language Models (LLMs) and Agentic pipelines. Use this reference to reliably convert Natural Language (NL) logic into valid YAML/JSON PyRulesEngine (JDM) schemas using Google Common Expression Language (CEL).
Guidelines for LLM Generation
When converting user requirements into PyRulesEngine schemas, an LLM must follow these strict rules:
- Top-Level Array: The output must always be a list/array of Workflow objects.
- CEL Syntax Mandatory: All
Expressionstrings must use CEL syntax:- Booleans: Use lowercase
trueandfalse. - Operators: Use
&&(and),||(or), and!(not). - Equality: Use
==and!=.
- Booleans: Use lowercase
- Mandatory Fields: Every Rule inside a Workflow must have
RuleName. If it's an evaluation node, it must have anExpression. - RuleExpressionType: Default is
"LambdaExpression". To define a "Kill Switch" or "Blocker", use"ErrorType": "Error". - No Python Logic: The engine DOES NOT support Python-specific keywords like
and,or,not,in(except for list inclusion), or anyimportstatements. - Built-in Functions:
- Use
abs(),min(),max(), andround()for numerical operations. - Use CEL macros for collections:
collection.exists(x, x.score > 10),collection.all(...),collection.filter(...).
- Use
- Aggregators (Operators):
- Use
AndAlso(short-circuiting AND) orOrElse(short-circuiting OR) for complex multi-rule conditions. - Dictionary Access: Access inputs via standard dot notation or brackets. Example:
user.age > 18oruser['age'] > 18.
Few-Shot Example Library (NL to YAML)
Prompt 1: Multi-Condition Decision
User Input: "Approve if the transaction amount is under 5000 and the customer is not in a high-risk region."
Expected LLM Output (YAML):
- WorkflowName: "TransactionApproval"
Version: "1.0.0"
Rules:
- RuleName: "BasicRiskCheck"
RuleExpressionType: "LambdaExpression"
Expression: "transaction.amount < 5000 && !customer.is_high_risk_region"
SuccessEvent: "APPROVED"
Prompt 2: Collection Processing (CEL Macros)
User Input: "Decline the request if any product in the cart has a quantity greater than 100."
Expected LLM Output (YAML):
- WorkflowName: "InventoryGuard"
Version: "1.0.0"
Rules:
- RuleName: "BulkQuantityBlocker"
RuleExpressionType: "LambdaExpression"
ErrorType: "Error"
Expression: "cart.products.exists(p, p.quantity > 100)"
ErrorMessage: "Bulk orders are not permitted via this channel."
Prompt 3: Math Extensions
User Input: "Calculate the absolute difference between 'actual' and 'expected' values. If it exceeds 'threshold', flag for review."
Expected LLM Output (YAML):
- WorkflowName: "AnomalousDrift"
Version: "1.0.0"
Rules:
- RuleName: "ThresholdDriftCheck"
Expression: "abs(measurement.actual - measurement.expected) > measurement.threshold"
SuccessEvent: "TRIGGER_REVIEW"
Anti-Patterns to Avoid
- Python Keywords: Error:
expr: "user.is_admin and active == True". Correct:expr: "user.is_admin && active == true". - Missing Quotes: Error:
expr: "user.role == admin". Correct:expr: "user.role == 'admin'". - Invalid Macros: Error:
expr: "Linq.any(items, x => x > 0)". Correct:expr: "items.exists(x, x > 0)".
Validation and Self-Healing
All generated schemas pass through a CEL Syntax Validation Gate. If the generated Expression is syntactically invalid (e.g., mismatched brackets or incorrect operators), the engine will return the specific error message to the LLM for immediate correction.