Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Generative AI Notes
Topic #703

JSON Schema for LLM Output

JSON Schema is a formal specification format for describing exactly what shape a JSON object should have — field names, types, which fields are required, and constraints on values. Using it with an LLM makes your format expectations precise rather than described in prose.

A Basic Schema Example

{
  "type": "object",
  "properties": {
    "order_id": {"type": "string"},
    "status": {"type": "string", "enum": ["pending", "shipped", "delivered", "cancelled"]},
    "total": {"type": "number"},
    "items": {
      "type": "array",
      "items": {"type": "string"}
    }
  },
  "required": ["order_id", "status", "total"]
}

This precisely specifies: field names, their types, that status must be one of four specific values (not any string), and that three fields are mandatory.

Using a Schema to Guide Prompting

prompt = f"""
Extract order information matching this exact schema:
{json.dumps(schema, indent=2)}

Order text: {order_text}

Return only valid JSON matching the schema above.
"""

Providing the schema itself (not just a prose description) gives the model an unambiguous structural target — and the same schema can then be reused for validating the actual output.

Schema as Shared Contract

A well-defined schema serves double duty: it guides generation (via the prompt) and defines validation (checking the output afterward) — using the same schema definition for both keeps them from drifting out of sync, which is a real, avoidable source of bugs if prompt instructions and validation logic are maintained separately by hand.

Practical Use Case

Any application with a clearly defined data contract for LLM output — an internal API response format, a database record structure, a UI component's expected props — benefits from expressing that contract as an actual JSON Schema rather than an informal prose description scattered across prompt and code.

Common Mistakes

  • Describing the desired structure only in prose ("return order_id as a string, status as one of...") instead of an actual schema — more ambiguous and harder to reuse for validation
  • Maintaining the prompt's format description and the validation logic as two separate, hand-written things that can silently drift apart over time
  • Over-specifying an overly rigid schema for genuinely variable data, causing valid responses to fail validation unnecessarily

Interview Relevance

"Why use JSON Schema instead of just describing the format in the prompt?" — precision, and the ability to reuse the exact same definition for both guiding generation and validating output, are the key points.

Practice Question

Write a JSON Schema for a product review analysis result with fields: sentiment (positive/negative/neutral), rating_mentioned (boolean), and key_phrases (array of strings).

Related Notes

Want to go beyond the notes?

Join CodingNow's Generative AI course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →