Prompt Engineering: When the Model Breaks the Shape
A model can break the shape it was asked for. Three common breaks are a missing field, extra text around the JSON and a wrong type.
The break: a missing field
The prompt asks for JSON but names no fields. The reply leaves a field out.
Give the status of order 1042 as JSON.The fix: name every field
The prompt names every field and marks each one required. This fixes the missing field.
Give the status of order 1042 as JSON. The schema is order_number as a number, customer as a string, product as a string and status as a string. All four fields are required.The fix: only JSON and the right types
The prompt asks for only the JSON object and names the type of each field. This fixes the extra text and the wrong type.
Give the status of order 1042 as JSON. Reply with only the JSON object and no other text. The schema is order_number as a number, customer as a string, product as a string and days_ago as a number. All four fields are required.Example
Give the status of order 1043 as JSON. Reply with only the JSON object and no other text. The schema is order_number as a number, customer as a string, product as a string and status as a string. All four fields are required.Key points
- A missing field is a field the schema required and the reply left out.
- Extra text around the JSON is words before or after the JSON object.
- A wrong type is a value in the wrong shape. A number written as words is a wrong type.
- Each break has a fix in the prompt. Name the fields, ask for only the JSON and name the types.
- A broken reply is fixed by changing the prompt, not by asking the same prompt again.
This example runs the fixed shape prompt for order 1043 against the local model and shows the reply it gave.
Note
A reply that looks like JSON is not JSON until a program can read it.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
The reply has words before the JSON object. What fixes it?