Principle:Predibase Lorax JSON Schema Definition
| Knowledge Sources | |
|---|---|
| Domains | Structured_Output, API_Design |
| Last Updated | 2026-02-08 02:00 GMT |
Overview
A schema specification pattern that defines the expected structure of model output using JSON Schema, enabling compile-time validation of generated text against a formal grammar.
Description
JSON Schema Definition provides the declarative specification for structured output generation. Instead of relying on prompt engineering to coerce a model into producing valid JSON, the schema is compiled into a finite state machine (FSM) that constrains token generation at each step.
The schema can be provided in two ways:
- Pydantic model: Use model_json_schema() to auto-generate the JSON Schema
- Raw JSON Schema dict: Hand-craft the schema directly
The ResponseFormat type wraps the schema with a type field (json_object) to signal to the server that constrained decoding should be applied.
Usage
Use this principle when you need guaranteed valid JSON output from the model. Define a JSON Schema (either via Pydantic or manually) and pass it as the response_format parameter.
Theoretical Basis
JSON Schema defines the grammar that constrains generation:
Pseudo-code:
# Schema definition approaches
# 1. From Pydantic model
from pydantic import BaseModel
class Answer(BaseModel):
name: str
score: float
tags: List[str]
schema = Answer.model_json_schema()
# 2. Manual JSON Schema
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"score": {"type": "number"},
},
"required": ["name", "score"]
}
# Schema → Regex → FSM → Token constraints