Skip to main content

Overview

This is a comprehensive guide on integrating Guardrails with LangChain, a framework for developing applications powered by large language models. By combining the validation capabilities of Guardrails with the flexible architecture of LangChain, you can create reliable and robust AI applications.

Key Features

  • Easy Integration: Guardrails can be seamlessly added to LangChain’s LCEL syntax, allowing for quick implementation of validation checks.
  • Flexible Validation: Guardrails provides various validators that can be used to enforce structural, type, and quality constraints on LLM outputs.
  • Corrective Actions: When validation fails, Guardrails can take corrective measures, such as retrying LLM prompts or fixing outputs.
  • Compatibility: Works with different LLMs and can be used in various LangChain components like chains, agents, and retrieval strategies.

Prerequisites

  1. Ensure you have the following langchain packages installed. Also install Guardrails
  1. As a prerequisite we install the necessary validators from the Guardrails Hub:
  • CompetitorCheck: Identifies and optionally removes mentions of specified competitor names.
  • ToxicLanguage: Detects and optionally removes toxic or inappropriate language from the output.

Basic Integration

Here’s a basic example of how to integrate Guardrails with a LangChain LCEL chain:
  1. Import the required imports and do the OpenAI Model Initialization
  1. Create a Guard object with two validators: CompetitorCheck and ToxicLanguage.
  1. Define the LCEL chain components and pipe the prompt, model, output parser, and the Guard together. The guard.to_runnable() method converts the Guardrails guard into a LangChain-compatible runnable object.
  1. Invoke the chain
Example output:
In this example, the chain sends the question to the model and then applies Guardrails validators to the response. The CompetitorCheck validator specifically removes mentions of the specified competitors (Delta, American Airlines, United), resulting in a filtered list of non-competitor airlines.

Advanced Usage

LangSmith Integration

LangSmith is a powerful tool for tracing, monitoring, and debugging your AI applications. Here’s how to use it with your Guardrails-enhanced LangChain:
  1. Set up Langsmith:
  1. View traces in the Langsmith UI to analyze:
  • LLM inputs and outputs
  • Guardrails validator results
  • Performance metrics
  • Error logs
Langsmith 1 C64ae3c9de8e4d68774997857b3569e7 Langsmith 2 8fae361960a9f4a46f6169ae4cc45acd By integrating LangSmith, you can gain deeper insights into how your Guardrails validators are affecting the LLM outputs and overall chain performance.