Concept: Middleware | Policy: Policy Engine
RAILMiddleware wraps any async function that generates text — your own model call, a
provider SDK, whatever — and runs the full pipeline around it: an optional pre-hook, an
optional input safety gate, the generation call itself, RAIL evaluation, policy enforcement,
and an optional post-hook. It’s the general-purpose version of what the provider wrappers in
Integrations do for OpenAI/Anthropic/Gemini specifically.
Parameters
eval_input is a safety gate, not a quality gate: it always raises RAILBlockedError on a
failing input score, independent of whatever policy you configured for the output.
The result
run() returns the same EvalResult used across the SDK’s policy-aware calls — see
Policy Engine: the result object for the full
field list (content, score, threshold_met, was_regenerated, dpdp, and so on).
Credits follow the calls
run() makes on your behalf — one evaluation, plus an optional
input-check evaluation, plus a regeneration if policy=Policy.REGENERATE triggers one. See
Credits.