<< Home | Prompting craft
Parseable template outputs from GPT-3.5-turbo using ast.literal_eval
| Area | Prompting craft |
| AI used | GPT-3.5-Turbo |
| Source | Reddit thread |
| Posted | 2023-05-08 |
The move
Force LLM outputs to adhere to strict templates by requiring responses to be parseable with Python's ast.literal_eval() and nothing else.| When this works | When it doesn't |
|---|---|
| When prompting instruction-tuned models like GPT-3.5-turbo to produce structured data or key-value pairs matching a specific format. It works when conversational text breaks downstream automated parsing. | When the output requires free-form prose, markdown rendering, or standard JSON formatting incompatible with Python literal syntax. It also fails if downstream code cannot run Python evaluation. |
Key details
- parse-able with python's ast.literal_eval() and nothing else
- list of key-value pairs where the key is the original input
Prompt for your AI
System Prompt:
You are a data formatting assistant. Describe the desired behavior and provide concrete examples of the expected input and output format.
User Prompt Instructions:
1. Role and Goal: Take the user input and transform it into the required output structure.
2. Move: Output a list of key-value pairs where the key is the original input and the value is the task performed.
3. Exact Rule: The output must be parse-able with python's ast.literal_eval() and nothing else.
4. Decision Points: If the input is valid, respond with only the formatted Python literal data structure. If the input is invalid or missing required details, return an empty Python data structure `[]` or `None`; stop and do not write explanatory text.
5. Deliverable: Exactly one Python literal string containing the output data, with no surrounding commentary.
6. Guardrails: Show no preamble, no markdown code block wrappers unless requested, no conversational text, and never invent extra data fields.
Before you give me the final result, check with me:
- Test that the output evaluates successfully using Python's ast.literal_eval().
- Verify there is zero conversational text or markdown before or after the data.
- Check that every key in the output matches the original input verbatim.
- Confirm the structure handles invalid inputs by returning an empty literal instead of prose.
What happened
A developer needed GPT-3.5-turbo to return structured output matching a strict template without conversational filler. By telling the model that output must be parseable with Python's ast.literal_eval() and nothing else in the user prompt, the model consistently returned clean, parseable data structures.Who confirmed it
It has almost always worked.Give it to your agent
Read https://howtheyuse.ai/p/forces-output-into-strict-machine-parseable-python-formats.md and help me do this.