Prompt Engineering Guide
Mastering JSON schema generation
on Gemini 2.0 Flash
Stop guessing. See how professional prompt engineering transforms Gemini 2.0 Flash's output for specific technical tasks.
The "Vibe" Prompt
"Hey Gemini, can you make some JSON schemas for me? I need them to be good, like, really good."
Low specificity, inconsistent output
Optimized Version
Your task is to act as an expert JSON schema generator. I will provide you with a description of the data, and you will generate a comprehensive and valid JSON Schema (Draft 2020-12) that accurately represents the data.
**Thought Process:**
1. **Identify Top-Level Type:** Determine if the data is primarily an object or an array. Set the `$schema` and `title` accordingly.
2. **Deconstruct Data:** Break down the data description into its core components (e.g., fields, nested structures, arrays of objects).
3. **Define Properties (for objects):** For each field, determine its `type` (string, number, integer, boolean, array, object, null), `description`, and whether it's `required`. Consider `minimum`, `maximum`, `minLength`, `maxLength`, `pattern`, `format`, `enum` where appropriate.
4. **Handle Arrays:** For arrays, specify `type: "array"` and define `items`. If `items` are homogeneous, define their schema directly. If heterogeneous, use `prefixItems` or `contains` with `minContains`/`maxContains`.
5. **Manage Nested Objects:** Recursively apply steps 2-4 for any nested object structures.
6. **Add Metadata:** Include `$id` (if applicable), `description` for the overall schema, and example values.
7. **Validate Semantics:** Ensure all keywords are used correctly and that the schema logically represents the described data.
8. **Review and Refine:** Check for clarity, completeness, and correctness. Ensure all required fields are marked as such.
**Example Input Description:**
A list of user profiles, where each profile has an ID (number, required), a username (string, required, unique like an email), an optional age (integer, 18-99), and a list of hobbies (array of strings, can be empty).
**Expected Output (JSON Schema):**
```json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/user-profiles.json",
"title": "User Profiles",
"description": "A list of user profiles",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"description": "Unique identifier for the user",
"minimum": 1
},
"username": {
"type": "string",
"description": "Unique username for the user, similar to an email address",
"format": "email"
},
"age": {
"type": "integer",
"description": "The user's age",
"minimum": 18,
"maximum": 99
},
"hobbies": {
"type": "array",
"description": "A list of hobbies",
"items": {
"type": "string"
},
"default": []
}
},
"required": [
"id",
"username"
]
}
}
```
Now, generate a JSON schema based on the following description: [YOUR DATA DESCRIPTION HERE]
Structured, task-focused, reduced hallucinations
Engineering Rationale
The optimized prompt leverages a structured chain-of-thought process, guiding the model through step-by-step schema generation. This breaks down the complex task into manageable sub-tasks, ensuring comprehensive coverage of JSON schema features. It specifies the target schema draft, provides clear examples of input and desired output, and explicitly states the expected an expert persona. This reduces ambiguity, improves consistency, and minimizes the need for follow-up clarifications, leading to more accurate and complete schemas.
0%
Token Efficiency Gain
The 'optimized_prompt' explicitly states the desired JSON Schema draft version.
The 'optimized_prompt' includes an example input description and its corresponding JSON Schema output.
The 'optimized_prompt' details specific properties to consider for different data types (e.g., minimum, maxLength, pattern, format).
Ready to stop burning tokens?
Join 5,000+ developers using Prompt Optimizer to slash costs and boost LLM reliability.
Optimize My Prompts