Skip to main content

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
Perf’s Schema Enforcement:
  • 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

Response

When schema enforcement is active, responses include validation metadata:

Validation Fields

Auto-Repair

When schema_strict: false (default), Perf attempts to repair common issues:

Example: Auto-Repair in Action

LLM returns:

Supported Semantic Types

Example: Semantic Correction in Action

Request with semantic types:
Response with corrections:

Semantic Validation Response Fields

Strict Mode

Enable strict mode to disable auto-repair:
In strict mode:
  • 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, null
  • object, array

String Formats

  • email, uri, date, date-time, uuid

Validation Keywords

  • required, properties, additionalProperties
  • minLength, maxLength, pattern
  • minimum, maximum, exclusiveMinimum, exclusiveMaximum
  • minItems, maxItems, uniqueItems
  • enum, const

Composition

  • allOf, anyOf, oneOf
  • $ref (local references only)

Best Practices

1. Keep Schemas Simple

2. Use Descriptive Names

Add title and description to help the LLM understand:

3. Prefer Enums for Categories

4. Set Reasonable Defaults

Use default for optional fields:

Error Handling

Validation Failed

If validation fails after retries:

Invalid Schema

If your schema is invalid:

Plan Limits