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.
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_objectmode- JSON Schema response
- Tool arguments as JSON
| Failure | Symptom | Mitigation |
|---|---|---|
| Markdown fences | ```json wrapper | Ban fences; strip if present |
| Trailing commas | json.loads fails | Retry with repair prompt |
| Extra keys | Schema drift | Reject unknown properties |
| Wrong types | "true" vs true | Validate with Pydantic / Zod |
Practical Prompt + Validation
Schema + ONLY JSON.
Strip fences; loads.
Types & enums.
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
“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
- Short Answer: What does JSON prompting request? Answer: Model output as parseable JSON objects/arrays.
- True/False: Trailing commas are valid in strict JSON. Answer: False.
- Multiple Choice: A common parse breaker is: (a) markdown code fences, (b) UTF-8 text, (c) using double quotes. Answer: (a).
- Short Answer: Name one library for schema validation in Python. Answer: Pydantic (or jsonschema).
- True/False:
json_objectmode always enforces your custom keys. Answer: False—often only “is JSON.” - Multiple Choice: Prefer JSON when the consumer is: (a) a parser/API, (b) only a human reader, (c) a GPU shader. Answer: (a).
- Short Answer: Why ban extra keys? Answer: Prevents schema drift and silent field invention.
- Short Answer: What should follow a failed
json.loads? Answer: A repair/retry prompt or fallback path. - Multiple Choice: Enums in JSON prompts help: (a) limit allowed categories, (b) train word2vec, (c) resize images. Answer: (a).
- 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.
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.