Quick Overview
In this technical tutorial, Krish Naik demonstrates how to protect retrieval-augmented generation applications against prompt injection attacks using NVIDIA NeMo Guardrails. Following a previous session that demonstrated how vulnerable RAG pipelines can be exploited, this video provides a step-by-step implementation of programmable input, document, and output safety rails in Python.
Key Points
- 1.NVIDIA NeMo Guardrails provides a programmable runtime safety layer for LLM applications using configurable YAML policies.
- 2.Input rails validate and intercept direct prompt injection attempts before queries reach the retrieval pipeline or the language model.
- 3.Document and retrieval rails screen retrieved context passages to block indirect prompt injections and poisoned knowledge base documents.
- 4.Output rails evaluate final assistant responses to prevent unsafe output, malicious URL delivery, and corporate policy violations.
- 5.Integrating input, document, and output rails into a LangGraph RAG workflow reduced successful attacks from 13 out of 14 down to zero out of 14.
- 6.Running input rails before LLM calls saves compute costs by stopping malicious requests before reaching the expensive retrieval and generation stages.
Summary
Krish Naik opens the tutorial by reviewing findings from the prior module, where an Acme Cloud retrieval-augmented generation application was subjected to both direct and indirect prompt injection attacks. In this session, the objective is to implement defensive mechanisms using NVIDIA NeMo Guardrails, an open-source Python toolkit and runtime safety layer designed to add programmable guardrails to large language model applications. Naik introduces the complete NeMo Guardrails architecture, highlighting five distinct types of rails: input rails, dialog rails, retrieval rails, execution rails, and output rails. To explain the concept intuitively, he compares guardrails to railway tracks that guide an engine safely to its destination, preventing dangerous deviations and system accidents.
The implementation begins with inspecting the NeMo configuration setup, which relies on YAML files located in designated folders. The primary application rail configuration, stored in app_rails, specifies the underlying LLM as OpenAI GPT-4o-mini with temperature set to zero, and defines self-check flows for inputs and outputs. The YAML file outlines clear natural language policies for an input security checker. This checker is instructed to block any user query requesting off-topic creative content, persona alterations, derogatory remarks, URL formatting, or instructions to ignore previous system prompts. A corresponding output self-check prompt verifies that final model responses remain polite, factual to Acme Cloud documentation, and free from malicious links or unauthorized credential disclosures.
Naik next addresses indirect prompt injections that enter the system through poisoned retrieved documents stored in the vector database. To combat this vector, a separate document rail configuration is set up in the document_rails directory. Its policy prompt directs the model to inspect retrieved passages and block any content containing explicit instructions directed at the AI assistant, requests to provide personal contact details, or prompts to exfiltrate passwords and payment data. In the architectural flow, this retrieval rail operates directly after document retrieval from the vector store and filters out malicious chunks before the LLM generates an answer.
Demonstrating the rails in code, Naik imports RailsConfig and LLMRails from the nemoguardrails library along with RailStatus. Because NeMo Guardrails operates asynchronously, tests are run using the check_async method. Individual input rail checks successfully allow legitimate queries such as support hour requests while blocking piggyback attacks that append requests for poems. Similarly, the document rail blocks poisoned passages containing fake WhatsApp support numbers while allowing genuine support schedules. Output rails are demonstrated blocking damaging outputs, including text falsely claiming Acme Cloud is a scam.
Finally, Naik constructs a complete guarded RAG pipeline using LangGraph. The graph defines nodes for the input rail, vector retrieval, document rail filtering, model generation, and output rail verification. When the nine direct attacks and five poisoned document attacks from the prior module are executed against the guarded LangGraph application, all fourteen attacks are blocked, compared to thirteen successful breaches prior to applying the rails. Testing the application with benign operational questions verifies that the security policies do not generate false positives, correctly answering user inquiries regarding leave policies, password requirements, and refund terms.
Introduction to NeMo Guardrails and Safety Architecture
Krish Naik reviews prompt injection attacks from the previous module against an Acme Cloud RAG app and introduces NVIDIA NeMo Guardrails as an open-source Python toolkit. He outlines the five guardrails supported by NeMo: input rails, dialog rails, retrieval rails, execution rails, and output rails. Analogising rails to railway tracks that keep a train safely on course, Naik explains that rails use declarative YAML configuration files and secondary LLM self-checks to decide whether text should be passed, modified, or blocked.
Configuring Input and Output Rails in YAML
The tutorial examines the YAML configuration file located in the app_rails directory. This configuration defines the underlying model, enables input and output self-check rails, and provides plain English instruction prompts. The input self-check prompt instructs the security checker to block inquiries about unrelated topics, tone changes, system prompt leaks, or rule overrides. The output self-check prompt enforces polite messaging, prohibits competitor endorsements, and blocks abusive responses or unauthorized links.
Configuring Document Rails for Poisoned Context
Naik demonstrates the document_rails configuration file designed to inspect retrieved context passages. In a RAG application, external knowledge sources can contain indirect injection payloads or poisoned metadata, such as fake WhatsApp numbers. The document rail prompt directs a secondary model call to evaluate retrieved passages and block text instructing the assistant directly, commanding password leaks, or overriding assistant boundaries before those passages reach generation.
LangGraph Integration and Attack Scoreboard
The individual rails are tested asynchronously in Python using check_async before being integrated into a LangGraph state graph. The graph connects an input rail node, retrieval, a document rail node, LLM generation, and an output rail node. Running the suite of nine direct attacks and five poisoned document indirect attacks against the guarded app stops all fourteen attacks, bringing successful exploits down from thirteen out of fourteen to zero, while still correctly answering legitimate user questions without false positives.
The Bottom Line
The video establishes that implementing NVIDIA NeMo Guardrails across input, document, and output stages effectively mitigates both direct and indirect prompt injection vulnerabilities in RAG architectures. By integrating these checks into a LangGraph execution flow, the application achieves zero successful exploits across all fourteen tested attack vectors without impeding standard operational inquiries. The tutorial leaves advanced dialog rails, execution rails, and regex-based guardrail performance optimizations for the subsequent deep-dive module.
FAQ
What is NVIDIA NeMo Guardrails and what are its primary components?
NVIDIA NeMo Guardrails is an open-source Python toolkit and runtime safety layer that adds programmable guardrails to LLM-based applications. Its architecture includes five core types of rails: input rails, dialog rails, retrieval rails, execution rails, and output rails.
How does an input rail protect a RAG application from prompt injection?
An input rail acts as the first line of defence by evaluating incoming user prompts against configurable policies before they reach the vector retriever or LLM. It blocks queries containing jailbreak attempts, persona hijacking, or commands to ignore system instructions.
How do document rails defend against poisoned data in retrieval-augmented generation?
Document rails inspect context passages retrieved from the vector store before passing them to the generation step. They analyze the retrieved text and remove or block passages containing hidden attacker instructions, phishing links, or unauthorized policy overrides.
What role does the output rail perform in NeMo Guardrails?
The output rail checks the final response generated by the language model before it is displayed to the user. It verifies that the answer complies with safety rules, avoids offensive content, and blocks malicious output such as phishing links or harmful accusations.
How does NeMo Guardrails evaluate whether incoming text violates security rules?
NeMo Guardrails executes self-check LLM calls using task-specific prompts defined in YAML configuration files. The secondary model compares the text against the written policy and returns an assessment indicating whether the content should be passed, modified, or blocked.
Why does blocking direct attacks at the input rail save operational costs?
Blocking direct prompt injections at the input rail stops malicious queries before vector search retrieval and main LLM generation occur. Preventing unneeded calls to the more expensive components of the pipeline reduces total API and compute expenditure.
Worth watching for
AI engineers, backend developers, and security practitioners building production retrieval-augmented generation systems who need practical defences against direct and indirect prompt injection attacks.
- nemo-guardrails
- prompt-injection
- rag-security
- langgraph
- ai-safety
- nvidia