This is a cache of https://developer.ibm.com/articles/json-prompting-llms/. It is a snapshot of the page as it appeared on 2025-11-23T05:20:23.978+0000.
JSON prompting for LLMs - IBM Developer

Article

JSON prompting for LLMs

The ultimate guide for structured AI prompts

By

Aditya Gidh

In 2025, AI tools and large language model (LLM) chat interfaces are flooding our workflows. Raw prompts in these AI tools can produce creative chaos, being brilliant one moment and frustrating the next. Because structured prompting uses clear, machine-readable instructions, LLMs transform into precise and reliable guides.

LLMs respond best to structure. When you communicate in text format, ambiguity can creep in, but JavaScript Object Notation (JSON) provides the LLM a clear, machine-readable format. JSON prompting is a technique that transforms how humans interact with LLMs by bridging the gap between vague requests and precise, structured responses. Whether you are extracting data from emails, generating API payloads, or analyzing user feedback, JSON prompting helps ensure that outputs are not only useful but also usable.

Basics of JSON prompt structure

JSON is the native language of the modern software world: APIs, databases, web services, and configuration files. It is essentially the lingua franca of modern software. LLMs, and especially the powerful instruction-tuned models (for example, IBM Granite or OpenAI models), were trained extensively on code and structured data. At its core, LLMs use JSON to wrap instructions, context, and expected outputs in a hierarchical, key-value structure. This structured data reduces ambiguity, making responses clearer, more consistent, and easier to integrate into downstream systems like databases or APIs.

Structured prompts reduce guesswork and minimize confusing results, making AI easier for anyone to use. JSON prompting provides the LLM a precise, structured data contract (a schema) for the model's response, prompting the LLM to behave like a data processor rather than a creative writer. It works because JSON follows strict rules and structure. The model essentially shifts its output mode from free-form text to structured serialization.

A JSON prompt sets expectations by using object structure, fields for each desired output, constraints for content, and clear instructions. A basic JSON prompt wraps instructions in an object with keys such as task, context, and output_schema. JSON is modular, so you can think of it like Lego blocks for prompts.

JSON templates for extraction, generation, and analysis

The following JSON templates cover common use cases: extraction, generation, and analysis. You can reuse these templates across LLM pipelines or JSON schema enforcement layers as ready-to-use building blocks.

Extraction template

Use this template to convert unstructured input into structured, typed data, such as information from text, logs, web pages, or documents.

Replace the fields in the following template with the structure that you need and include it as a system or developer message in your LLM API call.

{

  "instruction": "Extract structured data from the following text",
  "context": "This is a user support message log",
  "input": "<raw text>",
  "output_format": {
    "user_id": "string",
    "timestamp": "string (ISO 8601)",
    "issue_type": "string",
    "entities": ["string"]
  },

  "constraints": {
    "max_entities": 10
  }
}

Generation template

Use this template to structure creative or factual content, such as titles, outlines, or responses, in a defined format.

Replace the input field with your topic, context, or seed idea and include it as a system or developer message in your LLM API call.

{

  "task": "generate",
  "description": "Generate content based on the given topic or context.",
  "input": "<your topic, seed, or context here>",
  "schema": {
    "fields": [
      {
        "name": "string",
        "type": "string | array | object",
        "description": "Field name and type of generated content."
      }
    ]
  },

  "constraints": {
    "output_format": "JSON",
    "max_length": 1000
  }
}

Analysis template

Use this template to assess input data, such as text, code, metrics, or text-based image descriptions, and generate insights or conclusions. With this template, the model evaluates, interprets, or derives insights from structured or unstructured input.

Replace the input field with the data or text you want analyzed and include it as a system or developer message in your LLM API call.

{

  "task": "analyze",
  "description": "Analyze the given input and provide structured insights or evaluations.",
  "input": "<data or text to analyze>",
  "schema": {
    "fields": [
      {
        "name": "string",
        "type": "string | number | boolean | array | object",
        "description": "Analytical dimension or metric to output."
      }
    ]
  },

  "constraints": {
    "output_format": "JSON",
    "include_reasoning": false
  }
}

Nested workflow template

Use this template when you want the model to follow a multi-step, structured reasoning process and return only the requested fields for each step and a final summary. This template is ideal for more complex tasks such as incident triage, root cause analysis, or code review checklists.

Provide the context, steps, final summary, and strict instructions to the model and include it as a system or developer message in your LLM API call.

{
  "task": "Perform a multi-step reasoning workflow.",
  "context": "{{input_data}}",
  "steps": [
    {
      "step_name": "Analyze input",
      "output": {
        "observations": [
          "string"
        ],
        "issues_detected": [
          "string"
        ]
      }
    },
    {
      "step_name": "Propose solutions",
      "output": {
        "recommendations": [
          {
            "solution": "string",
            "priority": "high | medium | low",
            "estimated_effort": "string"
          }
        ]
      }
    }
  ],
  "final_summary": {
    "overall_recommendation": "string",
    "confidence": "float (0-1)"
  },
  "instructions": "Produce a valid nested JSON structure only."
}

Example: Schema-driven reusable integration pattern

This small facade pattern is a reusable wrapper that:

  • Builds a schema‑first prompt
  • Calls any LLM provider
  • Normalizes the output to valid JSON (with retries or repair if needed).

In this pattern, the function sends a JSON prompt to the specified model and attempts to parse the response as JSON. If the model returns invalid JSON, it returns "None" so that you can retry or log the error.

The following pseudocode shows this facade pattern for running JSON prompts:

import json

def run_json_prompt(llm, document_text, schema_definition):
    payload = {
        "task": "extract",
        "input": document_text,
        "schema": schema_definition,
        "constraints": {"output_format": "JSON"}
    }
    response = llm.call(prompt=json.dumps(payload))
    try:
        data = json.loads(response)
    except json.JSONDecodeError:
        # Handle malformed output by retrying or alerting
        data = None
    return data

This pattern adapts well to platforms like IBM Granite, OpenAI, Anthropic, and Ollama because:

  • These model providers support JSON-style prompting and schema enforcement, either through native structured output modes or by following well-crafted prompt instructions.
  • You can guide the model to return valid JSON by explicitly describing the expected format in the prompt or by enabling structured output features if the provider supports them.
  • Parsing the output with standard libraries (like json.loads) can ensure consistency, reduces ambiguity, and makes the output easier to validate and use programmatically.

This JSON prompting method is widely applicable, though small adjustments might be needed for each provider’s API syntax and authentication.

Automating JSON prompting

The JSON prompting ecosystem has developed rapidly with frameworks, libraries, and APIs that make structured prompting safe, flexible, and suitable for production. Although JSON prompting provides clear structure, challenges usually arise after the model responds, when it is necessary to ensure that the output is valid, typed, and ready for use by another system.

This is where automation frameworks and validation libraries play a role. The open-source community offers several libraries, particularly in Python, that help automate the more complex aspects of JSON prompting.

  • Data contracts (validation) libraries

    • Libraries such as Pydantic (Python) or Zod (TypeScript/JavaScript) enable developers to define the data structure once using native language classes.
    • These libraries can automatically generate JSON schemas for LLM prompts and handle validation and parsing of the final output.
    • Instead of manually checking whether fields such as summary or keywords exist in the response, you can define a schema once, and the libraries manage the validation.
  • Prompt frameworks

    • Specialized libraries like Instructor can wrap LLM SDKs to simplify Pydantic-to-LLM-to-Pydantic workflows, making the entire structured output process feel native.
    • Frameworks like Instructor, Guardrails AI, and Outlines extend functionality by automating JSON prompting.
    • LangChain, LlamaIndex, BeeAI Framework provide structured prompting tools that define schemas, generate JSON prompts, validate output, and retry on failure.

Error handling in production patterns

In real-world automation, error handling is essential. LLMs can sometimes return malformed JSON or unexpected extra text. To ensure that structured prompting remains reliable in production systems, you can use the following common strategies:

  • Provide explicit constraints in the prompt: Clearly instruct the model. For example, "Return only valid JSON, with no explanations or commentary". This reduces the chance of stray text causing errors.

  • Enforce schema validation: Use libraries such as Pydantic, Zod, or Guardrails to automatically check and repair model outputs. Validation ensures that every response matches the expected structure.

  • Use retries and self-healing: When validation fails, reprompt the model to correct its last output. This self-correction loop often restores valid JSON without human intervention.

  • Add fallback logic: If all else fails, log the error, trigger an alert, or queue it for manual review. This keeps workflows stable and prevents downstream issues.

JSON prompting use cases

JSON prompting can support a wide range of automation tasks. The following examples show how adding structure turns LLMs into reliable workflow engines.

  • Document summarization pipelines

    • Automate reading PDFs, emails, or knowledge-base articles.
    • Generate structured summaries such as {"summary": "...", "topics": [...], "sentiment": "positive"}.
    • Ideal for enterprise knowledge extraction and reporting dashboards.
  • Multi-agent coordination

    • Agents communicate through JSON contracts in which one extracts, another analyzes, and another decides.
    • JSON prompting serves as a common language for orchestrating agent workflows.
  • Log and error triage

    • Send application logs or CI/CD build errors to an LLM.
    • Receive structured outputs such as {"error_type": "timeout", "suggested_fix": "increase API timeout"}.
    • Useful for DevOps and incident analysis tools.
  • Security or compliance audits

    • LLMs review policies or code snippets and produce structured risk findings.
    • Enables downstream automation for alerts, reports, or audits.
  • Semantic data enrichment

    • Convert unstructured text into structured fields such as tags, categories, geolocations, or entities.
    • Supports content moderation, search indexing, and data labeling processes.
  • Invoice and receipt parsing

    • Extract vendor names, amounts, dates, and line items from uploaded receipts.
    • JSON provides exact key–value mapping for integration with accounting systems such as SAP or QuickBooks.

Summary

As AI systems evolve, developers must embrace structured prompting. JSON is popular because it integrates easily with APIs, but structure matters more than format. YAML works well for configuration tasks, Markdown for readable reports, and XML for legacy systems. The key is to guide LLMs with clear, code-like instructions instead of conversational text.