Skip to main content
Validate task output with guardrails — failed checks retry automatically with feedback about what to fix. To enforce a typed contract instead of custom checks, use Structured Outputs; parse failures now surface via TaskResult.error.
The retry-with-feedback prompt wiring described below (upstream output + guardrail feedback injected into the next attempt’s prompt) requires a praisonaiagents build from 2026-07-28 or later (see upstream fixes 36da000 and de5a95d). On earlier builds task.validation_feedback was populated on the Task object, but the retry attempt’s LLM prompt was assembled without it — the retry ran with the same original prompt as attempt 1.
The user asks for output that must meet a guardrail; failed validation sends feedback into the next retry until the check passes or retries are exhausted.

How It Works

Quick Start

1

Install Package

First, install the PraisonAI Agents package:
2

Create Validation with Guardrails

The simplest way to add validation is using guardrails:

Validation Methods

PraisonAI offers two primary validation approaches: Guardrails provide inline validation with automatic retry and feedback mechanisms.

Function-Based Guardrails

LLM-Based Guardrails

For complex validation that requires understanding:

2. Decision-Based Validation Workflows

For complex validation flows with multiple validators:

How Validation Feedback Works

When validation fails, the system automatically:
  1. Captures the validation feedback including:
    • The validation decision (e.g., “retry”, “invalid”)
    • Detailed feedback about what was wrong
    • The original output that failed
    • Which validator made the decision
  2. Creates typed validation outcome (recommended):
  3. Passes feedback to retry task via context (legacy):
  4. Includes feedback in task context for the next attempt Under the hood the workflow process engine writes the feedback and any upstream output onto task._execution_context. The prompt builder folds this field into the retry attempt’s context. The field is deliberately preserved across the run_task/arun_task retry loop and reset by the Process engine before the next task is selected, so retries within the same attempt see the feedback and there is no cross-task leak.
Decision tasks expose task.validation_feedback (dict) with retry details. Use it in the next attempt’s prompt or handler.
Use the typed outcome for robust error handling:

Complete Examples

Example 1: Data Validation Pipeline

Example 2: Multi-Stage Validation

Example 3: Complex Validation with Context

Validation Feedback in Action

When validation fails, agents receive both typed outcomes and legacy feedback: Typed Outcome (Recommended):
Legacy Dict Format (Backward Compatibility):

Best Practices

Define specific, measurable criteria and return actionable feedback strings when validation fails. Include examples of valid output in task descriptions.
Use function guardrails for simple checks (length, format, required fields). Reserve LLM guardrails (string prompts) for subjective quality checks.
Use max_retries=3 as a default. Increase only when feedback is precise enough for the agent to self-correct.
Validate JSON schema, required sections, or word counts before expensive downstream tasks run.

Advanced Configuration

Retry Strategies

Custom Feedback Formatting

Common Validation Patterns

Word/Character Count

Content Requirements

Troubleshooting

  • Check validation criteria are achievable
  • Verify feedback is clear and actionable
  • Test validation function separately
  • Increase max_retries if needed
  • Ensure using proper validation return format
  • Check workflow connections
  • Verify decision task conditions
  • Enable verbose mode for debugging
  • Set appropriate max_retries
  • Implement retry counters
  • Add fallback conditions
  • Log validation attempts

Agent Run Outcomes

Typed validation outcomes and status handling

Guardrails

Deep dive into the guardrails system

Task Retry Policy

Per-task retry with exponential backoff

Workflows

Complex workflow patterns