Structured output prompting: how to get reliable JSON, tables, and lists from any AI

Techniques and templates for prompting AI to return structured, machine-readable output consistently — without hallucinated fields, format drift, or plain-text contamination.

最后更新: · Markdown版本

Why structured output prompts fail

  • No schema: asking for 'JSON with the extracted data' without specifying exact field names, types, and nesting almost always produces inconsistent results across runs.
  • Prose leakage: the model adds an explanation before or after the JSON block. This breaks parsers. Fix: 'Return only the JSON object. No explanation, no markdown code fences, no other text.'
  • Hallucinated fields: the model invents field names it thinks you want. Fix: provide the exact schema — field names, their types, and what to put when a value is not found ('null', empty string, or a sentinel like 'N/A').
  • Nested structure mismatch: you ask for a list but get a dict, or vice versa. Fix: show an example output in the prompt — not just a description, but the actual structure with placeholder values.
  • Inconsistent date/number formats: the model formats dates differently across responses. Fix: specify the exact format ('ISO 8601: YYYY-MM-DD') and give an example value.

The four elements of a reliable structured output prompt

  • 1. Task description: what to extract or generate, and from what input. 'Extract all line items from the invoice text below.'
  • 2. Exact schema: the complete JSON structure with field names, types, and instructions for missing values. Paste the schema literally, not as a prose description.
  • 3. Example output: a complete, filled-in example with realistic placeholder values. Models learn format from examples faster than from descriptions.
  • 4. Output constraint: 'Return only the JSON. No commentary, no markdown formatting, no preamble.' Put this at the end of the prompt — it is the last instruction the model sees before generating.

模板

Structured data extraction to JSON

Extract structured data from the text below and return it as JSON.

Text to process:
"""
[Paste the input text here — an invoice, email, form, article, etc.]
"""

Return a JSON object matching this exact schema:
{
  "field_one": string,         // [describe what this field contains]
  "field_two": string | null,  // [describe; null if not found]
  "field_three": number | null, // [describe; null if not found]
  "items": [
    {
      "item_field_a": string,
      "item_field_b": number | null
    }
  ]
}

Rules:
- Use null (not "N/A", not empty string) for any field not found in the text
- Dates in ISO 8601 format: YYYY-MM-DD
- Numbers as numeric types, not strings
- Do not add fields not in the schema
- Return only the JSON object — no explanation, no markdown code fence, no other text

Example output (for reference only — use values from the actual text):
{
  "field_one": "Example value",
  "field_two": null,
  "field_three": 42,
  "items": [
    { "item_field_a": "Item A", "item_field_b": 19.99 }
  ]
}
将在分析器中打开,并预先填入该提示词。

Structured comparison table

Generate a structured comparison of the items listed below and return the result as a JSON array.

Items to compare:
1. [Item A]
2. [Item B]
3. [Item C — add more as needed]

Dimensions to compare across (evaluate each item on each dimension):
- [Dimension 1, e.g. "Ease of setup"]
- [Dimension 2, e.g. "Cost"]
- [Dimension 3, e.g. "Scalability"]
- [Add or remove dimensions as needed]

Return a JSON array in this exact format:
[
  {
    "item": string,
    "scores": {
      "dimension_1": { "rating": "high" | "medium" | "low", "note": string },
      "dimension_2": { "rating": "high" | "medium" | "low", "note": string },
      "dimension_3": { "rating": "high" | "medium" | "low", "note": string }
    },
    "best_for": string,
    "avoid_if": string
  }
]

Rules:
- Rating must be exactly "high", "medium", or "low" — no other values
- Note: one sentence, under 20 words, factual, no superlatives
- best_for: one sentence describing the ideal use case
- avoid_if: one sentence describing when NOT to choose this item
- Return only the JSON array — no preamble, no explanation, no markdown
将在分析器中打开,并预先填入该提示词。

常见问题

Which model is best for structured output?
Most current models offer a native structured-output (JSON schema) mode and tool use: the selectable GPT, Claude, Gemini, Grok, DeepSeek and Kimi models in Promptea's registry all do, while Perplexity's Agent API presets are built for cited web answers rather than schema-constrained output. Prompt quality still matters: an explicit schema, field descriptions and rules for missing values reduce wrong or empty fields even when the format itself is enforced. For critical production pipelines, use the model's native JSON mode or function-calling API rather than relying on the prompt alone: it enforces the schema at the generation level, not just as a request.
What should I do when the model keeps adding prose before the JSON?
Two changes fix this in most cases. First, move the output constraint to the very end of your prompt: 'Return only the JSON. Nothing before it, nothing after it.' Second, start your user message with what you want the model to output: some models treat the first token of the response as completion of whatever you started. If you still get prose, switch to the model's native JSON mode if available — it enforces the constraint at the sampling level, making prose contamination structurally impossible.