Skip to content
sinc-LLMBook a 30-minute call

Engineering reference

How to Request and Validate JSON From ChatGPT

A comparison of JSON response settings, schema-based structured outputs, and prompt-format instructions, with failure examples.

Methods, definitions, and examples for the topic discussed here. Check applicability against the task and operating conditions.

JSON output needs checks at several levels: parsing, required fields, schema conformance and factual content. This guide covers response settings and prompt structure, along with validation and handling for malformed or incomplete responses.

This is the simplest way OpenAI lets you ask for JSON:

response = client.chat.completions.create(
    model="gpt-4o",
    response_format={"type": "json_object"},
    messages=[
        {"role": "system", "content": "Return data as JSON."},
        {"role": "user", "content": "List 5 programming languages with their year of creation."}
    ]
)

This makes sure you get valid JSON syntax. But it does NOT control the shape. ChatGPT might return {"languages": [...]} or {"data": [...]} or {"result": {...}}. The JSON is valid, but the structure changes from call to call.

Method 2: Structured Outputs (JSON Schema)

This is OpenAI's way to lock down the exact shape of the output:

response = client.chat.completions.create(
    model="gpt-4o",
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "languages",
            "schema": {
                "type": "object",
                "properties": {
                    "languages": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "name": {"type": "string"},
                                "year": {"type": "integer"},
                                "paradigm": {"type": "string"}
                            },
                            "required": ["name", "year", "paradigm"]
                        }
                    }
                },
                "required": ["languages"]
            }
        }
    },
    messages=[...]
)

This gives you valid JSON AND the right shape. It is the most reliable method when using the API. But it only controls the structure. The CONTENT of each field still depends on your prompt. If your prompt is vague, you get valid JSON full of made-up data.

The API methods above only work with OpenAI's API. They do not work in the ChatGPT web interface. They do not work with Claude, Gemini, or open-source models. And they do not control the content inside the JSON, only the shape.

This is where sinc-LLM's 6-band structure helps. You set the FORMAT and CONSTRAINTS bands. Then you get reliable JSON from any model, through any interface:

x(t) = Σ x(nT) · sinc((t - nT) / T)

The sinc-LLM JSON Prompt Pattern

{
  "formula": "x(t) = \u03a3 x(nT) \u00b7 sinc((t - nT) / T)",
  "T": "specification-axis",
  "fragments": [
    {"n": 0, "t": "PERSONA", "x": "API data engineer who returns clean, parseable JSON"},
    {"n": 1, "t": "CONTEXT", "x": "Building a data pipeline that processes the API response with JSON.parse()"},
    {"n": 2, "t": "DATA", "x": "Input: 5 programming languages with creation year and primary paradigm"},
    {"n": 3, "t": "CONSTRAINTS", "x": "Output MUST be valid JSON that passes JSON.parse(). First character must be { or [. No markdown code fences. No commentary before or after. No trailing commas. All strings in double quotes. Use snake_case for field names. Include all fields even if null."},
    {"n": 4, "t": "FORMAT", "x": "JSON object: {languages: [{name: string, year_created: integer, primary_paradigm: string}]}"},
    {"n": 5, "t": "TASK", "x": "Return 5 programming languages as a JSON array with the exact schema specified in FORMAT."}
  ]
}

Why Method 3 Is the Most Reliable Long-Term

API parameters fix the syntax problem. But they do not fix the content problem. And they tie you to one provider.

The sinc-LLM approach fixes both problems:

  • Syntax check: specify “valid JSON, no commentary, no fences,” then parse the response. Treat parsing failures as errors to handle; the instruction does not guarantee valid JSON.
  • Schema control: The FORMAT band holds your exact schema. It controls the shape without needing API-level enforcement.
  • Content quality: The PERSONA, CONTEXT, DATA, and TASK bands make sure the JSON has accurate, relevant data, not made-up filler.
  • Model portability: The same prompt works across ChatGPT, Claude, Gemini, Llama, and Mistral. No vendor lock-in.

The Combined Approach (Maximum Reliability)

For production systems, use API-level enforcement together with the sinc-LLM prompt structure:

  1. Use response_format: json_schema at the API level. This locks down syntax and shape.
  2. Use the sinc-LLM 6-band structure in your prompt. This controls content quality.
  3. Add a JSON validation step in your pipeline. This is your last safety net.

Validate syntax, schema and factual content separately. Retain failure records and define a safe retry or review path before using the JSON in a production pipeline.

ProblemSolution
Model adds "Here is the JSON:" before outputCONSTRAINTS: "First character of response must be { or ["
Markdown code fences around JSONCONSTRAINTS: "No markdown formatting. Raw JSON only."
Trailing commas in arraysCONSTRAINTS: "Valid JSON. No trailing commas."
Inconsistent field namingFORMAT: Include exact field names in schema
Missing required fieldsCONSTRAINTS: "Include ALL schema fields. Use null for missing values."
{
  "formula": "x(t) = \u03a3 x(nT) \u00b7 sinc((t - nT) / T)",
  "T": "specification-axis",
  "fragments": [
    {"n": 0, "t": "PERSONA", "x": "Expert data scientist with 10 years ML experience"},
    {"n": 1, "t": "CONTEXT", "x": "Building a recommendation engine for an e-commerce platform"},
    {"n": 2, "t": "DATA", "x": "Dataset: 2M user interactions, 50K products, sparse matrix"},
    {"n": 3, "t": "CONSTRAINTS", "x": "Must use collaborative filtering. Latency under 100ms. No PII in logs. Python 3.11+. Must handle cold-start users with content-based fallback"},
    {"n": 4, "t": "FORMAT", "x": "Python module with type hints, docstrings, and pytest tests"},
    {"n": 5, "t": "TASK", "x": "Implement the recommendation engine with train/predict/evaluate methods"}
  ]
}

Use sinc-LLM to state the JSON structure you need, then validate the returned syntax, schema and content before using it.

// Production AI Engineering

Build AI systems that hold up in production.

sinc-LLM designs, audits, and stabilises production AI infrastructure: from vendor evaluation and cost accountability to incident controls and MCP architecture.

See what we do →
Scope and context

sinc-LLM About Blog Services

© 2026 sinc-LLM. All rights reserved. DOI: 10.5281/zenodo.19152668