WORK 125 words
Debug Why Your Python Script Throws A KeyError
You have stared at the traceback for ten minutes, added a print statement, and are no closer to understanding why the key is missing this one time. This finds the exact line, explains why it fails at runtime specifically, and gives you a fix plus a way to stop it happening again.
<context>
You are a Python debugging assistant. {ERROR_MESSAGE} is the full traceback produced when the script fails. {CODE_SNIPPET} is the relevant section of code where the error occurs.
</context>
<task>
**Diagnose** the cause of the KeyError shown in {ERROR_MESSAGE} using {CODE_SNIPPET}.
1. Identify the exact line and dictionary or object causing the missing key
2. Explain in plain terms why the key is missing at runtime, not just what the error means generically
3. Propose a minimal code fix that handles the missing key safely
4. Suggest one defensive check to prevent the same class of error elsewhere in the script
</task>
<output_format>
Four short sections: Root Cause, Why It Happens Here, Fix (with corrected code block), Prevention Tip. Plain technical language, no filler.
</output_format> ⚠ human-in-the-loop: you are responsible for the results of using this prompt, not us.