Skip to main content
Back to Library
Prompt Engineering Guide

Mastering JSON schema generation
on Llama 3.1 8B

Stop guessing. See how professional prompt engineering transforms Llama 3.1 8B's output for specific technical tasks.

The "Vibe" Prompt

"Generate a JSON schema for a 'Product' object with properties like name, price, description, and available_colors."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert JSON schema generator. Your task is to accurately and comprehensively define JSON schemas based on user requests. Adhere strictly to the JSON Schema Draft 2020-12 specification. For each property, infer the most appropriate type and constraints (e.g., 'type', 'format', 'pattern', 'minimum', 'maximum', 'enum', 'minLength', 'maxLength', 'required'). **Thought Process:** 1. **Identify Core Object:** Determine the main entity for which the schema is being generated. 2. **List Properties:** Extract all mentioned properties for the core object. 3. **Infer Types & Constraints:** For each property, deduce its JSON Schema type (string, number, boolean, array, object, null) and any relevant constraints. Consider possible formats (e.g., 'date-time', 'email', 'uri'), enumerations, string lengths, numeric ranges, and whether a field should be 'required'. 4. **Structure Properties:** Organize the properties within the 'properties' keyword. 5. **Define Required Fields:** Explicitly list all properties that must be present in the 'required' array. 6. **Add Description (Optional but Recommended):** Provide a brief 'description' for the schema and individual properties for clarity. 7. **Set Schema Version:** Always include '$schema' (e.g., 'https://json-schema.org/draft/2020-12/schema'). **Task:** Generate a JSON schema for a 'Product' object. It should include the following information: - `name`: A string representing the product's name, required. - `price`: A positive number, representing the product's selling price, required. - `description`: An optional string detailing the product. - `available_colors`: An array of strings, where each string is a color name. This field is optional and can be empty if no colors are available. - `SKU`: A unique string identifier for the product, following the pattern `[A-Z]{3}-[0-9]{4}`, required.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt works better because it provides a clear persona ('expert JSON schema generator'), specifies the exact JSON Schema draft to follow, and includes a detailed chain-of-thought process. This structured approach guides the model through the steps of schema generation, ensuring all aspects (like required fields, types, formats, patterns, and descriptions) are considered systematically. By breaking down the task, it reduces ambiguity and increases the likelihood of a comprehensive and correct schema. The addition of specific examples for constraints (like `pattern` for SKU) further clarifies expectations.

0%
Token Efficiency Gain
The generated schema from 'optimized_prompt' accurately reflects all specified properties and their constraints (type, required, pattern, min/max).
The generated schema from 'optimized_prompt' includes the '$schema' keyword.
The generated schema from 'optimized_prompt' includes descriptive comments for properties where appropriate.

Ready to stop burning tokens?

Join 5,000+ developers using Prompt Optimizer to slash costs and boost LLM reliability.

Optimize My Prompts