JSON schema prompt builder
Getting an LLM to return clean, parseable JSON is mostly a prompting problem.
Ask loosely and you get markdown fences, chatty preambles, and stray keys that
break your parser. This builder takes your schema — or a sample object — and
writes an instruction block that pins down every field, restates its type,
marks what is required, and forbids anything that would trip up JSON.parse.
How it works
The tool accepts either a JSON Schema or a plain example object. From a schema
it reads properties, type, and required; from an example it infers field
names and types directly. It then assembles an instruction block listing each
field with its expected type and whether it is required, plus a set of
guardrails sized to your chosen strictness: return only JSON, no markdown fence,
no commentary, and — in strict mode — no keys beyond those in the schema. You
paste the block into your prompt and the model has an unambiguous contract.
Why a loose “return JSON” instruction fails
When you only write “respond with JSON,” several failure modes appear regularly:
- Markdown fences. Most models are trained to wrap code in triple-backtick fences for readability. The model returns
```json\n{...}\n```and yourJSON.parsesees a backtick and throws. - Preamble text. “Here is the JSON you requested:” followed by the object. The text before the
{breaks the parse. - Extra keys. Models may add helpful-seeming fields the schema did not define. These can cause strict schema validators to reject the response or confuse downstream code that iterates keys.
- Wrong types. A field typed as a boolean might come back as the string
"true", or a required number might come back asnull.
A well-formed prompt instruction eliminates these by being explicit about the output contract.
What a generated instruction block looks like
For a schema with name (string, required) and age (integer, optional), the generated instruction in strict mode looks roughly like:
Return ONLY a valid JSON object with no other text before or after it.
Do NOT include markdown code fences or backticks.
Do NOT include any commentary or explanation.
Required fields:
- name (string): The person's full name.
Optional fields:
- age (integer): The person's age in whole years.
Include ONLY these fields. Do not add any extra keys.
This is appended to your actual task prompt — the model then has both the task and the output contract in one place.
Tips and notes
- Use strict mode for pipelines. When the output feeds code, the no-extra-keys, no-prose rules are what keep your parser happy.
- Combine with native JSON mode where the model supports it — the prompt and the feature reinforce each other. A strong prompt plus JSON mode is more reliable than either alone.
- Spell out enums and formats in the schema (e.g.
"status": "active|paused") so the reminders carry that detail through. - Always parse defensively. Even a strict prompt can occasionally slip; wrap the parse in a try/catch and retry rather than trusting it blind.
- For short pipelines, put the schema last. Models tend to follow instructions most reliably when the output contract is close to the end of the prompt, just before they start generating.