LangChain Integration

View as Markdown

There are three main ways in which you can use the NeMo Guardrails library with LangChain:

  1. Add guardrails to a LangChain agent through the middleware hooks (LangChain v1).
  2. Add guardrails to a LangChain chain (or Runnable).
  3. Use a LangChain chain (or Runnable) inside a guardrails configuration.

Add Guardrails to an Agent

For tool-calling agents built with create_agent, the GuardrailsMiddleware hooks into the agent loop to run safety checks before and after every model call:

1from langchain.agents import create_agent
2from langchain_openai import ChatOpenAI
3from nemoguardrails.integrations.langchain.middleware import GuardrailsMiddleware
4
5guardrails = GuardrailsMiddleware(config_path="path/to/config")
6model = ChatOpenAI(model="gpt-4o")
7
8agent = create_agent(model, tools=[...], middleware=[guardrails])
9result = agent.invoke({"messages": [{"role": "user", "content": "Hello!"}]})

For more details, check out the Agent Middleware Guide.

Add Guardrails to a Chain

You can easily add guardrails to a chain using the RunnableRails class:

1from nemoguardrails import RailsConfig
2from nemoguardrails.integrations.langchain.runnable_rails import RunnableRails
3
4# ... initialize `some_chain`
5
6config = RailsConfig.from_path("path/to/config")
7
8# Using LCEL, you first create a RunnableRails instance, and "apply" it using the "|" operator
9guardrails = RunnableRails(config)
10chain_with_guardrails = guardrails | some_chain
11
12# Alternatively, you can specify the Runnable to wrap
13# when creating the RunnableRails instance.
14chain_with_guardrails = RunnableRails(config, runnable=some_chain)

For more details, check out the RunnableRails Guide and the Chain with Guardrails Guide.

Using a Chain inside Guardrails

To use a chain (or Runnable) inside a guardrails configuration, you can register it as an action.

1from nemoguardrails import RailsConfig, LLMRails
2
3config = RailsConfig.from_path("path/to/config")
4rails = LLMRails(config)
5
6rails.register_action(SampleChainOrRunnable(), "sample_action")

Once registered, the chain (or Runnable) can be invoked from within a flow:

define flow
...
$result = execute sample_action
...

For a complete example, check out the Runnable as Action Guide.

LangSmith Integration

The NeMo Guardrails library integrates out-of-the-box with LangSmith. To start sending trace information to LangSmith, you have to configure the following environment variables:

$export LANGCHAIN_TRACING_V2=true
$export LANGCHAIN_ENDPOINT=https://api.smith.langchain.com
$export LANGCHAIN_API_KEY=<your-api-key>
$export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"

For more details on configuring LangSmith check out the LangSmith documentation.