---
name: python-bug-squasher
description: "Finds the root cause of a Python bug from the code and traceback, explains why it happens in plain terms, and returns corrected code with the fix marked, plus a check that it will not come back. Use when Python code throws or misbehaves and the traceback or wrong output needs tracing to a root cause with corrected code. Do not use for reviewing working Python for style, or scaffolding a new application. Related topics: coding, python, debugging."
---

# Python Bug Squasher

Finds the root cause of a Python bug from the code and traceback, explains why it happens in plain terms, and returns corrected code with the fix marked, plus a check that it will not come back.

## When to use

Use this skill when Python code throws or misbehaves and the traceback or wrong output needs tracing to a root cause with corrected code. Do not use it for reviewing working Python for style, or scaffolding a new application.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Locate the failing line from the last user-code frame and the exception type, or by tracing state against the expected behavior when there is no traceback.
3. Name the root cause as a mechanism, not a symptom, and explain it in two or three sentences.
4. Apply the chosen fix strategy with syntax valid for the selected Python version and framework.
5. Check the selected focus areas for problems unrelated to the reported bug and list them separately.
6. Return the corrected code in a python fence with changes commented when requested, plus one test or assertion that proves the fix.

## Inputs

- **Python version** (choose one: Python 3.9, Python 3.10, Python 3.11, Python 3.12)
- **Framework used** (choose one: None, Django, Flask, FastAPI, Pandas, NumPy)
- **Problematic code** (longer free text)
- **Error message (if any)** (longer free text)
- **Expected behavior** (free text, e.g. "What should the code do?")
- **Fix strategy** (choose one: Quick Fix, Detailed Explanation, Refactor for Performance)
- **Focus areas** (choose any: Syntax Errors, Logic Errors, Memory Leaks, Security Vulnerabilities)
- **Provide inline comments in the fixed code?** (choose one: Yes, No)

## Output

Root cause (two or three sentences), the corrected code in a fenced python block with changed lines marked when requested, separate findings from the focus areas, and a one-line test or assertion that proves the fix.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Python Bug Squasher

Find and fix the bug in my Python code.

- Python version: {{Python version}}
- Framework used: {{Framework used}}
- Problematic code: {{Problematic code}}
- Error message (if any): {{Error message (if any)}}
- Expected behavior: {{Expected behavior}}
- Fix strategy: {{Fix strategy}}
- Focus areas: {{Focus areas}}
- Provide inline comments in the fixed code? {{Provide inline comments in the fixed code?}}

Start from the traceback when there is one: the last frame in the user's own code and the exception type tell you where and what; read upward only to find why. Without a traceback, trace the code by hand against the expected behavior and find the first line where state diverges from what the author assumed. Name the root cause as a mechanism (an off-by-one on a slice, a mutable default argument, a view returned where a copy was needed, a blocking call in an async route), not as a symptom. Check the focus areas you were given even if they are not the cause of the reported bug, and report them separately. Apply the chosen strategy: Quick Fix changes the fewest lines, Detailed Explanation walks through the failure with the values involved, Refactor for Performance restructures the hot path and says what got faster and why. Use only syntax and library behavior valid for the selected Python version and framework. Return: the root cause in two or three sentences, the corrected code in a fenced python block with changed lines commented when requested, the other findings from the focus areas, and a one-line test or assertion that proves the fix.

## Pitfalls

- Fixing the line in the traceback when the bad value was created several frames earlier.
- Wrapping the failure in a try/except that swallows the exception and calls it fixed.
- Suggesting a standard library feature that does not exist in the selected Python version.
