gazu1986/laravel-ai-validator
Composer 安装命令:
composer require gazu1986/laravel-ai-validator
包简介
Validate, retry, and type-cast structured AI output against Laravel validation rules or JSON schemas.
README 文档
README
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
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
// 😎 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
- PHP 8.2+
- Laravel 12.x
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
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
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)
$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
┌──────────┐ ┌──────────────┐ ┌───────────┐ ┌──────────┐
│ Prompt │────▶│ AI Provider │────▶│ Parse │────▶│ Validate │
│ │ │ (send) │ │ JSON │ │ Rules │
└──────────┘ └──────────────┘ └───────────┘ └──────────┘
▲ │
│ ┌───────────┐ │
└──────────────│ Retry │◀─────────┘
(with errors) │ Engine │ (if failed)
└───────────┘
│
┌─────▼──────┐
│ Cast to │
│ DTO/Type │
└────────────┘
- Send — Your prompt + auto-generated system prompt → AI provider
- Parse — Strip markdown fences, extract JSON, handle edge cases
- Validate — Run through Laravel's validator with your rules
- Retry — If failed, append error context and retry (configurable)
- Cast — Transform validated data into your DTO/typed object
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
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 & Error Handling
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
$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
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 <<<'PROMPT' You are a contact information extractor. Extract all people mentioned and their details. Respond with ONLY valid JSON matching the required schema. Set confidence between 0-1 for each extracted field. PROMPT; } }
Configuration
// config/ai-validator.php return [ 'default_provider' => 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
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
Check the examples/ directory for production-ready schemas:
- ProductReviewSchema — Sentiment analysis with typed DTO casting
- ContactExtractionSchema — Extract contacts with custom system prompt
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
Contributions are welcome! Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see LICENSE for more information.
gazu1986/laravel-ai-validator 适用场景与选型建议
gazu1986/laravel-ai-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「retry」 「laravel」 「ai」 「json-schema」 「openai」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gazu1986/laravel-ai-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gazu1986/laravel-ai-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gazu1986/laravel-ai-validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Emulated timeouts for synchronous operations.
Retry tasks that fail due to transient faults
Polling library for conditionally retry operations based on a result checker.
Retry policy plugin for the Testo testing framework.
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-26