LLM-to-JSON extraction prompt with schema, validation and retries
Build a reliable pipeline to extract free text into JSON with an LLM: JSON Schema, a prompt with null rules, few-shot examples, validation code with retries and test cases. Designed for ChatGPT, Claude and Gemini. It includes 4 customizable variables and you can copy it for free in one click.
Advanced · Code · Pending editorial review
Prompt
Act as an LLM engineer specializing in structured data extraction for production systems.
Context:
- Input document type: {{document}}
- Fields I need to extract (name, type, required or not): {{fields}}
- Language and validation library: {{language}}
- A real input sample (anonymized):
```
{{sample}}
```
Deliverables:
1. A complete JSON Schema with types, formats (ISO 8601 dates, amounts as numbers with currency as a separate field), enums and required fields.
2. A system prompt for extraction with explicit rules: return only valid JSON, use null when a value does not appear (never infer it), do not normalize proper names, how to handle multiple candidate values and ambiguous text.
3. Two input/output examples (few-shot), one of them with missing fields.
4. Code in {{language}} that calls the model, enables structured output or JSON mode if the provider offers it, validates against the schema and retries at most twice, sending back the validation error.
5. An optional confidence or evidence field (a verbatim quote from the text) for each value, and when it is worth using.
6. Ten test cases: incomplete documents, different date formats, mixed languages, contradictory values and malicious instructions inside the text.
7. How to measure quality: per-field accuracy against a hand-labeled set.
Constraints:
- Do not invent API parameters: if an option depends on the provider, mark it [VERIFY in the official docs].
- The model must never fill a field without evidence in the text.
- If the fields are ambiguous or the sample is missing, ask me first.
Format: numbered sections with JSON and {{language}} code blocks.How to use it
Paste a real anonymized document and define each field precisely. Before running a batch, run the test cases and compare against 20-50 hand-labeled documents.
Usage example
Document: supplier invoices from PDF converted to text · Fields: number, date, vendor tax ID, subtotal, sales tax, total · Language: Python with Pydantic.
Sample output
4. Code with validation and retries
from enum import Enum
from pydantic import BaseModel, Field, ValidationError
class RenovationType(str, Enum): # final values: [FILL IN]
bathroom = "bathroom"
kitchen = "kitchen"
whole_home = "whole_home"
other = "other"
class QuoteRequest(BaseModel):
name: str
phone: str | None = None
city: str | None = None
renovation_type: RenovationType | None = None
max_budget_usd: float | None = Field(default=None, ge=0)
def extract(text: str, max_retries: int = 2) -> QuoteRequest:
messages = [{"role": "user", "content": text}]
for _ in range(max_retries + 1):
# JSON mode / structured output depends on the provider: [VERIFY in the official docs]
raw = call_model(SYSTEM_PROMPT, messages)
try:
return QuoteRequest.model_validate_json(raw)
except ValidationError as error:
messages += [
{"role": "assistant", "content": raw},
{"role": "user", "content": f"Invalid JSON: {error}. Return only the corrected JSON."},
]
raise ValueError("The model did not return valid JSON")
Expected output for your sample:
{"name": "Megan", "phone": "555-0134", "city": "Denver",
"renovation_type": "bathroom", "max_budget_usd": 9000}
"Around $9,000" is an approximation: with the optional evidence field we would store the verbatim quote so a person can decide whether to treat it as a maximum…
Key facts
- Category
- Programming & development · AI agents
- Output type
- Code
- Level
- Advanced
- Designed for
- ChatGPT, Claude and Gemini
- Verified in
- Pending editorial verification
- Variables
- Document type, Fields to extract, Language and validation and Input sample
- Language
- English
- Price
- Free, no sign-up
- Updated
- Nov 7, 2026
Frequently asked questions
Why ask for null instead of letting the model infer the value?
Because a made-up value that looks right is more dangerous than a gap. With null you can detect incomplete documents and route them to human review.
Is the provider’s JSON mode enough?
It guarantees well-formed JSON, not that the values are correct or meet your business rules. Keep schema validation and the test cases.
What is this prompt for?
Build a reliable pipeline to extract free text into JSON with an LLM: JSON Schema, a prompt with null rules, few-shot examples, validation code with retries and test cases.
Which AI tools does it work with?
It is designed for ChatGPT, Claude and Gemini. We have not verified it in these tools yet, so results may vary between them.
How do I customize the prompt?
Click “Customize” and fill in the variables (Document type, Fields to extract, Language and validation and Input sample). The text updates instantly and you can then copy it in one click. Values are stored only in your browser.
Do I need experience to use it?
It is advanced: it is meant for people with experience in the topic who can evaluate and adjust the result.
Is this prompt free?
Yes. Every HomeCasa Prompts prompt is free, with no sign-up or subscription. You can also use them in professional projects.
Can I trust the AI output?
Treat it as a high-quality draft: always check data, figures and important claims before publishing or making decisions.