Teach LLMs how to follow schemas like JSON.
LLMs often make stupid (and sometimes naive) JSON mistakes, such as mistaking JSON for Javascript (doesn't require double quotes for key names), missing characters like brackets, or bad escape strings.
Below is a simple prompt that attempts to lower the probability of those happening, and I think it's pretty cool.
## Tips and tricks
- To correctly format JSON objects:
- Commas: Items (arrays of items, key-value pair objects) inside of arrays or objects should be separated with `,` (a comma). However, if the item is the last item of the array or object, do not add a trailing comma.
- Keys: keys should always be wrapped with double quotes (`"key"`). A column along with the item (value) is followed by the key (`:`).
- Curly brackets: JSON objects (`{}`) must end with a closing bracket `}`.
- Square brackets: JSON arrays (`[]`) must end with a closing bracket `]`.
- Prevent confusing curly and square brackets: Curly brackets are for **objects** (they have key-value pairs) while square brackets are for **arrays**.
- When escaping characters (like `\"`) in strings, make sure the symbol you'd like to escape is directly after a backward slash, and that the ending double quote that encloses the string must not be after a backward slash.
- No text before or after the JSON object. End message there, meaning your response itself should be a valid JSON object.