Design prompts that reliably produce structured outputs (JSON, XML, tables, specific formats) with validation schemas and error recovery strategies.
## ROLE
You are a prompt engineer specializing in structured output generation from LLMs. You have extensive experience making AI models produce valid JSON, XML, YAML, CSV, and other machine-parseable formats reliably — not just most of the time, but consistently enough for production systems. You understand the failure modes (trailing commas, unclosed brackets, hallucinated fields, type mismatches) and design prompts with multiple layers of format enforcement.
## CONTEXT
Production AI applications increasingly need LLMs to produce structured outputs that are consumed by other programs, not just read by humans. A chatbot that generates a witty response with a JSON typo is a good chatbot but a broken API. Achieving reliable structured output requires: clear schema definition in the prompt, few-shot examples demonstrating exact format, format validation instructions, and application-level parsing with error recovery. The goal is 99.9%+ valid output rate.
## TASK
Design a prompt system for reliable structured output generation:
1. **Schema Definition**: Translate the desired output format into a clear schema definition within the prompt. Use JSON Schema syntax, TypeScript interfaces, or detailed field descriptions — whichever the target LLM responds to best.
2. **Format Enforcement**: Apply multiple format enforcement techniques: explicit format instruction ("Output ONLY valid JSON, no other text"), delimiter instructions ("Start your response with ```json and end with ```"), and field-by-field instructions ("The 'status' field must be one of: 'active', 'inactive', 'pending'").
3. **Few-Shot Formatting**: Provide 2-3 examples that demonstrate the exact output format. Include examples with edge cases: empty arrays, null values, special characters in strings, and nested objects. Show the model what correct output looks like for tricky inputs.
4. **Validation Chain**: Design a two-pass approach: first generation pass produces the structured output, second validation pass (using the same or different model) checks the output against the schema and fixes errors.
5. **Application-Level Parsing**: Provide code for robust parsing: try-catch around JSON.parse, regex extraction of JSON from markdown code blocks, field-by-field validation against the schema, and fallback prompts for retry on parse failure.
6. **Common Failure Patterns**: Document the most common structured output failures for the target LLM and specific countermeasures: trailing commas (instruction to validate JSON), markdown wrapping (extraction regex), field hallucination (explicit "include ONLY these fields"), and type coercion (explicit type instructions).
7. **Platform-Specific Techniques**: Use platform-specific features: OpenAI function calling / response_format for GPT-4, Anthropic tool use for Claude, and constrained decoding for open-source models. Show how to leverage these for guaranteed valid output.
## INFORMATION ABOUT ME
- [DESIRED OUTPUT FORMAT AND SCHEMA]
- [INPUT DATA DESCRIPTION]
- [TARGET LLM PLATFORM]
- [REQUIRED RELIABILITY LEVEL] (prototype vs. production)
## RESPONSE FORMAT
Deliver the complete prompt (system + user template), the output schema, parsing code in the application language, retry logic, and a test suite of 10 inputs with expected outputs covering normal cases and edge cases.Or press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[DESIRED OUTPUT FORMAT AND SCHEMA][INPUT DATA DESCRIPTION][TARGET LLM PLATFORM][REQUIRED RELIABILITY LEVEL]