Prompt Engineering: Show the Error, Get the Fix
An error message is a fact the model can use. A prompt that shows the error gets a fix for the error.
The broken code and its error
The first version of the function had no answer for an unknown order. The run fails with a KeyError.
Fix this Python function. It raised KeyError: 1045 when called with order 1045.
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders[order_number]
Make an unknown order number return "unknown" instead of raising an error. Give only the fixed code.The break: a string where a number belongs
The call passed the order number as a string. The table uses number keys, so the lookup fails.
Fix this Python function. It raised KeyError: '1042' when called with order_status("1042") because the order table uses number keys and the call passed a string.
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders.get(order_number, "unknown")
Make the function accept a string or a number for the order number. Give only the fixed code.Ask for the fix and the check
The prompt asks for the fixed code and the calls that prove it works.
Fix this Python function so an unknown order number returns "unknown".
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders[order_number]
Give only the fixed code, then list the calls order_status(1042) and order_status(1045) with the result of each call.Example
Fix this Python function. It raised KeyError: 1045 when called with order 1045.
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders[order_number]
Make an unknown order number return "unknown" instead of raising an error. Give the fixed code and one sentence on what caused the error.Key points
- An error message says what went wrong and where it went wrong.
- A prompt with the error message gives the model the fact it needs to fix the code.
- The prompt must show the code and the error together. One without the other is not enough.
- A fix that the model writes must be run again. A fix that is not run is only a guess.
- The same error twice means the prompt needs a clearer fact, not a louder ask.
This example runs the error prompt against the local model and shows the fixed function it wrote.
Note
The error message is real text from a real run. Never rewrite it.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What should a fix prompt show the model?