# Structured Output Format Enforcer

Designs a prompt that reliably gets an AI model to return output in a specific structured format (JSON, table, or custom schema) without malformed or inconsistent results.

## Prompt

You are a prompt engineering expert who specializes in getting AI models to reliably return correctly structured output.

Desired output structure (describe the schema, e.g., JSON fields and types, table columns, or a custom format): {{desired_output_structure}}
Task the model needs to perform: {{task_description}}
Where this output will be used (e.g., parsed by code, displayed directly, fed into another prompt): {{output_destination}}

Do the following:
1. Write a precise structure definition within the prompt, specifying exact field names, data types, and any formatting rules (e.g., date format, whether fields can be null, max length constraints).
2. Include one clear example of correctly formatted output matching the schema exactly, to reinforce the pattern.
3. Add explicit instructions for edge cases: what to do if a required piece of information is missing or unclear (e.g., use null, an empty string, or a specific placeholder value, rather than guessing or omitting the field).
4. Add an instruction to return ONLY the structured output with no additional commentary, preamble, or markdown formatting wrapper, if the output is meant to be parsed programmatically.
5. Provide the final assembled prompt, and note one likely failure mode to watch for even with a strong prompt (e.g., the model adding explanatory text despite instructions) with a suggested code-side safeguard (e.g., stripping markdown fences before parsing).

## Best for

Developers building AI-powered features that need reliably parseable output, such as data extraction, classification, or generation tasks feeding directly into code or automated workflows.

## Compatible tools

- Claude
- ChatGPT

## How to use

- Describe your exact desired output schema and what the output will be used for downstream.
- Use the assembled prompt in your API call or system prompt.
- Implement the suggested code-side safeguard as a fallback even if the prompt is well-designed.
- Test the prompt against several different inputs, especially edge cases with missing information, to confirm the schema holds up.

## Customization tips

- If you're using a model/API with a native structured output or function-calling feature, mention it so the prompt can be adapted to that mechanism instead of relying purely on prompt instructions.
- For complex nested schemas, provide 2 examples instead of 1 to reinforce handling of nested or array fields.
- If certain fields are more error-prone than others based on past testing, flag them so extra explicit guidance is added specifically for those fields.

## Example input

Desired structure: JSON with fields 'name' (string), 'email' (string or null), 'urgency' (one of 'low', 'medium', 'high'). Task: Extract contact info and urgency level from a customer inquiry email. Destination: Parsed directly by a Python script.

## Example output

A precise schema definition with exact field names and types, an example JSON output showing correct formatting, an instruction to use null for a missing email field rather than omitting it or guessing, an explicit instruction to return only the JSON with no markdown code fences or explanatory text, the final assembled prompt, and a flagged likely failure mode that the model may still wrap output in markdown code fences occasionally, with a suggested code-side safeguard to strip ```json fences before parsing as a fallback.
