Using the GuardrailsOutputParser in LangChain for Structured Output

Posted: Jan 28, 2025.

The GuardrailsOutputParser is a powerful tool in LangChain that helps enforce structure and validation on LLM outputs using the Guardrails library. This parser ensures that LLM responses conform to specific schemas and validation rules.

What is GuardrailsOutputParser?

GuardrailsOutputParser is an output parser that uses the Guardrails library to validate and structure LLM outputs. It's particularly useful when you need to:

  • Enforce specific data structures in LLM outputs
  • Validate the format and content of responses
  • Convert free-form text into structured data
  • Handle parsing errors with retries

Reference

Here are the key methods available in GuardrailsOutputParser:

MethodDescription
from_rail(rail_file, num_reasks=1)Creates a parser from a Guardrails rail file
from_rail_string(rail_str, num_reasks=1)Creates a parser from a rail string
from_pydantic(output_class, num_reasks=1)Creates a parser from a Pydantic model
parse(text)Parses raw text into structured output
get_format_instructions()Gets formatting instructions for the LLM

How to Use GuardrailsOutputParser

1. Creating a Parser from a Rail File

from langchain_community.output_parsers import GuardrailsOutputParser

# Create parser from a rail file
parser = GuardrailsOutputParser.from_rail("path/to/rail_file.rail")

2. Creating a Parser from a Rail String

# Define rail string with validation rules
rail_str = """
<rail version="0.1">
<output>
    <object>
        <string name="name" description="The person's name" />
        <integer name="age" description="The person's age" />
    </object>
</output>
</rail>
"""

# Create parser from rail string
parser = GuardrailsOutputParser.from_rail_string(rail_str)

3. Using with a Language Model Chain

from langchain_community.chat_models import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

# Create the chain
llm = ChatOpenAI()
prompt = ChatPromptTemplate.from_template(
    """Extract the person's information from the text:
    {text}
    
    {format_instructions}
    """
)

chain = prompt | llm | parser

# Run the chain
text = "John is 30 years old"
result = chain.invoke({
    "text": text,
    "format_instructions": parser.get_format_instructions()
})

print(result)  # {'name': 'John', 'age': 30}

4. Creating a Parser from a Pydantic Model

from pydantic import BaseModel

class Person(BaseModel):
    name: str
    age: int

# Create parser from Pydantic model
parser = GuardrailsOutputParser.from_pydantic(Person)

5. Handling Parsing Errors with Retries

# Create parser with multiple retry attempts
parser = GuardrailsOutputParser.from_rail_string(
    rail_str,
    num_reasks=3  # Will try up to 3 times if parsing fails
)

# Use with error handling
try:
    result = parser.parse("Invalid response from LLM")
except Exception as e:
    print(f"Failed to parse after 3 attempts: {e}")

This parser is particularly useful when you need to ensure that your LLM outputs follow a specific structure and meet validation requirements. By using rail files or strings, you can define complex validation rules and data structures that your outputs must conform to.

Remember that GuardrailsOutputParser will automatically attempt to fix invalid outputs by re-asking the LLM, making it more robust in production environments where reliable structured output is critical.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs