← Master Index
Vol. 13 Module 13.1 Lecture

JSON Prompting

Prompting Techniques

How This Lesson Fits the Module & Volume

After the umbrella of structured output prompting, this lecture specializes in JSON prompting—the de facto interchange format for LLM apps, agents, and APIs. JSON pairs cleanly with Python dicts, TypeScript types, and OpenAPI-style schemas.

Compare with sibling carriers XML prompting and Markdown formatting when you need tagged sections or human-readable documents instead of pure data objects.

Learning Objectives

By the end of this lesson, students should be able to:

  • Write prompts that request valid JSON with explicit keys and types.
  • Use provider JSON object / JSON schema modes when available.
  • Handle common failure modes: trailing commas, comments, fences, single quotes.
  • Design flat vs. nested schemas for extraction and classification.
  • Combine JSON output with system prompts for durable format rules.
  • Measure format compliance as an evaluation metric.
Definition

JSON prompting is instructing a language model to produce responses as JSON (JavaScript Object Notation)—objects and arrays of typed values—so application code can parse and validate them with standard libraries.

Prompt Patterns That Raise Compliance

Show the Shape

  • Paste a skeleton object
  • Mark required keys
  • Give one filled example

Hard Rules

  • “ONLY JSON”
  • No fences / commentary
  • Double quotes only

API Assist

  • json_object mode
  • JSON Schema response
  • Tool arguments as JSON
FailureSymptomMitigation
Markdown fences```json wrapperBan fences; strip if present
Trailing commasjson.loads failsRetry with repair prompt
Extra keysSchema driftReject unknown properties
Wrong types"true" vs trueValidate with Pydantic / Zod

Practical Prompt + Validation

SYSTEM = """You are a data extractor. Return ONLY a JSON object with keys: sentiment: "positive" | "neutral" | "negative" confidence: number between 0 and 1 themes: array of up to 5 short strings Do not wrap in markdown. Do not add keys.""" USER = f"Review:\n{review_text}" # After the model replies: import json from pydantic import BaseModel, Field, ValidationError class ReviewOut(BaseModel): sentiment: str confidence: float = Field(ge=0, le=1) themes: list[str] raw = model_reply.strip() if raw.startswith("```"): raw = raw.strip("`") if raw.lower().startswith("json"): raw = raw[4:].lstrip() try: obj = ReviewOut.model_validate_json(raw) except (json.JSONDecodeError, ValidationError) as e: # retry with: "Fix this into valid JSON matching the schema: ..." raise
Ask

Schema + ONLY JSON.

Parse

Strip fences; loads.

Validate

Types & enums.

Retry

Repair prompt if needed.

Strengths

  • Universal language bindings
  • First-class in modern LLM APIs
  • Easy logging and evals

Tradeoffs

  • Verbose for long documents
  • Escape hassles in nested strings
  • Humans prefer Markdown for reading
Common Misconception

response_format=json_object means my schema is enforced.” Many APIs only guarantee some JSON object, not your keys or types. You still need a schema (or JSON Schema mode / tools) plus validation. See also guardrails for post-parse policy checks.

Knowledge Check

  1. Short Answer: What does JSON prompting request? Answer: Model output as parseable JSON objects/arrays.
  2. True/False: Trailing commas are valid in strict JSON. Answer: False.
  3. Multiple Choice: A common parse breaker is: (a) markdown code fences, (b) UTF-8 text, (c) using double quotes. Answer: (a).
  4. Short Answer: Name one library for schema validation in Python. Answer: Pydantic (or jsonschema).
  5. True/False: json_object mode always enforces your custom keys. Answer: False—often only “is JSON.”
  6. Multiple Choice: Prefer JSON when the consumer is: (a) a parser/API, (b) only a human reader, (c) a GPU shader. Answer: (a).
  7. Short Answer: Why ban extra keys? Answer: Prevents schema drift and silent field invention.
  8. Short Answer: What should follow a failed json.loads? Answer: A repair/retry prompt or fallback path.
  9. Multiple Choice: Enums in JSON prompts help: (a) limit allowed categories, (b) train word2vec, (c) resize images. Answer: (a).
  10. True/False: Sibling XML prompting is sometimes better for tagged multi-section content. Answer: True.

Key Takeaways

  • JSON is the default machine contract for LLM features.
  • Show the skeleton, ban fences, validate types.
  • API JSON mode helps but does not replace schema checks.
  • Next: XML Prompting for tagged hierarchical sections.
Trainer’s Guide

Hands-on: Run the same extraction prompt with and without JSON mode; chart valid-parse rate.

Discussion: Should product teams version JSON schemas alongside prompts in git?

Recap: JSON prompting turns completions into typed data—if you validate. Continue with XML Prompting.