JSON Schema Guide
Radium supports structured output with JSON schema enforcement, allowing you to get reliably formatted responses from AI models. This guide covers how to use JSON schemas with Radium's CLI and explains provider-specific differences.
Quick Startβ
Basic Usageβ
# JSON mode (no schema validation)
rad step agent-id "Generate a user profile" --response-format json
# JSON schema mode (with validation)
rad step agent-id "Extract user data" --response-format json-schema --response-schema user-schema.json
# Inline schema
rad step agent-id "Extract data" --response-format json-schema --response-schema '{"type":"object","properties":{"name":{"type":"string"}}}'
Response Format Optionsβ
The --response-format argument accepts three values:
text: Plain text output (default)json: JSON-formatted output without schema validationjson-schema: JSON output conforming to a provided schema (requires--response-schema)
Schema Input Methodsβ
File-Based Schemaβ
Create a JSON schema file and reference it:
rad step agent-id "prompt" --response-format json-schema --response-schema schema.json
The schema file should contain valid JSON Schema:
{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "number"}
},
"required": ["name"]
}
Inline Schemaβ
Pass the schema directly as a string:
rad step agent-id "prompt" --response-format json-schema --response-schema '{"type":"object","properties":{"name":{"type":"string"}}}'
Example Schemasβ
User Profile Schemaβ
File: examples/schema-examples/user-profile.json
{
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string"},
"age": {"type": "number", "minimum": 0, "maximum": 150},
"address": {
"type": "object",
"properties": {
"street": {"type": "string"},
"city": {"type": "string"},
"zip": {"type": "string"}
},
"required": ["city"]
}
},
"required": ["name", "email"]
}
Usage:
rad step extract-user "Extract user information from: John Doe, john@example.com, 30 years old" \
--response-format json-schema \
--response-schema examples/schema-examples/user-profile.json
API Response Schemaβ
File: examples/schema-examples/api-response.json
{
"type": "object",
"properties": {
"status": {"type": "string", "enum": ["success", "error"]},
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {"type": "string"},
"title": {"type": "string"},
"price": {"type": "number"}
},
"required": ["id", "title"]
}
},
"message": {"type": "string"}
},
"required": ["status"]
}
Data Extraction Schemaβ
File: examples/schema-examples/data-extraction.json
{
"type": "object",
"properties": {
"entities": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {"type": "string"},
"value": {"type": "string"},
"confidence": {"type": "number", "minimum": 0, "maximum": 1}
},
"required": ["type", "value"]
}
},
"summary": {"type": "string"}
},
"required": ["entities"]
}
Enum Validation Schemaβ
File: examples/schema-examples/enum-validation.json
{
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["pending", "active", "completed", "cancelled"]
},
"priority": {
"type": "string",
"enum": ["low", "medium", "high", "urgent"]
}
},
"required": ["status", "priority"]
}
Provider Differencesβ
Geminiβ
- Full Schema Support: Gemini supports complete JSON Schema validation
- Strict Enforcement: Schema violations are caught by the API
- Format: Uses
response_mime_typeandresponse_schemafields
Example:
rad step agent-id "prompt" --response-format json-schema --response-schema schema.json --engine gemini
OpenAIβ
- Structured Outputs: OpenAI supports JSON schema with strict mode
- Format: Uses
response_formatwithjson_schemaobject containingname,schema, andstrict: true - Default Name: Radium uses "response_schema" as the default schema name
Example:
rad step agent-id "prompt" --response-format json-schema --response-schema schema.json --engine openai
Key Differencesβ
| Feature | Gemini | OpenAI |
|---|---|---|
| Schema Support | β Full | β Full |
| Strict Mode | β Always | β Always (strict: true) |
| Schema Name | N/A | Required (default: "response_schema") |
| Error Handling | API-level | API-level |
Common Errors and Solutionsβ
Error: "Invalid response format"β
Problem: Invalid value for --response-format
Solution: Use one of: text, json, json-schema
# Wrong
rad step agent-id "prompt" --response-format xml
# Correct
rad step agent-id "prompt" --response-format json
Error: "--response-schema is required when using --response-format json-schema"β
Problem: Missing schema argument
Solution: Provide a schema file or inline JSON
# Wrong
rad step agent-id "prompt" --response-format json-schema
# Correct
rad step agent-id "prompt" --response-format json-schema --response-schema schema.json
Error: "Failed to read schema file"β
Problem: Schema file doesn't exist or is unreadable
Solution: Check file path and permissions
# Check file exists
ls -la schema.json
# Use absolute path if needed
rad step agent-id "prompt" --response-format json-schema --response-schema /full/path/to/schema.json
Error: "Invalid JSON schema"β
Problem: Schema is not valid JSON
Solution: Validate your JSON schema
# Validate JSON
cat schema.json | jq .
# Or use online validator
# https://jsonschema.dev/
Error: "--response-schema cannot be used with --response-format json"β
Problem: Schema argument provided with json format (not json-schema)
Solution: Use json-schema format instead
# Wrong
rad step agent-id "prompt" --response-format json --response-schema schema.json
# Correct
rad step agent-id "prompt" --response-format json-schema --response-schema schema.json
Best Practicesβ
When to Use Schemasβ
- Data Extraction: When you need structured data from unstructured text
- API Integration: When responses need to match specific formats
- Form Filling: When populating forms or databases
- Validation: When you need guaranteed structure
Schema Design Tipsβ
- Start Simple: Begin with basic object schemas, add complexity gradually
- Use Required Fields: Mark essential fields as
requiredfor validation - Leverage Enums: Use
enumfor constrained string values - Nested Structures: Organize related data in nested objects
- Array Constraints: Use
minItemsandmaxItemsfor array validation
Performance Considerationsβ
- Schema Size: Large schemas may increase API latency slightly
- Validation: Schema validation happens server-side (no client overhead)
- Caching: Consider caching parsed schemas for repeated use
Advanced Usageβ
Complex Nested Schemasβ
For deeply nested structures:
{
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"profile": {
"type": "object",
"properties": {
"personal": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "number"}
}
}
}
}
}
}
}
}
Pattern Validationβ
Use regex patterns for string validation:
{
"type": "object",
"properties": {
"email": {
"type": "string",
"pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
}
}
}
Array Schemasβ
Define arrays with item constraints:
{
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {"type": "string"},
"value": {"type": "number"}
}
},
"minItems": 1,
"maxItems": 100
}
}
}
Referenceβ
See Alsoβ
- CLI Documentation
- Model Parameters Guide
- Model Providers - Provider-agnostic setup