nemoguardrails.guardrails.rails_manager
nemoguardrails.guardrails.rails_manager
Rails manager for IORails engine.
Orchestrates input/output safety checks by delegating to RailAction instances. Rails run sequentially by default; the first failing rail short-circuits. When parallel mode is enabled, all rails run concurrently and the first unsafe result cancels remaining rails immediately.
Module Contents
Classes
Data
API
Orchestrates input and output safety checks for IORails.
Reads the rails configuration to determine which checks are enabled, instantiates the corresponding RailAction for each flow, then runs them sequentially or in parallel.
Instantiate the tool rails for flows, checking each resolves to expected_cls.
Raises RuntimeError on a duplicate flow, an unknown flow, or a flow that
resolves to the wrong direction. Duplicates are rejected because the dispatch
keys its coroutine map by flow, so a repeated flow would silently drop a run.
Instantiate the RailAction for a given flow base name.
Resolve the per-request enable toggle into the configured flows to run.
True (the default) runs every configured flow; False runs none; a list
runs only the named flows that are configured, preserving configured order and
ignoring unknown names. The two booleans are spelled out as separate cases so a
non-empty list is never mistaken for True.
List membership is compared on the normalized flow name (_get_flow_name),
the same way _create_action, _build_tool_actions and unsupported_reason
do, so a request toggle carrying the canonical rail name matches a configured flow
that carries a $model=/(...) suffix instead of silently dropping it
(fail-open). Shared by the input, output, and tool rail families.
Dispatch a single rail flow to its RailAction instance.
Run rail coroutines concurrently, cancelling remaining on first unsafe result.
Run rail coroutines sequentially, short-circuiting on first unsafe result.
Dispatch a single tool-call rail to its action, wrapped in an OUTPUT rail span.
Validate each turn’s results against that turn’s calls, wrapped in an INPUT rail span.
Each exchange is validated independently so call_id linkage stays turn-local;
the first unsafe exchange short-circuits.
Validate the model’s emitted tool calls (OUTPUT-direction tool rail).
The tool-call counterpart to :meth:is_output_safe: takes the model’s output
(tool_calls) plus the request’s declared tools (llm_params) and returns
a RailResult.
Validate incoming tool results (INPUT-direction tool rail).
The tool-result counterpart to :meth:is_input_safe: takes the conversation
messages and returns a RailResult. Groups the conversation into per-turn
(calls, results) exchanges via the engine adapter and validates each result
against its own turn’s calls, so call ids reused across turns (spec-allowed) are
not flagged as ambiguous duplicates.
Run the enabled input rails, short-circuiting on the first failure.
The per-request enabled toggle selects which configured input rails run:
True (the default) runs all, False runs none, and a list runs only the
named flows (matched on the normalized flow name). When parallel mode is enabled,
all selected rails run concurrently and the first unsafe result cancels the rest.
Run the enabled output rails, short-circuiting on the first failure.
The per-request enabled toggle selects which configured output rails run:
True (the default) runs all, False runs none, and a list runs only the
named flows (matched on the normalized flow name). When parallel mode is enabled,
all selected rails run concurrently and the first unsafe result cancels the rest.