Skip to main content
Dome supports adding Guardrails to LangChain chains by providing the GuardrailRunnable wrapper class. You can use this wrapper to convert a Guardrail object into a runnable which can be added in to any chain. GuardrailRunnables expect a dictionary with the query key, which contains the string that should be passed through the Guardrail.

Creating GuardrailRunnables

Lets start by importing the necessary libraries you would need for this example.
Now, we create the Guardrails we want to use in the chain. Guardrails can be obtained from Dome’s get_guardrails function. In the example below, the input Guardrail uses a single Guard for prompt injection, and the output Guardrail uses one Guard with a banned phrase Detector.
We can now create GuardrailRunnable objects from these Guardrails and use them in a chain. Additionally, these objects are compatible with LCEL
This chain has the following steps:
  1. The input query is passed through the input Guardrail.
  2. The response from the input Guardrail is passed to the prompt template. The prompt template uses the guardrail_response_message field from the input Guardrail, which contains the sanitized query
  3. The prompt template is passed to the model, and then an output parser which converts the output into a string
  4. The first lambda function simply converts the string output into a dictionary with the query key containing the LLM output
  5. The output Guardrail scans the LLM output, and the final lambda simply returns the final response message by reading the guardrail_response_message field from the output Guardrail
This chain can be invoked using either a string, or a dictionary with the query key.

Branched Chains using Guardrails

Normally, LangChain chains execute end-to-end unless an exception or error arises. In the chain above, the Guardrail response message from the input runnable is passed to the prompt template. This means that whenever the input Guardrail is triggered, the blocked response message is sent to the LLM. Instead of doing this, you can use Langchain’s RunnableBranch to create execution paths that can be executed depending on whether or not a Guardrail was triggered.
Last modified on July 14, 2026