Skip to main content

Claude/ChatGPT Prompt to Guarantee Schema-Valid JSON Output

Get schema-valid JSON from an LLM every time: a prompt with the schema, examples, strict output rules, and an automatic repair step when parsing fails.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt makes the model design a prompt that returns valid, schema-conforming JSON on real inputs, with a recovery path for when it doesn't. A single stray sentence around the JSON breaks the downstream parser, so this embeds the schema directly, shows matching examples, and forbids prose or code fences in the output. Embedding the schema and showing a sparse example does more than begging the model to be valid.

The [task] variable is the extraction or generation job, and [schema] is the target JSON schema embedded into the prompt with field types and required fields. [edge_inputs] names the awkward cases — missing fields, multiple items, nulls — that get explicit handling rules so they don't silently produce malformed output. [failure_action] defines what the repair step does on a parse error, for example returning the closest valid JSON with unknown fields set to null. A second, cheap LLM call fixes structure without re-answering the task, so the repair never changes your data.

When to use it

  • An LLM feeds JSON into a real parser and an occasional stray sentence breaks it.
  • You need the output to match a strict [schema] every time, including sparse cases.
  • Handling edge inputs like missing line items or a missing total without crashing.
  • Adding an automatic repair step so a parse failure self-corrects instead of erroring out.
  • Building a test list of inputs that should each parse cleanly to guard against regressions.
  • Defining exactly how malformed output should degrade rather than failing hard.

Example output

You get the full prompt template, then the repair-call prompt, then the test inputs. The main prompt embeds [schema] with types and required flags, includes two example outputs (one sparse), and explicitly instructs JSON-only with no prose or fences. It adds handling rules for the named [edge_inputs] and describes a validation-plus-repair step where a second LLM call applies [failure_action] on parse error. The test list is inputs that should each parse cleanly. It's a complete template plus its repair path and tests, ready to wire into a pipeline.

Pro tips

  • Embed [schema] and show a sparse example — that does far more than begging the model to be valid.
  • Keep the repair call cheap and narrow: it should fix structure only, never re-answer [task], or it'll change your data.
  • Spell out [edge_inputs] concretely (missing total, no line items, nulls) so the model has explicit rules instead of guessing.
  • Choose a [failure_action] that degrades safely, like nulling unknown fields, so downstream code gets predictable shapes.
  • Forbid code fences explicitly in the output rules, since a wrapping fence is a common reason strict parsers reject otherwise-valid JSON.
  • Run the test list after any prompt change; clean-parsing inputs are your regression guard against reintroduced prose or fences.

Frequently Asked Questions

How does it stop the model from adding prose around the JSON?
The prompt includes an explicit instruction to output only JSON with no prose or code fences, and it embeds the schema plus example outputs to anchor the format. Showing a sparse matching example does more to enforce validity than simply asking the model to behave.
What happens when the output still fails to parse?
A validation-plus-repair step makes a second LLM call that applies your `[failure_action]`, such as returning the closest valid JSON with unknown fields set to null. The repair call is meant to fix structure only, never to re-answer the original task and change your data.
Can it handle missing or sparse fields?
Yes. You list awkward cases in `[edge_inputs]`, such as missing line items or a missing total, and the prompt generates explicit handling rules for them. One of the two example outputs is deliberately sparse so the model sees how a minimal valid response should look.
Does embedding the schema guarantee valid JSON every time?
It greatly improves reliability but is not an absolute guarantee, which is exactly why the repair step exists. Models can still occasionally drift, so the test list and the second-call recovery path together provide the safety net for real production inputs.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in AI Prompt Engineering Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support