nemoguardrails.guardrails.rails_manager
nemoguardrails.guardrails.rails_manager
Rails manager for IORails: runs input/output checks through compiled, manifest-driven rails.
Module Contents
Classes
Functions
Data
API
Compiles a manifest-driven rail per configured flow and 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.
Turn both directions sequential, because concurrent rails cannot carry a rewrite.
Resolve the per-request toggle into the configured flows to run: True all, False none.
The flows this request runs, in the order it runs them: rewriting rails first.
Bundle the collaborators a compiled rail’s action may declare as parameters.
Dispatch a single rail flow to its compiled rail and record what it did.
Run rail coroutines concurrently; on the first unsafe result, finish draining its completion batch (so rails that finished alongside it still contribute their records) before cancelling the rest.
Run a direction’s rails in turn, threading each rewrite into the rails behind it.
Dispatch a single tool-call rail to its action, wrapped in an OUTPUT rail span.
Run tool rail coroutines in turn, short-circuiting on the first unsafe result.
Separate from the input/output loop because tool rails carry no text between them, so their coroutines are built up front and the unreached ones need closing.
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.
Close the runtime-owned pooled HTTP client.
Return the text this direction’s rails check, which is the text a rewrite replaces.
Merge a captured model call’s usage, naming and timing into record.
Build the per-rail GenerationLog record for a rail that reached no model.
Build the per-rail GenerationLog record from a rail’s result and the calls it made.
Map an engine-neutral rail verdict onto IORails’ rail result.
Refuse a rewrite from a concurrent rail: peers read the arriving text, so it cannot compose.
Report the text that survived every rail, so a rewrite undone by a later rail is no rewrite.
The configured flows whose surface declares they may rewrite the text they check.
Return what a rail rewrote this direction’s variable to, ignoring any it also named.
A rail guarding both sides of a turn names both, and each direction takes its own, as the Colang flows do with the same verdict.
Map a tool rail’s verdict, refusing a rewrite it has nowhere to apply.
Order rails that may rewrite ahead of rails that only judge, so a mask reaches the check.