nemoguardrails.guardrails.guardrails_types
nemoguardrails.guardrails.guardrails_types
Module Contents
Classes
Functions
Data
API
One rail’s execution record, carried on RailResult for GenerationLog synthesis.
Captures what a single rail did — its verdict and the (at most one) model call it
made — as engine-neutral data. IORails maps a RailCallRecord to an
ActivatedRail (with a single synthetic ExecutedAction and LLMCallInfo);
the raw usage/timing is kept here so this module stays free of the pydantic
GenerationLog types. Tool rails that make no model call leave usage None.
Bases: enum.Enum
Direction of a rail check, used for logging.
Wrapper-class around RailOutcome object with IORails-specific metadata
The verdict itself lives entirely in outcome, which is the single source of
truth: is_safe, reason and return_value are derived views of it rather
than a second copy that could drift. What this type adds is the aggregation
RailOutcome has no concept of, because it belongs to running many rails:
which one blocked (triggered_rail) and what every rail did (records).
records carries the per-rail execution records for every rail that ran in this
check (not just the blocking one), so IORails can synthesize a GenerationLog.
It is empty unless log collection is active, and it is log-capture data rather than
part of the verdict, so it is excluded from equality (compare=False).
__hash__ is spelled out as None because RailOutcome is deliberately
unhashable: without this a frozen dataclass would generate a __hash__ that
raises from inside hash() instead of reporting this type as unhashable.
Whether this block came from a rail that raised rather than one that decided.
Whether the checked content may proceed.
The rail’s own explanation, when it authored one.
The rail’s structured verdict, as the log’s ExecutedAction.return_value.
The verdict keys are applied last so they win: metadata is free-form evidence and a
custom action may put an allowed or failed key in it, which must not be able to
record a blocked rail as having allowed the content, nor forge a rail failure.
failed is always present, as allowed is, so a log consumer reads a verdict
rather than inferring one from a missing key. Without it a rail that broke and a rail
that decided to block are the same record, which is the distinction the client-facing
message already draws.
A result that lets the content through.
A result that stops the content. Only a block names a triggering rail.
An LLM response paired with wall-clock start/finish timestamps and a monotonic duration.
Returned by IORails’ main-model call helper so the sequential and speculative paths both
carry real timing into the generation RailCallRecord.
Whether a verdict value carries anything worth showing.
Render a rail’s metadata as text, or None when it carries no evidence.
Render one verdict value, flattening a sequence into a comma-separated list.
Set an explicit request ID (e.g., derived from an OTEL trace ID).
Unlike set_new_request_id which generates a random ID, this accepts
a caller-provided string. Returns the reset token for use with
reset_request_id.
Render a blocked rail’s explanation for the error payload sent to the caller.
Position of the turn being checked: the last user message that carries content.
Render a blocked rail’s full explanation for a log line or a span.
Return the current per-request correlation ID.
Return the content of the turn being checked, or "" as the library actions expect.
Restore the request ID ContextVar to its previous value.
Return messages with the turn last_user_content reads rewritten to text.
Copied at both levels, because the caller’s own list reaches the engine by identity.
Render a chat message list to a role-labeled string for GenerationLog’s prompt.
Content parity with LLMRails’ logged prompt, not byte-for-byte format parity: each
message becomes "<role>: <content>". Non-content fields present on the message
(name, tool_call_id, tool_calls, reasoning) are appended as a compact
[key=value, ...] suffix so tool-call and reasoning-only turns are preserved rather
than dropped. Messages are blank-line separated.
Generate a random request ID, set it in the current context, and return the reset token.
Return str(text) truncated to max_len characters (default: LOG_CONTENT_TRUNCATE_LENGTH).