Guardrails › Guardrail Frameworks
Language AI HandbookAll chapters Guardrails › Guardrail FrameworksOne section · Sections in this chapter4 / 9 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Building a guardrail system from scratch requires substantial engineering effort: designing the check logic, managing policy configuration, handling failures gracefully, logging results, and maintaining the system as requirements evolve. Guardrail frameworks provide pre-built components, integration patterns, and policy management infrastructure that accelerate development. Understanding the major frameworks helps you choose the right tools and design patterns for your application.
NeMo Guardrails
NVIDIA's NeMo Guardrails framework introduces the concept of rails, declarative rules that constrain what conversations can go and how the model should respond to various situations. Rails are defined in a domain-specific language called Colang, which allows operators to specify allowed topics, disallowed topics, and custom dialog flows that override default model behavior.
The architecture of NeMo Guardrails places a second LLM call before the main model call. This "guard model" interprets the user's intent using the defined rails and either allows the request to proceed, redirects it to a predefined response, or blocks it. The guard model adds latency but provides flexible, intent-level control that is harder to achieve with simple classifiers. Because the guard model itself is a language model, it can understand semantic intent rather than just matching patterns or computing vector similarities.
Colang rails can define specific dialog flows for common scenarios:
- If a user asks about topics outside the application's scope, redirect to a predefined response explaining the limitation
- If a user expresses frustration, follow a specific de-escalation flow
- If a user attempts certain manipulations, provide a specific counter-response
This dialog-level control is more expressive than binary block/allow decisions. A guardrail system that can recognize a frustrated user and route them to a specific supportive flow is doing something qualitatively different from a system that just accepts or rejects individual requests. NeMo Guardrails models the conversation as an ongoing interaction with state rather than as a series of independent message evaluations. The tradeoff is increased complexity: maintaining Colang rules requires ongoing effort as the application evolves, and the additional LLM call adds 200-500ms of latency per request. NeMo Guardrails is most appropriate for applications where the conversation flow is a critical product concern rather than a safety afterthought.
Guardrails AI (RAIL)
The Guardrails AI framework (formerly RAIL) takes a schema-centric approach. Operators define expected output structure and validation rules in a RAIL schema, and the framework handles both generating and validating the output. If the model's output fails validation, the framework automatically re-prompts the model with instructions to correct the specific issue.
This retry-on-failure pattern is the core innovation of Guardrails AI. Rather than blocking a failed response, the framework attempts to coerce the model into producing valid output through iterative correction. For structured output generation (extracting entities, filling forms, generating code), this approach dramatically reduces format failures compared to single-shot generation. A model that produces valid JSON on 80% of attempts might produce valid JSON on 98% of attempts with one retry cycle, and on 99.9% with two retry cycles.
The framework also supports validators that check semantic properties: does this response contain a disclaimer? Is this code syntactically valid? Does this summary preserve key facts from the source? Validators can be composed into complex output requirements that are automatically enforced without manual post-processing. This makes Guardrails AI particularly useful for applications that need to extract structured information from unstructured model outputs.
One limitation of the retry-on-failure approach is cost. Each retry cycle requires an additional LLM inference call, doubling or tripling the cost of a request that fails on the first attempt. For high-volume applications where structured output is critical, this cost tradeoff must be evaluated against alternatives like constrained decoding, which enforces output format at the token sampling level without requiring multiple calls.
LangChain and LangSmith
LangChain provides modular components for building LLM pipelines, and its ecosystem includes tools for adding safety and quality checks. The chain abstraction allows operators to insert custom processing steps before and after model calls, which provides a natural integration point for guardrails. Rather than being a dedicated guardrails framework, LangChain is a general-purpose pipeline framework that makes it easy to compose guardrails with other LLM operations.
LangSmith, LangChain's observability platform, enables monitoring of guardrail performance in production. Logs show which requests triggered which guardrails, how often classifiers fired, and where the system's policies are creating friction for legitimate users. This observability is critical for calibrating guardrails over time. Without production monitoring, you cannot know whether your thresholds are correctly set or whether your guardrails are causing more harm (through false positives) than they prevent.
LangSmith's tracing capabilities are particularly valuable for debugging. When a request produces an unexpected result, the trace shows exactly which guardrail fired, with what score, at what stage in the pipeline. This makes it possible to diagnose whether a wrong outcome resulted from a miscalibrated threshold, a missing exemplar in the topic set, a pattern gap in the injection detector, or a downstream issue in the model itself. Without this level of observability, debugging guardrail failures in production becomes a process of guesswork.
Llama Guard and Prompt Guard
Meta's Llama Guard is a purpose-built safety model trained to classify both user inputs and model outputs against the MLCommons AI Safety taxonomy of harm categories. It is a transformer model fine-tuned specifically for this classification task, giving better calibration for safety-specific decisions than general-purpose models. Unlike general-purpose language models used for safety classification, Llama Guard is optimized for the specific task of safety classification and provides more reliable calibration across harm categories.
Meta also released Prompt Guard, a classifier specifically trained to detect prompt injection and jailbreak attempts. These two models can be combined as pre- and post-processing steps around any LLM, giving a baseline level of safety without custom classifier development. Because both are open-source, they can be evaluated, audited, and fine-tuned for specific deployment contexts in ways that managed safety APIs cannot.
The practical advantage of these models is that they are open-source and can be self-hosted, avoiding the latency and privacy implications of calling external safety APIs. For applications with strict data handling requirements, where sending user messages to any external service is prohibited, self-hosted safety models may be the only viable option.
Llama Guard's taxonomy covers categories like violent crimes, hate speech, sexual content, privacy violations, and code malware generation. Each category can be independently enabled or disabled, allowing operators to configure exactly which harms the classifier will screen for. This configurability is important for applications with unusual scope requirements: an adult content platform might disable sexual content filtering while keeping violent content filtering enabled; a cybersecurity research tool might enable code malware generation detection at a different threshold than it would for a general-purpose tool. The ability to configure each category independently is a significant advantage over monolithic safety APIs that offer less granular control.
OpenAI Moderation API and Similar Services
Cloud providers offer safety APIs that classify text against harm categories without requiring operators to train or host their own models. OpenAI's Moderation API, Anthropic's content moderation endpoints, and similar services provide fast, calibrated classifiers that stay up to date as new harm patterns emerge. Managed safety APIs benefit from the provider's ongoing investment in classifier improvement: as new categories of harmful content emerge and as attack patterns evolve, the provider updates the classifier and operators receive the improvements transparently.
The tradeoff is that all text sent to these APIs leaves the application's trust boundary. For applications handling sensitive user data, this may be unacceptable under GDPR, HIPAA, or contractual data handling requirements. For applications where data sensitivity is lower, managed safety APIs provide a practical alternative to self-hosted models, particularly for teams without the infrastructure expertise to operate machine learning models at production scale.
A hybrid approach is worth considering: use managed safety APIs for categories that are well-covered by the provider's taxonomy and where data sensitivity permits, while self-hosting specialized classifiers for domain-specific categories that the managed API does not cover well. For example, a financial services application might use a managed API for general harm categories while self-hosting a custom classifier trained to detect requests that violate financial regulations, because the managed API was not trained on financial compliance data.