P

JSON Mode: Definition and Examples

JSON Mode is a parameter available in some language model APIs that forces the model to produce a response exclusively in valid JSON format, ensuring a structured and directly code-usable output.

Full definition

JSON Mode is a feature offered by language model providers (such as OpenAI, Anthropic, or Google) that constrains the model's output to syntactically correct JSON format. When activated, the model ensures that its response constitutes a valid JSON object, eliminating common parsing issues related to natural language responses.

In practice, JSON Mode solves a fundamental problem of using LLMs in production: output format reliability. Without this constraint, a model may add explanatory text before or after the JSON, use incorrect quotation marks, or produce malformed JSON. JSON Mode guarantees that each response can be directly parsed by a standard JSON interpreter without additional processing.

It is important to distinguish JSON Mode from Structured Output (or JSON Schema). JSON Mode only guarantees that the output is valid JSON, but does not control the exact structure of the data (which keys, which value types). Structured Outputs go further by allowing you to specify a precise JSON schema that the model must follow. JSON Mode is therefore a format constraint, while Structured Outputs are a structure constraint.

For best results with JSON Mode, it is recommended to explicitly describe the expected JSON structure in the prompt, by providing an example or a schema. The model will then respect both the technical constraint (valid JSON) and the semantic constraint (desired structure) defined in your instructions.

Etymology

The term combines 'JSON' (JavaScript Object Notation), a lightweight data format created by Douglas Crockford in the early 2000s, and 'Mode', referring to an API configuration parameter. The concept was popularized by OpenAI when they introduced this option in the GPT-4 Turbo API in November 2023.

Concrete examples

Structured data extraction from free text

Extract the following information from this resume and return it in JSON with the keys: name, email, experience_years, skills (list). Resume: Marie Dupont, marie@email.com, 8 years of experience in Python, React and PostgreSQL development.

Product catalog generation for an e-commerce application

Generate 3 product sheets in JSON format with the fields: id, name, description, price, category, in_stock. Theme: ergonomic office accessories.

Sentiment analysis for an automated data pipeline

Analyze the sentiment of each customer review below. Return a JSON array where each element contains: original_text, sentiment (positive/negative/neutral), confidence_score (0 to 1), keywords (list).

Practical usage

In prompt engineering, enable JSON Mode via the response_format parameter of the API (for example `{"type": "json_object"}` from OpenAI) and always describe the desired JSON structure in your system or user prompt. This is particularly useful for automated pipelines where the LLM output feeds directly into a database, API, or another service. Combine it with clear instructions on expected data types to maximize reliability.

Related concepts

Structured OutputFunction CallingOutput ParsingPrompt Engineering

FAQ

What is the difference between JSON Mode and Structured Outputs?
JSON Mode guarantees that the output is syntactically valid JSON, but it does not control which keys or value types are present. Structured Outputs (available from some providers) allow you to specify an exact JSON schema that the model must follow, offering much finer control over the structure of the returned data.
Does JSON Mode work with all language models?
No, JSON Mode is a feature specific to certain APIs. OpenAI offers it on GPT-4 Turbo and GPT-4o, Google on Gemini, and other providers offer similar mechanisms. For models that do not natively support it, you can get JSON by requesting it in the prompt, but without a guarantee of syntactic validity.
Do I still need to describe the JSON format in the prompt if JSON Mode is enabled?
Yes, absolutely. JSON Mode only guarantees that the output will be valid JSON, but the content of your prompt determines the structure and data included. Without clear instructions on expected keys and their format, the model will produce valid JSON but potentially with an unexpected or incomplete structure.

See also

How to use this prompt

  1. Copy the prompt with the button above.
  2. Paste it into ChatGPT, Claude or your favorite AI assistant.
  3. Replace the bracketed variables with your details, then refine the result.

About Prompt Guide

Prompt Guide is a free library of 2500+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.

More definitions

Get new prompts every week

Join our newsletter.