person-military-pointingSimple Guardrail

This guide will walk you through adding a Guardrail component to your existing RAG pipeline that validates inputs and terminates execution when conditions are not met, ensuring your pipeline only processes valid requests.

Guardrail functionality provides input validation and safety checks, preventing errors and protecting your system from malicious or malformed inputs.

circle-info

This tutorial extends the Your First RAG Pipeline tutorial. Ensure you have followed the instructions to set up your repository.

chevron-rightPrerequisiteshashtag

This example specifically requires:

  1. Completion of the Your First RAG Pipelinearrow-up-right tutorial - this builds directly on top of it

  2. Completion of all setup steps listed on the Prerequisitesarrow-up-right page

  3. A working OpenAI API key configured in your environment variables

You should be familiar with these concepts and components:

  1. Components in Your First RAG Pipeline - Required foundation

  2. guardstep


githubView full project code on GitHub

Installation

# you can use a Conda environment
pip install --extra-index-url "https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/" gllm-rag gllm-core gllm-generation gllm-inference gllm-pipeline gllm-retrieval gllm-misc gllm-datastore

How to Use this Guide

You can either:

  1. Download or copy the complete guide file(s) to get everything ready instantly by heading to 📂 Complete Guide Files section in the end of this page. You can refer to the guide whenever you need explanation or want to clarify how each part works.

  2. Follow along with each step to recreate the files yourself while learning about the components and how to integrate them.

Both options will work—choose based on whether you prefer speed or learning by doing!

Project Setup

1

Extend Your RAG Pipeline Project

Start with your completed RAG pipeline project from the Your First RAG Pipeline tutorial. We don't need to add any new file for this tutorial. Therefore, the structure should stay as is:


1) Build the Guardrail Pipeline

1

Create modules/validators.py with a simple length validation function:

The validator function takes an inputs dictionary and returns True for valid queries, False for invalid ones.

2

Define the extended state

Create a custom state that includes validation parameters:

3

Create the guardrail step

This is the core guard logic that validates inputs and controls execution:

How it works: If validation passes, executes retriever step; if it fails, logs error and terminates.

4

Compose the final pipeline

Chain all steps to create the complete guardrail pipeline:

This creates a pipeline that validates inputs before processing and automatically terminates on invalid requests.

🧠 The guard step acts as a gatekeeper, ensuring only valid requests reach your expensive operations.

2) Run the Pipeline

circle-info

When running the pipeline, you may encounter an error like this:

Don't worry about this, since we do not use this Chroma feature. Your Pipeline should still work.

1

Configure the pipeline state for testing

Set up test cases to see guardrail validation in action:

When you run the pipeline with valid input, you should see normal pipeline execution with retrieval and response generation. Otherwise, you should see validation failure, error logging, and pipeline termination without expensive operations.

Troubleshooting

  1. Validation not working:

    1. Ensure validator function returns boolean (True/False)

    2. Check input_state_map keys match your state variables

    3. Verify validator function signature accepts inputs: dict[str, Any]

    4. Use debug mode to see if guard step is executing

  2. Pipeline not terminating on invalid input:

    1. Confirm guard step is properly configured with success/failure branches

    2. Check that the validator function logic is correct

    3. Verify all required state variables are present

    4. Use debug mode to inspect guard step execution

  3. State mapping errors:

    1. Ensure all required state variables are initialized in your test

    2. Check that input_state_map keys exist in your pipeline state

    3. Verify validator function accesses inputs through the dictionary

    4. Match state field names exactly in the mapping


Congratulations! You've successfully enhanced your RAG pipeline with guardrail functionality. Your pipeline now validates inputs before processing and automatically blocks invalid requests, protecting your system resources and improving reliability.

Last updated

Was this helpful?