Prompt

building structured json output with llm api

Artificial Intelligence / AI Platforms1 observationLast seen Oct 8, 2026

Latest observation

Oct 8, 2026OpenAI APIWeb search: off

If you want structured JSON output from an LLM API, the safest pattern is:

  1. Define the schema
  2. Tell the model to output only JSON
  3. Validate the response
  4. Retry / repair if invalid

1) Use a schema

Example JSON structure:

{
  "name": "Alice",
  "age": 30,
  "skills": ["python", "sql"],
  "is_active": true
}

2) Prompt the model clearly

Good prompt:

Return only valid JSON matching this schema:

  • name: string
  • age: integer
  • skills: array of strings
  • is_active: boolean

Do not include markdown, commentary, or extra text.

3) Example API pattern

Python

import json

response = client.responses.create(
    model="gpt-4.1",
    input="Return only JSON with keys: name, age, skills, is_active."
)

text = response.output_text
data = json.loads(text)
print(data)

4) Better: use structured output / JSON schema if supported

Many modern LLM APIs support JSON mode or schema-constrained output.

Example idea:

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"},
        "skills": {
            "type": "array",
            "items": {"type": "string"}
        },
        "is_active": {"type": "boolean"}
    },
    "required": ["name", "age", "skills", "is_active"],
    "additionalProperties": False
}

Then send that schema to the API and ask it to comply.

5) Validate the result

Use a validator like:

  • jsonschema in Python
  • Zod in TypeScript
  • Pydantic in Python

Example with Python jsonschema:

from jsonschema import validate

validate(instance=data, schema=schema)

6) Handle failures

LLMs sometimes return:

  • trailing text
  • malformed JSON
  • wrong types
  • missing fields

Common fix:

  • try parsing
  • if it fails, ask the model to repair the JSON
  • reject anything that doesn’t validate

If you want, I can give you:

  1. a Python example with OpenAI structured outputs,
  2. a TypeScript example, or
  3. a generic prompt template for any LLM API.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.