PHPackages                             gazu1986/laravel-ai-validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. gazu1986/laravel-ai-validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

gazu1986/laravel-ai-validator
=============================

Validate, retry, and type-cast structured AI output against Laravel validation rules or JSON schemas.

1.0.0(2mo ago)00MITPHP ^8.2

Since Feb 26Pushed 1mo agoCompare

[ Source](https://github.com/gazu1986/laravel-ai-validator)[ Packagist](https://packagist.org/packages/gazu1986/laravel-ai-validator)[ Docs](https://github.com/gazu1986/laravel-ai-validator)[ RSS](/packages/gazu1986-laravel-ai-validator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (7)Versions (2)Used By (0)

Laravel AI Validator
====================

[](#laravel-ai-validator)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a6e0de8d1336b05ddf7032e5d82b6e327f60752033f75378b8f2d1d56bd124e9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f67617a75313938362f6c61726176656c2d61692d76616c696461746f722e737667)](https://packagist.org/packages/gazu1986/laravel-ai-validator)[![Total Downloads](https://camo.githubusercontent.com/317803e89b9936ea1d40d585445c8a0c92cbdc9ef73ce737a5d360ef577e292d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f67617a75313938362f6c61726176656c2d61692d76616c696461746f722e737667)](https://packagist.org/packages/gazu1986/laravel-ai-validator)[![License](https://camo.githubusercontent.com/180cf71091ed9a33949954fe21c8607ae1522570db1f0f593b3aa5e5267b6da1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f67617a75313938362f6c61726176656c2d61692d76616c696461746f722e737667)](https://packagist.org/packages/gazu1986/laravel-ai-validator)

**Validate, retry, and type-cast structured AI output using Laravel's validation rules.**

Stop writing defensive JSON parsing code for AI responses. Define your expected output schema with familiar Laravel rules, and let this package handle validation, retries with error context, and type casting — across any AI provider.

---

The Problem
-----------

[](#the-problem)

Every time you call an AI API expecting structured JSON, you end up writing:

```
// 😩 The reality of working with AI APIs
$response = $openai->chat($prompt);
$json = json_decode($response, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    // retry? log? throw? strip markdown fences?
}

if (!isset($json['sentiment']) || !in_array($json['sentiment'], ['positive', 'negative'])) {
    // retry with error context? give up?
}

// ... 30 more lines of defensive code
```

The Solution
------------

[](#the-solution)

```
// 😎 With Laravel AI Validator
$result = AiValidator::validate(
    'Analyze the sentiment of this review: "Great product!"',
    new SentimentSchema()
);

$result->dataOrFail(); // Typed, validated data — or throws with full attempt history
```

---

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 12.x

Installation
------------

[](#installation)

```
composer require gazu1986/laravel-ai-validator
```

Publish the config:

```
php artisan vendor:publish --tag=ai-validator-config
```

Add your API key to `.env`:

```
# Use any provider: openai, anthropic, ollama
AI_VALIDATOR_PROVIDER=openai
OPENAI_API_KEY=sk-...

# Or Anthropic
# AI_VALIDATOR_PROVIDER=anthropic
# ANTHROPIC_API_KEY=sk-ant-...
```

---

Quick Start
-----------

[](#quick-start)

### 1. Create a Schema

[](#1-create-a-schema)

```
php artisan make:ai-schema ProductReviewSchema
```

This creates `app/AiSchemas/ProductReviewSchema.php`:

```
use gazu1986\AiValidator\Support\StructuredOutput;

class ProductReviewSchema extends StructuredOutput
{
    public function rules(): array
    {
        return [
            'sentiment'  => ['required', 'string', 'in:positive,negative,mixed,neutral'],
            'confidence' => ['required', 'numeric', 'min:0', 'max:1'],
            'summary'    => ['required', 'string', 'min:10', 'max:200'],
            'pros'       => ['required', 'array', 'min:1'],
            'pros.*'     => ['string', 'max:100'],
            'cons'       => ['present', 'array'],
            'cons.*'     => ['string', 'max:100'],
        ];
    }
}
```

### 2. Validate AI Output

[](#2-validate-ai-output)

```
use gazu1986\AiValidator\Facades\AiValidator;

$result = AiValidator::validate(
    'Analyze this review: "Amazing laptop, but the keyboard feels cheap"',
    new ProductReviewSchema()
);

if ($result->success) {
    $data = $result->data;
    // ['sentiment' => 'mixed', 'confidence' => 0.85, 'summary' => '...', ...]
}
```

### 3. Or Use Inline Rules (No Schema Class)

[](#3-or-use-inline-rules-no-schema-class)

```
$result = AiValidator::validateWithRules(
    'Extract the person name and age from: "John is 30 years old"',
    [
        'name' => ['required', 'string'],
        'age'  => ['required', 'integer', 'min:0'],
    ]
);

$result->data; // ['name' => 'John', 'age' => 30]
```

---

How It Works
------------

[](#how-it-works)

```
┌──────────┐     ┌──────────────┐     ┌───────────┐     ┌──────────┐
│  Prompt  │────▶│  AI Provider │────▶│  Parse    │────▶│ Validate │
│          │     │  (send)      │     │  JSON     │     │  Rules   │
└──────────┘     └──────────────┘     └───────────┘     └──────────┘
                        ▲                                     │
                        │              ┌───────────┐          │
                        └──────────────│  Retry    │◀─────────┘
                       (with errors)   │  Engine   │    (if failed)
                                       └───────────┘
                                                          │
                                                    ┌─────▼──────┐
                                                    │  Cast to   │
                                                    │  DTO/Type  │
                                                    └────────────┘

```

1. **Send** — Your prompt + auto-generated system prompt → AI provider
2. **Parse** — Strip markdown fences, extract JSON, handle edge cases
3. **Validate** — Run through Laravel's validator with your rules
4. **Retry** — If failed, append error context and retry (configurable)
5. **Cast** — Transform validated data into your DTO/typed object

---

Type-Safe Casting
-----------------

[](#type-safe-casting)

Cast validated data to a typed DTO:

```
class ProductReviewSchema extends StructuredOutput
{
    public function rules(): array { /* ... */ }

    public function cast(array $validatedData): ProductReviewDTO
    {
        return new ProductReviewDTO(
            sentiment: Sentiment::from($validatedData['sentiment']),
            confidence: (float) $validatedData['confidence'],
            summary: $validatedData['summary'],
            pros: $validatedData['pros'],
            cons: $validatedData['cons'],
        );
    }
}

// Usage
$result = AiValidator::validate($prompt, new ProductReviewSchema());
$dto = $result->dataOrFail(); // ProductReviewDTO instance — or throws
```

---

Multiple Providers
------------------

[](#multiple-providers)

Switch providers per-request:

```
// Default provider (from config)
AiValidator::validate($prompt, $schema);

// Use Anthropic for this request
AiValidator::using('anthropic')->validate($prompt, $schema);

// Use Ollama for local development
AiValidator::using('ollama')->validate($prompt, $schema);
```

---

Retry &amp; Error Handling
--------------------------

[](#retry--error-handling)

### Automatic Retries

[](#automatic-retries)

The package retries with error context automatically:

```
// Override max attempts per-request
$result = AiValidator::maxAttempts(5)->validate($prompt, $schema);

// Check what happened
$result->attemptCount;   // How many attempts were needed
$result->totalTokens();  // Total tokens consumed across all attempts
$result->attempts;       // Array of AttemptLog objects
```

### Inspecting Failures

[](#inspecting-failures)

```
$result = AiValidator::validate($prompt, $schema);

if (!$result->success) {
    // What went wrong on the last attempt?
    $lastAttempt = end($result->attempts);
    $lastAttempt->jsonValid;         // Was it valid JSON?
    $lastAttempt->schemaValid;       // Did it pass validation?
    $lastAttempt->validationErrors;  // Laravel validation errors
    $lastAttempt->rawResponse;       // Raw AI response text
}

// Or throw with full context
try {
    $result->dataOrFail();
} catch (ValidationFailedException $e) {
    $e->lastErrors();  // Validation errors from final attempt
    $e->attempts;      // Full attempt history
}
```

---

Custom System Prompts
---------------------

[](#custom-system-prompts)

The package auto-generates system prompts from your rules, but you can override:

```
class ContactExtractionSchema extends StructuredOutput
{
    public function rules(): array { /* ... */ }

    public function systemPrompt(): string
    {
        return  env('AI_VALIDATOR_PROVIDER', 'openai'),

    'providers' => [
        'openai' => [
            'api_key' => env('OPENAI_API_KEY'),
            'model' => env('AI_VALIDATOR_OPENAI_MODEL', 'gpt-4o'),
            'temperature' => 0.0,
        ],
        'anthropic' => [
            'api_key' => env('ANTHROPIC_API_KEY'),
            'model' => env('AI_VALIDATOR_ANTHROPIC_MODEL', 'claude-sonnet-4-5-20250929'),
        ],
        'ollama' => [
            'base_url' => env('OLLAMA_BASE_URL', 'http://localhost:11434'),
            'model' => 'llama3',
        ],
    ],

    'retry' => [
        'max_attempts' => 3,
        'backoff_ms' => 500,
        'backoff_multiplier' => 2.0,
    ],

    'cache' => [
        'enabled' => false,
        'ttl' => 3600,
    ],

    'logging' => [
        'enabled' => true,
        'log_prompts' => false,
        'log_responses' => false,
    ],
];
```

---

Testing
-------

[](#testing)

The package works seamlessly with Laravel's HTTP faking:

```
use Illuminate\Support\Facades\Http;

Http::fake([
    'api.openai.com/*' => Http::response([
        'choices' => [[
            'message' => ['content' => '{"name":"John","age":30}'],
            'finish_reason' => 'stop',
        ]],
        'model' => 'gpt-4o',
        'usage' => ['prompt_tokens' => 100, 'completion_tokens' => 50, 'total_tokens' => 150],
    ]),
]);

$result = AiValidator::validateWithRules('Extract info', [
    'name' => ['required', 'string'],
    'age' => ['required', 'integer'],
]);

expect($result->success)->toBeTrue();
```

Run the package tests:

```
composer test
```

---

Real-World Examples
-------------------

[](#real-world-examples)

Check the `examples/` directory for production-ready schemas:

- **ProductReviewSchema** — Sentiment analysis with typed DTO casting
- **ContactExtractionSchema** — Extract contacts with custom system prompt

---

Roadmap
-------

[](#roadmap)

- JSON Schema support (alongside Laravel rules)
- Streaming validation for long outputs
- Provider-native structured output (OpenAI JSON mode, Anthropic tool use)
- Artisan command to test schemas interactively
- Token budget limits per validation
- Event dispatching for monitoring integrations

---

Contributing
------------

[](#contributing)

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance87

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

76d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a367aed5bc3322b73ff9ec25a350975dc54a43847986bf911ef51011278d74c6?d=identicon)[gazu1986](/maintainers/gazu1986)

---

Top Contributors

[![gazu1986](https://avatars.githubusercontent.com/u/5903164?v=4)](https://github.com/gazu1986 "gazu1986 (2 commits)")

---

Tags

laravelvalidationjson-schemaaiopenairetryllmanthropicstructured output

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/gazu1986-laravel-ai-validator/health.svg)

```
[![Health](https://phpackages.com/badges/gazu1986-laravel-ai-validator/health.svg)](https://phpackages.com/packages/gazu1986-laravel-ai-validator)
```

###  Alternatives

[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k35.7M107](/packages/propaganistas-laravel-phone)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel and Lumen

3853.3M1](/packages/axlon-laravel-postal-code-validation)[wendelladriel/laravel-validated-dto

Data Transfer Objects with validation for Laravel applications

759569.4k13](/packages/wendelladriel-laravel-validated-dto)[laravel-validation-rules/credit-card

Validate credit card number, expiration date, cvc

2412.2M5](/packages/laravel-validation-rules-credit-card)[galahad/laravel-addressing

Laravel package providing addressing functionality

70316.6k](/packages/galahad-laravel-addressing)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
