How to beginner · 3 min read

How to install Guardrails AI

Quick answer
Install guardrails using pip install guardrails in your Python environment. This package enables you to define and enforce safety rules on AI model outputs easily.

PREREQUISITES

  • Python 3.8+
  • pip installed
  • Basic Python environment setup

Setup

Install the guardrails package via pip to start using Guardrails AI. Ensure you have Python 3.8 or higher installed.

bash
pip install guardrails

Step by step

Here is a simple example to create a Guardrails AI rail that validates AI responses against a schema.

python
from guardrails import Guard

# Define a simple rail schema as a string
rail_definition = '''
---
name: example_rail
steps:
  - name: user_input
    type: text
  - name: ai_response
    type: text
    constraints:
      - required
'''

# Create a Guard instance
guard = Guard.from_rail_string(rail_definition)

# Example user input
user_input = "Tell me a joke."

# Run the guard validation
result = guard.invoke(user_input)

print("Validated AI response:", result.ai_response)
output
Validated AI response: <AI model output validated by Guardrails>

Common variations

You can integrate Guardrails with various AI providers by passing your AI client to the Guard instance. Guardrails supports synchronous and asynchronous usage patterns.

python
import asyncio
from guardrails import Guard

rail_definition = '''
---
name: async_rail
steps:
  - name: user_input
    type: text
  - name: ai_response
    type: text
    constraints:
      - required
'''

async def main():
    guard = Guard.from_rail_string(rail_definition)
    user_input = "Give me a fun fact."
    result = await guard.ainvoke(user_input)
    print("Async validated AI response:", result.ai_response)

asyncio.run(main())
output
Async validated AI response: <AI model output validated by Guardrails>

Troubleshooting

  • If you see ModuleNotFoundError, ensure guardrails is installed in your active Python environment.
  • For schema validation errors, check your rail YAML syntax carefully.
  • Use pip show guardrails to verify the installed version.

Key Takeaways

  • Use pip install guardrails to install Guardrails AI quickly.
  • Define rails in YAML to enforce AI output constraints and safety.
  • Guardrails supports both synchronous and asynchronous Python usage.
  • Validate your rail schemas to avoid runtime errors.
  • Integrate Guardrails with any AI client by passing it to the Guard instance.
Verified 2026-04
Verify ↗