Schema Enforcement
Schema Enforcement ensures your LLM outputs conform to a defined JSON schema. Perf validates responses, auto-repairs common issues, applies semantic corrections (like fixing negative prices), and guarantees you receive properly structured data.Why Schema Enforcement?
LLMs are unreliable at producing consistent JSON:- Missing required fields
- Wrong data types (string instead of number)
- Extra fields not in schema
- Malformed JSON syntax
- Markdown code blocks wrapping JSON
- Validates outputs against your JSON Schema
- Auto-repairs common issues (type coercion, format fixes)
- Semantic correction fixes domain-specific errors (negative prices, invalid ratings)
- Retries with better prompts if validation fails
- Guarantees schema-compliant responses
Using Schemas in Requests
Inline Schema
Pass a JSON Schema directly in your request:Schema by ID
Reference a saved schema by ID or slug:Project Default Schema
Set a default schema for your project in the Dashboard. All requests without an explicit schema will use it.Schema Parameters
| Parameter | Type | Description |
|---|---|---|
schema | object | Inline JSON Schema (Draft 2020-12) |
schema_id | string | ID or slug of a saved schema |
schema_strict | boolean | Disable auto-repair, fail on any mismatch (default: false) |
Response
When schema enforcement is active, responses include validation metadata:Validation Fields
| Field | Type | Description |
|---|---|---|
enabled | boolean | Schema validation was active |
passed | boolean | Final output passed validation |
repaired | boolean | Auto-repair was applied |
schema_source | string | inline, schema_id, or project_default |
Auto-Repair
Whenschema_strict: false (default), Perf attempts to repair common issues:
| Issue | Repair Action |
|---|---|
| String instead of number | Parse to number ("42" → 42) |
| Number instead of string | Convert to string (42 → "42") |
| String instead of boolean | Parse ("true" → true) |
| Markdown code blocks | Extract JSON from json ... |
| Extra whitespace | Trim and normalize |
| Missing optional fields | Leave as undefined |
Example: Auto-Repair in Action
LLM returns:Supported Semantic Types
| Type | Description | Auto-Correction |
|---|---|---|
currency_positive | Prices, amounts (must be positive) | -$999 → $999 |
currency_allow_negative | Values that can be negative | Validates magnitude only |
human_age | Person ages (0-130) | Clamps to valid range |
percentage | Percentages (0-100 or 0-1) | Normalizes scale |
rating_1_5 | 5-star ratings | 12 → 5 (clamps to max) |
rating_1_10 | 10-point ratings | Clamps to 1-10 range |
email | Email addresses | Format validation |
url | URLs | Format validation |
date_past | Historical dates | Flags future dates |
date_future | Upcoming dates | Flags past dates |
Example: Semantic Correction in Action
Request with semantic types:Semantic Validation Response Fields
| Field | Type | Description |
|---|---|---|
result | string | pass, corrected, or failed |
corrections | array | List of auto-corrections applied |
warnings | array | Non-blocking issues detected |
latency_ms | number | Validation latency in milliseconds |
Strict Mode
Enable strict mode to disable auto-repair:- No type coercion
- No format repairs
- Validation fails on any mismatch
- Returns error if validation fails after retries
Managing Schemas
Create Schema (Dashboard API)
List Schemas
Test Schema
Test a schema against sample data:Supported JSON Schema Features
Perf supports JSON Schema Draft 2020-12 with these features:Types
string,number,integer,boolean,nullobject,array
String Formats
email,uri,date,date-time,uuid
Validation Keywords
required,properties,additionalPropertiesminLength,maxLength,patternminimum,maximum,exclusiveMinimum,exclusiveMaximumminItems,maxItems,uniqueItemsenum,const
Composition
allOf,anyOf,oneOf$ref(local references only)
Best Practices
1. Keep Schemas Simple
2. Use Descriptive Names
Addtitle and description to help the LLM understand:
3. Prefer Enums for Categories
4. Set Reasonable Defaults
Usedefault for optional fields:
Error Handling
Validation Failed
If validation fails after retries:Invalid Schema
If your schema is invalid:Plan Limits
| Tier | Max Schemas | Auto-Repair | Semantic Validation |
|---|---|---|---|
| Starter | 3 | No | No |
| Pro | 10 | Yes | Yes |
| Growth | 50 | Yes | Yes |
| Enterprise | Unlimited | Yes | Yes |
Related Endpoints
- Chat API - Use schemas with chat completions
- Streaming API - Schema validation with streaming
- Logs API - View validation history