How to Intermediate · 3 min read

Fix Guardrails validation always failing

Quick answer
Guardrails validation fails when the schema is incorrectly defined or the Guardrails SDK is outdated. Use the latest guardrails Python package, ensure your schema matches the expected format, and handle validation errors properly to fix validation failures.

PREREQUISITES

  • Python 3.8+
  • pip install guardrails>=0.3.0
  • Basic knowledge of JSON Schema or Pydantic
  • OpenAI API key or relevant AI API key

Setup

Install the latest guardrails package and set your API key as an environment variable.

bash
pip install guardrails>=0.3.0

Step by step

Define a correct Guardrails schema and integrate it with your AI client to validate responses properly.

python
import os
from guardrails import Guard
from openai import OpenAI

# Load your Guardrails schema (YAML or JSON format)
schema_path = "schema.yaml"
guard = Guard.from_path(schema_path)

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Example prompt
prompt = "Tell me a joke about cats."

# Generate completion
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

# Validate the response with Guardrails
try:
    validated_output = guard.parse(response.choices[0].message.content)
    print("Validated output:", validated_output)
except Exception as e:
    print("Validation failed:", e)
output
Validated output: {'joke': 'Why did the cat sit on the computer? Because it wanted to keep an eye on the mouse!'}

Common variations

  • Use guard.parse_async() for async validation.
  • Update your schema to match the AI model's output format exactly.
  • Use Pydantic models inside Guardrails for stricter type validation.
python
import asyncio

async def async_validate():
    validated = await guard.parse_async(response.choices[0].message.content)
    print("Async validated output:", validated)

asyncio.run(async_validate())
output
Async validated output: {'joke': 'Why did the cat sit on the computer? Because it wanted to keep an eye on the mouse!'}

Troubleshooting

  • If validation always fails, check your schema syntax and ensure it matches the AI output structure.
  • Update guardrails to the latest version to avoid bugs.
  • Print raw AI output before validation to debug mismatches.
  • Use explicit error handling to catch and log validation errors.
python
try:
    validated_output = guard.parse(response.choices[0].message.content)
except Exception as e:
    print("Raw output:", response.choices[0].message.content)
    print("Validation error:", e)
output
Raw output: {"joke": "Why did the cat sit on the computer? Because it wanted to keep an eye on the mouse!"}
Validation error: ValidationError: ...

Key Takeaways

  • Always keep your Guardrails schema in sync with the AI model's output format.
  • Use the latest Guardrails SDK version to avoid validation bugs.
  • Print and inspect raw AI outputs to debug validation failures.
  • Handle validation exceptions explicitly to improve error visibility.
Verified 2026-04 · gpt-4o
Verify ↗