Download the PHP package edgaras/llm-json-cleaner without Composer
On this page you can find all versions of the php package edgaras/llm-json-cleaner. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download edgaras/llm-json-cleaner
More information about edgaras/llm-json-cleaner
Files in edgaras/llm-json-cleaner
Package llm-json-cleaner
Short Description A PHP library that ensures strict JSON extraction and schema validation from LLM API responses, preventing malformed or unexpected output.
License MIT
Homepage https://github.com/Edgaras0x4E/LLM-JSON-Cleaner
Informations about the package llm-json-cleaner
LLM-JSON-Cleaner
PHP library for sanitizing JSON responses from LLM APIs and validating them against a specified JSON schema.
Features
- JSON Response Cleaning: Remove unwanted artifacts.
- Schema Validation: Validate and enforce JSON schema constraints.
Installation
Install the package via Composer:
Usage
Extracting JSON from LLM Responses
Cleaning Malformed JSON
JsonCleaner::extract() automatically fixes common LLM output issues:
Validating JSON Against a Schema
Validation Rules
| Rule | Description |
|---|---|
required |
Field must be present and non-empty |
nullable |
Field may be null (skips other rules when null) |
string |
Must be a string |
integer |
Must be an integer |
float |
Must be a float or integer |
numeric |
Must be numeric (int, float, or numeric string) |
boolean |
Must be a boolean |
array |
Must be an array |
email |
Must be a valid email address |
url |
Must be a valid URL |
date |
Must be a valid date string |
min:N |
Minimum value (numbers) or minimum item count (arrays) |
max:N |
Maximum value (numbers) or maximum item count (arrays) |
in:a,b,c |
Value must be one of the listed options |
Fields without required are optional - missing fields won't trigger errors, but present fields are still validated.
Nested array items are validated using wildcard dot notation: items.*.field_name
Unexpected fields (not defined in schema) are detected at all nesting levels.
Changelog
v1.1.0
JsonCleaner
- Added automatic trailing comma removal (
{"a": 1,}) - Added single-quote to double-quote conversion (
{'key': 'value'}) - Added unquoted key detection and quoting (
{key: "value"}) - Added multiline string value sanitization (literal newlines inside JSON strings)
- Added top-level JSON array extraction (
[...]embedded in text) - All sanitization is string-aware - content inside quoted values is never corrupted
JsonValidator
- Added
nullablerule - allowsnullvalues and skips further validation - Added
floatrule - accepts floats and integers - Added
numericrule - accepts any numeric value including numeric strings - Added
email,url,datevalidation rules - Added optional field support - fields without
requireddon't error when missing - Added unexpected nested field detection inside wildcard arrays (not just top-level)
- Added sequential array check for wildcard (
.*) paths - associative arrays are rejected - Fixed
requiredrule to allowfalseboolean values
General
- Tested and compatible with PHP 8.4 and PHP 8.5