Download the PHP package muyki-labs/laravel-guardrails without Composer
On this page you can find all versions of the php package muyki-labs/laravel-guardrails. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download muyki-labs/laravel-guardrails
More information about muyki-labs/laravel-guardrails
Files in muyki-labs/laravel-guardrails
Package laravel-guardrails
Short Description A safety layer for LLM input and output in Laravel: a fluent, composable pipeline of guards (PII redaction, prompt-injection detection, moderation, schema validation) plus a provider-agnostic wrapper for any LLM call.
License MIT
Homepage https://github.com/muyki-labs/laravel-guardrails
Informations about the package laravel-guardrails
Laravel Guardrails
A safety layer for LLM input and output in Laravel. Laravel Guardrails validates, sanitizes, and (optionally) blocks the data flowing in and out of language models so teams can ship AI features that are compliant and predictable — PII redaction, prompt-injection detection, moderation, and schema validation behind one fluent, composable pipeline.
It is provider-agnostic and ships with zero required external services: regex PII detection, heuristic prompt-injection and moderation, and Laravel-validation-backed schema checks all work out of the box. Every detector is a pluggable, extend()-able manager, so you can swap in an NER service, an OpenAI moderation driver, or an LLM-as-judge without touching call sites.
Installation
Publish the config:
If you want the persisted audit log, publish and run the migration:
Requires PHP 8.3+ and Laravel 12 or 13.
The pipeline
Guardrails::for($value) returns a fluent pipeline. Chain guards, then terminate with one of:
guard()— run the guards, apply the violation strategy, and return the resolved value (or throw).check()— run the guards and return aGuardResult(passed,value,violations) without throwing.passes()/fails()— boolean shortcuts.
Input guards
| Method | What it does |
|---|---|
stripPII(array $types = [], ?string $strategy = null) |
Redact detected PII. Strategy: mask, hash, tokenize, remove. |
blockPromptInjection() |
Flag jailbreak / instruction-override attempts. |
maxTokens(int $max) |
Reject (or truncate) input over a token budget. |
maxLength(int $max) |
Reject (or truncate) input over a character length. |
allow(array $terms) |
Require the input to mention at least one allowed term. |
deny(array $terms) |
Reject input that contains a denied term. |
Output guards
| Method | What it does |
|---|---|
validateSchema(array\|Closure $rules) |
Validate decoded JSON/array against Laravel rules or a callback. |
blockToxic() / moderate() |
Run moderation over the output. |
noPII(array $types = []) |
Fail if the model echoed PII back. |
Custom guards
Wrapping an LLM call
Guardrails::pipe() wraps any callable: input guards run before the call, output guards after. With the retry strategy a rejected output re-invokes the model.
The callable is provider-agnostic, so it works with Prism, laravel/ai, or your own client. Input violations always fail closed (they cannot be "retried"); only output guards drive the retry loop.
Violation strategies
Set per pipeline with onViolation() (default comes from config('guardrails.default_strategy')).
throw— throw aGuardrailViolation(fail closed, the default).redact— return the sanitized value produced by the guards.retry— (LLM wrapper) re-run the call; in a standalone pipeline this fails closed likethrow.null— returnnullinstead of the offending value.
Policies
Named, reusable guard sets live in config/guardrails.php:
PII detection & redaction
The built-in regex detector recognises email, phone, IBAN, Luhn-checked credit card, Turkish national ID (TCKN, checksum-validated), and IPv4. Patterns and the enabled types are configurable under guardrails.pii. Redaction strategies:
mask— replace with a label, e.g.[EMAIL].remove— strip the value entirely.hash— replace with a stable[[EMAIL:abc123...]]token.tokenize— reversible: the original is stored in a cache vault and can be restored.
Register a custom detector (e.g. an NER provider):
The moderation and prompt-injection subsystems work the same way via ModerationManager::extend() and InjectionManager::extend().
Events
Bind to any of these (e.g. for audit logging, dashboards, or alerting):
| Event | Fired when |
|---|---|
GuardrailPassed |
A pipeline completes with no hard violations. |
GuardrailViolated |
One or more guards reported a violation. |
InputRedacted |
An input guard sanitized the value (e.g. PII redaction). |
OutputBlocked |
An output pipeline reported a violation. |
GuardrailRetried |
The LLM wrapper re-ran the call under the retry strategy. |
Audit log
Set guardrails.audit.enabled to true to persist violations for a compliance trail (KVKK / GDPR). Drivers: database (queryable), log (writes to the Laravel logger), or null.
Enable scheduled pruning under prune.schedule in the config.
Commands
Testing
License
The MIT License (MIT). See LICENSE.md.
All versions of laravel-guardrails with dependencies
illuminate/console Version ^12.0 || ^13.0
illuminate/contracts Version ^12.0 || ^13.0
illuminate/database Version ^12.0 || ^13.0
illuminate/support Version ^12.0 || ^13.0
illuminate/validation Version ^12.0 || ^13.0
nesbot/carbon Version ^3.0