Semantic Query
AI-powered schema validation with automatic JSON generation for type-safe responses.
Quick Example
run using
cargo run --example readme_demo
This demo shows how you can build a quiz engine!
use ;
use JsonSchema;
use ;
async
async
Setup: Add ANTHROPIC_API_KEY=your_key_here
to .env
file.
Schema-Aware Prompt Generation
The doc comments and constraints in your structs are automatically converted to JSON schema and included in the AI prompt. Here's what the actual prompt looks like for the QuizQuestion struct:
Create 10 high school science quiz questions with A, B, C, D answers
You are tasked with generating a value satisfying a schema. First I will give you an example exchange then I will provide the schema of interest
Example Schema:
{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0},
"email": {"type": "string"},
"isActive": {"type": "boolean"},
"hobbies": {"type": "array", "items": {"type": "string"}}
},
"required": ["name", "age", "email", "isActive"]
}
Example response:
{"name": "Alice Smith", "age": 28, "email": "alice@example.com", "isActive": true, "hobbies": ["reading", "cooking"]}
Please provide a response matching this schema
```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Quiz",
"type": "object",
"required": [
"questions"
],
"properties": {
"questions": {
"type": "array",
"items": {
"$ref": "#/definitions/QuizQuestion"
}
}
},
"definitions": {
"QuizQuestion": {
"title": "QuizQuestion",
"type": "object",
"required": [
"question",
"description",
"a",
"b",
"c",
"d",
"correct_answer"
],
"properties": {
"question": {
"description": "The main question text to be asked",
"type": "string"
},
"description": {
"description": "A brief description or context for the question",
"type": "string"
},
"a": {
"description": "Answer choice A",
"type": "string"
},
"b": {
"description": "Answer choice B",
"type": "string"
},
"c": {
"description": "Answer choice C",
"type": "string"
},
"d": {
"description": "Answer choice D",
"type": "string"
},
"correct_answer": {
"description": "The correct answer (must be exactly one of: A, B, C, or D)",
"type": "string",
"pattern": "^[ABCD]$"
}
}
}
}
}
This schema ensures the AI understands exactly what each field represents and enforces the constraint that `correct_answer` must be exactly A, B, C, or D.