<!-- source: https://deepwiki.com/MervinPraison/PraisonAI/6-safety-and-validation -->

## Model Capability Validation

PraisonAI validates model capabilities before attempting operations to prevent runtime errors. The system uses LiteLLM's capability detection functions as the primary source.

### Capability Checks

The `model_capabilities` module provides capability detection functions:

**Title: Capability Validation Flow**

```

```

### LiteLLM Integration

The system uses LiteLLM's helper functions for accurate capability detection:

```

```

This ensures capability detection stays up-to-date with LiteLLM's maintained model database.

Sources: [src/praisonai-agents/praisonaiagents/llm/model_capabilities.py1-312](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/llm/model_capabilities.py#L1-L312) [src/praisonai-agents/praisonaiagents/llm/model_capabilities.py50-76](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/llm/model_capabilities.py#L50-L76) [src/praisonai-agents/praisonaiagents/llm/model_capabilities.py78-104](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/llm/model_capabilities.py#L78-L104)

## AutoAgents Configuration Validation

The `AutoAgents` class validates auto-generated agent configurations before execution to prevent malformed setups.

### Validation Flow

**Title: AutoAgents Validation Process**

```

```

### Validation Checks

The validation system performs multiple checks on generated configurations:

```

```

### Retry with Feedback

When validation fails, the system retries with detailed feedback:

```

```

This iterative validation ensures that auto-generated configurations meet structural requirements before agent execution begins.

Sources: [src/praisonai-agents/praisonaiagents/agents/autoagents.py245-272](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/autoagents.py#L245-L272) [src/praisonai-agents/praisonaiagents/agents/autoagents.py273-450](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/autoagents.py#L273-L450) [src/praisonai-agents/praisonaiagents/agents/autoagents.py452-511](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/autoagents.py#L452-L511)

# Safety and Validation

This page documents PraisonAI's safety and validation mechanisms that ensure agent reliability, security, and compliance. The framework implements multiple layers of protection across the execution pipeline.

## Overview

PraisonAI provides five complementary safety systems:

Additional validation systems include:

- Model Capability Validation : Ensure LLMs support required features before use
- Configuration Validation : Validate agent and task configurations (e.g., AutoAgents)

These systems work together to prevent errors, block dangerous operations, and ensure high-quality outputs.

## Safety Architecture

### Multi-Layer Protection System

PraisonAI implements safety at multiple layers of the execution pipeline:

**Title: Safety Architecture Layers**

```

```

**Title: Safety System Integration Points**

```

```

Sources: [src/praisonai-agents/praisonaiagents/agent/agent.py](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agent/agent.py) [src/praisonai-agents/praisonaiagents/task/task.py](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/task/task.py) [src/praisonai-agents/praisonaiagents/hooks/](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/hooks/) [src/praisonai-agents/praisonaiagents/policy/](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/policy/) [src/praisonai-agents/praisonaiagents/approval.py](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/approval.py)

## Guardrails System

Guardrails validate task outputs to ensure they meet quality and safety requirements. The system supports three validation approaches:

### Guardrail Types

**Title: Guardrail Execution Flow**

```

```

### Guardrail Configuration

Guardrails can be configured at both Agent and Task levels:

```

```

For detailed guardrail implementation, validation logic, and examples, see page 6.1.

Sources: [src/praisonai-agents/praisonaiagents/task/task.py161-215](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/task/task.py#L161-L215) [src/praisonai-agents/praisonaiagents/task/task.py389-430](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/task/task.py#L389-L430) [src/praisonai-agents/praisonaiagents/guardrails/llm_guardrail.py14-130](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/guardrails/llm_guardrail.py#L14-L130) [src/praisonai-agents/praisonaiagents/guardrails/guardrail_result.py13-43](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/guardrails/guardrail_result.py#L13-L43)

## Human Approval System

The human approval system provides human-in-the-loop control for dangerous operations. Tools are classified by risk level, and high-risk operations require explicit user approval before execution.

### Risk Classification

**Title: Approval Flow**

```

```

### Approval Configuration

The system supports custom approval callbacks and context-aware approval tracking:

```

```

For detailed approval workflow, callback implementation, and planning approval integration, see page 6.2.

Sources: [src/praisonai-agents/praisonaiagents/approval.py](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/approval.py) [examples/python/general/human_approval_example.py](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/python/general/human_approval_example.py)

## Hooks System

The hooks system provides lifecycle interception points for logging, validation, modification, and control flow across the agent execution pipeline.

### Hook Events

**Title: Hook Event Lifecycle**

```

```

The `HookEvent` enum defines all interception points:

### Hook Registry and Runner

The `HookRegistry` stores hook functions and the `HookRunner` executes them:

```

```

**Title: Hook Execution Flow**

```

```

### Hook Configuration

Hooks can be registered globally or per-agent:

```

```

### Hook Input Data

Hook functions receive event-specific data objects:

Sources: [src/praisonai-agents/praisonaiagents/hooks/](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/hooks/) [examples/python/hooks/](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/python/hooks/) [README.md1044-1081](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L1044-L1081)

## Policy Engine

The policy engine provides declarative resource-based access control using allow/deny rules.

### Policy Structure

**Title: Policy Evaluation Flow**

```

```

A `Policy` contains multiple `PolicyRule` objects:

```

```

### Policy Engine Integration

The `PolicyEngine` evaluates policies before operations:

**Title: PolicyEngine Evaluation**

```

```

### Policy Configuration

Policies can be loaded from JSON files or created programmatically:

```

```

Sources: [src/praisonai-agents/praisonaiagents/policy/](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/policy/) [examples/policy/](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/policy/) [README.md1158-1196](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L1158-L1196)

## Quality Assessment

The Memory system implements quality assessment to score task outputs and determine which results should be stored in long-term memory.

### Quality Metrics

The `Memory.compute_quality_score()` method combines multiple metrics into a single quality score:

**Title: Quality Score Computation**

```

```

### Quality-Based Storage

Quality scores determine storage behavior:

```

```

### Task Integration

Tasks can enable quality checking through the `quality_check` parameter:

```

```

Sources: [src/praisonai-agents/praisonaiagents/memory/memory.py](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/memory/memory.py) [src/praisonai-agents/praisonaiagents/task/task.py](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/task/task.py)

## Error Handling and Validation Flow

### Approval Error Handling

The approval system in [src/praisonai-agents/praisonaiagents/approval.py](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/approval.py) handles various error conditions:

### Validation Retry Mechanism

The `Process` class implements automatic retry with configurable limits:

**Title: Task Retry Flow**

```

```

The retry context includes detailed feedback:

```

```

### Hook and Policy Error Handling

Hooks and policies provide structured error handling:

Sources: [src/praisonai-agents/praisonaiagents/approval.py](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/approval.py) [src/praisonai-agents/praisonaiagents/process/process.py](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/process/process.py) [src/praisonai-agents/praisonaiagents/hooks/](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/hooks/) [src/praisonai-agents/praisonaiagents/policy/](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/policy/)

## Context Management and State

### Approval Context

The approval system uses `ContextVar` to manage approved operations within execution contexts:

```

```

This context management prevents redundant approval prompts for the same tool within a single execution session while maintaining security across different contexts.

### Guardrail State Management

Guardrails maintain validation state through the `GuardrailResult` class, which provides structured feedback for validation outcomes:

```

```

Sources: [src/praisonai-agents/praisonaiagents/approval.py28-59](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/approval.py#L28-L59) [src/praisonai-agents/praisonaiagents/guardrails/guardrail_result.py13-43](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/guardrails/guardrail_result.py#L13-L43)
