Constraints
This section covers constraint features in dotjson that control the values generated by language models during decoding. These constraints ensure outputs meet specific requirements and follow defined patterns.
Enumerated Values with enum
The enum keyword restricts a value to a predefined set of options. It works with any JSON data type and forces the model to choose from specific values.
String Enumerations
Perfect for categorization and classification tasks:
{
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["pending", "processing", "completed", "failed"],
"description": "Current task status"
},
"priority": {
"type": "string",
"enum": ["low", "medium", "high", "critical"],
"description": "Task priority level"
},
"category": {
"type": "string",
"enum": ["bug", "feature", "enhancement", "documentation"],
"description": "Issue category"
}
},
"required": ["status", "priority", "category"]
}{
"status": "processing",
"priority": "high",
"category": "bug"
}Numeric Enumerations
Use specific numeric values for codes or levels:
{
"type": "object",
"properties": {
"httpStatus": {
"type": "integer",
"enum": [200, 201, 400, 401, 403, 404, 500],
"description": "HTTP status code"
},
"fibonacciPriority": {
"type": "integer",
"enum": [1, 2, 3, 5, 8, 13, 21],
"description": "Priority using Fibonacci sequence"
},
"discount": {
"type": "number",
"enum": [0.05, 0.10, 0.15, 0.20, 0.25],
"description": "Available discount rates"
}
},
"required": ["httpStatus", "fibonacciPriority", "discount"]
}{
"httpStatus": 404,
"fibonacciPriority": 8,
"discount": 0.15
}Complex Classification System
Combine multiple enums for sophisticated categorization:
{
"type": "object",
"properties": {
"ticket": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["incident", "request", "question", "task"],
"description": "Ticket type"
},
"severity": {
"type": "string",
"enum": ["critical", "major", "minor", "trivial"],
"description": "Issue severity"
},
"department": {
"type": "string",
"enum": ["IT", "HR", "Finance", "Operations", "Sales"],
"description": "Responsible department"
},
"resolution": {
"type": "string",
"enum": ["resolved", "workaround", "escalated", "closed", "duplicate"],
"description": "Resolution status"
}
},
"required": ["type", "severity", "department"]
}
},
"required": ["ticket"]
}{
"ticket": {
"type": "incident",
"severity": "major",
"department": "IT",
"resolution": "resolved"
}
}Tip
When using enums, provide context to the model about what each option represents. This helps the model make appropriate selections based on the input data.
Constant Values with const
The const keyword specifies an exact value that must be generated. This is useful for fixed metadata, identifiers, or when you want to provide context without allowing variation.
String Constants
Fix specific values in the output:
{
"type": "object",
"properties": {
"version": {
"type": "string",
"const": "1.0.0",
"description": "API version"
},
"source": {
"type": "string",
"const": "automated-system",
"description": "Data source identifier"
},
"generated_content": {
"type": "string",
"minLength": 50,
"description": "Model-generated content"
}
},
"required": ["version", "source", "generated_content"]
}{
"version": "1.0.0",
"source": "automated-system",
"generated_content": "This is the dynamically generated content that the model creates based on the input context and requirements."
}Object Constants
Fix entire object structures while allowing other fields to vary:
{
"type": "object",
"properties": {
"metadata": {
"type": "object",
"const": {
"format": "JSON",
"encoding": "UTF-8",
"standard": "RFC 8259"
},
"description": "Fixed metadata"
},
"content": {
"type": "object",
"properties": {
"title": { "type": "string" },
"body": { "type": "string" },
"tags": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["title", "body"]
}
},
"required": ["metadata", "content"]
}{
"metadata": {
"format": "JSON",
"encoding": "UTF-8",
"standard": "RFC 8259"
},
"content": {
"title": "Introduction to JSON Schema",
"body": "JSON Schema provides a powerful way to validate JSON data structures.",
"tags": ["json", "validation", "schema"]
}
}Using Constants for Context
Constants can guide the model’s behavior without explicit prompting:
{
"type": "object",
"properties": {
"genre": {
"type": "string",
"const": "science fiction",
"description": "Story genre"
},
"tone": {
"type": "string",
"const": "humorous",
"description": "Writing tone"
},
"story": {
"type": "string",
"minLength": 100,
"description": "Generated story"
}
},
"required": ["genre", "tone", "story"]
}{
"genre": "science fiction",
"tone": "humorous",
"story": "The spaceship's AI insisted on making dad jokes during the alien invasion. 'Why don't aliens ever land at airports?' it quipped while dodging laser fire. 'Because they're looking for space!' The crew groaned, wondering if surrender might be preferable to another pun."
}Advanced String Constraints
String Length Constraints
Control the length of generated strings using minLength and maxLength:
{
"type": "object",
"properties": {
"password": {
"type": "string",
"minLength": 8,
"maxLength": 20,
"description": "Password between 8-20 characters"
},
"username": {
"type": "string",
"maxLength": 15,
"description": "Username up to 15 characters"
}
},
"required": ["password", "username"]
}{
"password": "secureP@ssw0rd",
"username": "johndoe"
}Pattern Combinations
Create precise generation constraints with regular expressions:
{
"type": "object",
"properties": {
"productCode": {
"type": "string",
"pattern": "^[A-Z]{3}-[0-9]{4}-[A-Z0-9]{2}$",
"description": "Format: XXX-0000-XX"
},
"serialNumber": {
"type": "string",
"pattern": "^SN[0-9]{10}$",
"description": "Serial number starting with SN"
},
"batchId": {
"type": "string",
"pattern": "^(20[2-9][0-9])-(0[1-9]|1[0-2])-B[0-9]{3}$",
"description": "Format: YYYY-MM-B000"
}
},
"required": ["productCode", "serialNumber", "batchId"]
}{
"productCode": "ABC-1234-X9",
"serialNumber": "SN1234567890",
"batchId": "2024-03-B042"
}Format Constraints
Use built-in formats for common patterns:
{
"type": "object",
"properties": {
"appointment": {
"type": "object",
"properties": {
"date": {
"type": "string",
"format": "date",
"description": "Appointment date"
},
"time": {
"type": "string",
"format": "time",
"description": "Appointment time"
},
"patientEmail": {
"type": "string",
"format": "email",
"description": "Patient contact email"
},
"clinicWebsite": {
"type": "string",
"format": "uri",
"description": "Clinic website URL"
},
"appointmentId": {
"type": "string",
"format": "uuid",
"description": "Unique appointment identifier"
}
},
"required": ["date", "time", "patientEmail", "appointmentId"]
}
},
"required": ["appointment"]
}{
"appointment": {
"date": "2024-04-15",
"time": "14:30:00Z",
"patientEmail": "[email protected]",
"clinicWebsite": "https://clinic.example.com",
"appointmentId": "f47ac10b-58cc-4372-a567-0e02b2c3d479"
}
}Numeric Constraints
Range Constraints with Context
Combine min/max values with meaningful descriptions:
{
"type": "object",
"properties": {
"healthMetrics": {
"type": "object",
"properties": {
"heartRate": {
"type": "integer",
"minimum": 40,
"maximum": 200,
"description": "Heart rate in BPM"
},
"bloodPressureSystolic": {
"type": "integer",
"minimum": 70,
"maximum": 190,
"description": "Systolic blood pressure"
},
"bloodPressureDiastolic": {
"type": "integer",
"minimum": 40,
"maximum": 130,
"description": "Diastolic blood pressure"
},
"bodyTemperature": {
"type": "number",
"minimum": 35.0,
"maximum": 42.0,
"description": "Body temperature in Celsius"
},
"oxygenSaturation": {
"type": "number",
"minimum": 0.70,
"maximum": 1.00,
"description": "Blood oxygen saturation (0-1)"
}
},
"required": ["heartRate", "bodyTemperature", "oxygenSaturation"]
}
},
"required": ["healthMetrics"]
}{
"healthMetrics": {
"heartRate": 72,
"bloodPressureSystolic": 120,
"bloodPressureDiastolic": 80,
"bodyTemperature": 36.6,
"oxygenSaturation": 0.98
}
}Combining Constraints
Create precise generation behavior by combining multiple constraints:
{
"type": "object",
"properties": {
"userAccount": {
"type": "object",
"properties": {
"username": {
"type": "string",
"pattern": "^[a-z0-9_]{3,16}$",
"description": "Lowercase alphanumeric with underscores, 3-16 chars"
},
"accountType": {
"type": "string",
"enum": ["free", "premium", "enterprise"],
"description": "Account tier"
},
"storageLimit": {
"type": "integer",
"enum": [5, 50, 500, -1],
"description": "Storage limit in GB (-1 for unlimited)"
},
"apiKey": {
"type": "string",
"pattern": "^sk-[a-zA-Z0-9]{48}$",
"description": "API key format"
},
"createdAt": {
"type": "string",
"format": "date-time",
"description": "Account creation timestamp"
},
"settings": {
"type": "object",
"properties": {
"theme": {
"type": "string",
"enum": ["light", "dark", "auto"],
"default": "auto"
},
"language": {
"type": "string",
"pattern": "^[a-z]{2}(-[A-Z]{2})?$",
"description": "Language code (e.g., 'en' or 'en-US')"
},
"emailNotifications": {
"type": "boolean",
"default": true
}
},
"required": ["theme", "language"]
}
},
"required": ["username", "accountType", "apiKey", "createdAt", "settings"]
}
},
"required": ["userAccount"]
}{
"userAccount": {
"username": "john_doe_2024",
"accountType": "premium",
"storageLimit": 50,
"apiKey": "sk-a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4",
"createdAt": "2024-03-15T10:30:00Z",
"settings": {
"theme": "dark",
"language": "en-US",
"emailNotifications": false
}
}
}Common Constraint Patterns
Email with Domain Restriction
{
"type": "string",
"pattern": "^[a-zA-Z0-9._%+-]+@(example\\.com|example\\.org)$",
"description": "Email from example.com or example.org domains only"
}Version Number
{
"type": "string",
"pattern": "^(0|[1-9]\\d*\\\.)(0|[1-9]\\d*\\\.)(0|[1-9]\\d*)$",
"description": "Semantic version (e.g., 1.2.3)"
}Percentage (0-100)
{
"type": "number",
"minimum": 0,
"maximum": 100,
"description": "Percentage value"
}Optional Field with Null
{
"anyOf": [
{"type": "string", "minLength": 1},
{"type": "null"}
],
"description": "Optional string field"
}Next Steps
- Explore Composition for advanced schema patterns.
- Review Practical Examples for real-world applications.
- Check Data Types for basic type constraints.