NeMo Guardrailsnemoguardrails 0.24.0 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
35 small wins to finish your pathNext lesson

Execution rails: checking what an action returns

An execution rail checks what a tool or action handed back before the bot uses it. In version 0.24 with Colang 1.0 that is a flow, and the YAML key you may find online for it does nothing.

Checking a result before the bot speaks

text
define user ask order status
  "Where is my order?"
  "Has order A17 shipped?"

define bot report order
  "Order $order_id is $status."

define bot unknown order
  "I cannot find that order number."
text
define flow order status
  user ask order status
  $order_id = execute find_order_id
  $status = execute lookup_order(order_id=$order_id)
  if $status == "unknown"
    bot unknown order
    stop
  bot report order

The flow calls two actions and keeps their results in variables with $, which is how one action's result reaches the next: $order_id goes into lookup_order. Before bot report order can put $status into a sentence, the flow checks it, and an unknown order gets its own reply.

Example
import pretend_nemo
from nemoguardrails import LLMRails, RailsConfig

from config_actions import find_order_id, lookup_order

rails = LLMRails(RailsConfig.from_path("."))
rails.register_action(find_order_id, "find_order_id")
rails.register_action(lookup_order, "lookup_order")
for asked in ["Has order A17 shipped?", "Where is order Z01?"]:
    print(asked, "->", rails.generate(messages=[{"role": "user", "content": asked}])["content"])

Without the check, the second reply would have been Order Z01 is unknown., a sentence built from a value that was never meant for a customer.

The rails.execution key

Example
from nemoguardrails import RailsConfig
from nemoguardrails.rails.llm.config import Rails

print(list(Rails.model_fields))
config = RailsConfig.from_content(yaml_content="""
rails:
  execution:
    flows:
      - check tool output
""")
print(hasattr(config.rails, "execution"))

NeMo's documentation shows rails: execution: flows: in its configuration reference. In 0.24.0 the rails section has no such field, and the configuration loads without a warning while the key is dropped. A rail configured that way never runs, and nothing tells you.

tool_input and tool_output are real fields. They run flows on tool calls a model makes and on the tool results sent back to it, so they apply when a model calls tools itself. The check above is for actions your own flows call.

Try it yourself
  • Add B99 to the questions and read which reply it gets.
  • Make lookup_order return an empty string for unknown orders and fix the flow to match.
  • Print config.rails.model_dump() for the configuration with the execution key.

Little by little, you're building something great.