nemoguardrails.guardrails.compiled_rail
nemoguardrails.guardrails.compiled_rail
Manifest-driven rail execution for IORails.
A CompiledRail is the executable unit behind one configured flow string. It is built
once, at engine construction. It resolves the flow’s RailSurface from the manifest
catalog, imports the library action the surface declares, and freezes a plan for filling
that action’s parameters. Thereafter each request is one await action(**kwargs) and
the returned RailOutcome is passed back to the caller unchanged.
Module Contents
Classes
Functions
Data
API
One configured flow, resolved to a library action and ready to run.
The manifest surface name, without any $param= suffix.
The variable the manifest says this rail may rewrite, which is how it is scheduled.
Assemble the action’s arguments from its declared parameters and the manifest.
Call the action, awaiting it only if it is asynchronous.
Two shipped library actions are plain def (the guardrails_ai validators), so an
unconditional await would raise TypeError on every request and the fail-closed
envelope would report a working rail as a block. ActionDispatcher has always made
the same allowance for LLMRails.
Every value injectable by parameter name; the caller filters against the signature.
Execute the rail, returning its verdict and the model calls it made.
A fresh processing_log_var sink is installed around the action, so llm_calls
holds this rail’s calls and only this rail’s — live calls, cache hits and jailbreak’s
NIM call all append there, and a rail that reaches no model appends nothing.
Execute the rail and return its engine-neutral verdict.
Return the same execution plan with its runtime collaborators finalized.
Bases: Exception
A configured flow cannot be turned into an executable rail.
Raised while compiling, never while serving a request: a rail that fails mid-request
produces a blocking outcome through rail_guard instead. The message is user-facing —
it is why a config is not servable — so name the flow and what is wrong with it.
Runtime collaborators a rail action may declare as parameters.
Injection is by parameter name, matching how the Colang runtimes supply the same values to the same actions. An action receives only what its signature declares.
One rail run: its engine-neutral verdict, plus every model call the action made.
The caller converts outcome into its own result type; CompiledRail never does.
One action parameter and the value the manifest says fills it.
One action parameter and the conversation variable that fills it, per request.
Unlike a literal or a surface parameter this cannot be frozen at compile time: its value is the request’s own text, so freezing it would pin every later request to the first one.
Return the parameter names action accepts by name.
**kwargs is excluded deliberately: a catch-all would otherwise look like a
parameter called kwargs and be handed the wrong value.
Whether action has a **kwargs catch-all, so any keyword can be passed to it.
Return a non-literal binding’s source key, or fail compilation naming the parameter.
Plan the bindings filled from the request’s own conversation variables.
A context binding maps one variable onto a specific action parameter, which is not the
same as injecting the whole context dict: user_message reaches an action that
calls the parameter text or user_prompt.
Freeze the bindings whose values are known at compile time: literals and $params.
Whether the selected classifier runs in-process, per backends.get_backend.
The turns preceding the one being checked.
Actions append the checked turn themselves, from their bound user_message, and always
at the end. So the history stops short of it: emitting it here would hand the model the
same turn twice, and emitting what follows it — an assistant reply in a check()
transcript, say — would place a later turn ahead of it and reorder the conversation.
With no user turn to check, every message is history.
Whether distribution is installed, without importing it.
Whether jailbreak detection runs in-process, which it does with no endpoint configured.
Pull the LLMCallInfo records out of a processing-log sink.
Return the manifest declaring surface, or None if the catalog has no owner for it.
Return rails.config.<name> from a RailsConfig, or None when it is not configured.
Fail compilation when a rail’s optional dependency is declared but not installed.
Library actions import their optional dependency lazily, inside the function, as
nemoguardrails/AGENTS.md requires. Nothing therefore fails until a request arrives, and
the fail-closed envelope turns the ImportError into a block — so a config missing an
extra is indistinguishable, to the caller, from one whose rail genuinely tripped. Refusing
at compile time reports it once, as the configuration error it is.
A manifest offering both an in-process and a remote backend is enforced only when the
configuration selects the in-process one; see _LOCAL_BACKEND_CHECKS.
Checked by distribution rather than by import, so it costs no import and needs no mapping
from a package name to a module name — guardrails-ai imports as guardrails.
Fail compilation when the manifest binds a parameter the action cannot be passed.
Note the asymmetry with injection, which is easy to get wrong. Injection ignores
**kwargs because “should this be offered?” must come from declared parameters, or a
catch-all gets handed every dependency. This asks “can this be passed?”, which a
catch-all always can — so reusing the injection set here refuses actions that work.
Fail compilation when a rail names a model type the configuration does not declare.
Model bindings identify this dependency explicitly. This covers both a configurable
$model= and a model type baked into the manifest without coupling compilation to the
action parameter’s name.
Build the conversation variables for one request.
Find the manifest surface for flow, or explain why there is not one.
Report a surface declaring a rewrite IORails has nowhere to put.
Refuses nothing today: every shipped surface agrees with its direction, and no schema rule makes it.
Return declared context variables that IORails cannot construct.
Report context requirements outside IORails’ request contract.
Manually edited blocklist of unsupported rails with reasons why
Compile one configured flow string into an executable rail.
Unservable rails raise a RailCompilationError, validated at compile time.
Convert IORails messages into the event shapes conversation-history actions read. Used by actions which are tightly-coupled with colang event definitions for backwards-compatibility.
Why flow cannot run under manifest-driven execution, or None when it can.
Why manifest-driven execution cannot run surface, or None when it can.