Skip to main content
Back to Library
Prompt Engineering Guide

Mastering JSON schema generation
on Claude 3.5 Sonnet

Stop guessing. See how professional prompt engineering transforms Claude 3.5 Sonnet's output for specific technical tasks.

The "Vibe" Prompt

"Generate a JSON schema for a 'Product' object. It should include fields like name, description, price, and category. Make sure price is a number and category is an enum of predefined values (Electronics, Books, Home)."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert in JSON schema generation and have a deep understanding of OpenAPI Specification and industry best practices. Your task is to generate a comprehensive and accurate JSON schema. Before generating the schema, reason through the requirements and constraints step-by-step. Here's the problem: Generate a JSON schema for a 'Product' object. The schema should define the following attributes: - `name`: A string representing the product's name. This field is required. - `description`: A string providing a detailed description of the product. This field is optional. - `price`: A number representing the product's price. It must be a positive number (greater than 0) and is required. - `category`: A string representing the product's category. It must be one of the following predefined values: 'Electronics', 'Books', 'Home'. This field is required. Now, let's think step by step to build the JSON schema: 1. **Identify the root object type**: The request is for a 'Product' object, which implies a `type: "object"`. 2. **Define properties for each attribute**: For each specified attribute (`name`, `description`, `price`, `category`), we need to define a corresponding entry in the `properties` block. 3. **Specify data types**: Assign the correct `type` (e.g., `string`, `number`) for each property as per the requirements. 4. **Handle required fields**: List all mandatory fields in the `required` array. 5. **Apply constraints**: For `price`, enforce `minimum: 0` and `exclusiveMinimum: true`. For `category`, use an `enum` with the provided values. 6. **Add descriptions (optional but good practice)**: Include a `description` for each property to make the schema more self-documenting. Given these steps, generate the JSON schema.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt works better because it explicitly assigns a role ('expert in JSON schema generation'), defines the task clearly, and most importantly, uses a chain-of-thought (`Now, let's think step by step...`) approach. This guides the model through the logical construction of the schema, ensuring all constraints (required fields, data types, specific values like enum, and numerical constraints) are considered and correctly implemented. It also encourages best practices like `description` for properties. This structured approach reduces ambiguity and the chances of misinterpretation compared to the 'vibe_prompt'.

-238.1%
Token Efficiency Gain
optimized_prompt contains 'type: "object"'
optimized_prompt correctly identifies and applies 'required' fields.
optimized_prompt correctly uses 'type: "number"' for price and applies 'minimum' and 'exclusiveMinimum'.

Ready to stop burning tokens?

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

Optimize My Prompts