Thursday, 3 Dec 2026 Global Edition
Live Wire
AI Today Daily AI journalism & intelligence
Front Page Tips & Tutorials Baca versi Indonesia

Stuck on a Stubborn Error? A Practical Guide to Debugging Your Scripts with AI Chat

Stuck on a stubborn error? Seven habits for debugging with AI chat: paste the full traceback, give real context, test fixes one at a time.

Stuck on a Stubborn Error? A Practical Guide to Debugging Your Scripts with AI Chat

It is late, your deadline is tomorrow, and the script that ran fine yesterday now dies with a wall of red text. This used to cost an hour or two: scrolling forums, trying random fixes, pasting in code snippets that had nothing to do with your problem. That workflow can now be cut down to fifteen minutes, because AI chatbots like ChatGPT, Claude, and Gemini are at their best in exactly one situation: reading an error message and pointing at the root cause — provided you know how to report the bug to them.

This article is not about writing new scripts from scratch. It is about fixing a script or small program that already exists and refuses to run. The seven habits below can be put into practice tonight.

1. First, Understand Why AI Is Good at Debugging

Debugging is fundamentally pattern matching: a given error message almost always points to a specific cause. AI models are trained on millions of programming conversations, so they recognize patterns like TypeError, KeyError, or ModuleNotFoundError instantly. Small wonder that Stack Overflow's annual survey finds the vast majority of developers now using or planning to use AI tools in their workflow. But there is a hard limit: the AI cannot run your code, cannot see your folder, and only knows what you paste into the chat. The principle is simple — the closer your report is to what an experienced developer would write, the sharper the diagnosis. Our only real job, then, is to produce a complete bug report. The next six habits all serve that goal.

2. Copy the Full Error Message, Never Retype It

The most common beginner mistake is copying only the last line, such as TypeError: unsupported operand type(s), without everything above it. The traceback carries the key information: file name, line number, and the code that triggered it. Always copy the whole error block straight from the terminal instead of retyping it from memory. Here is what a good error block looks like:

Traceback (most recent call last):
  File "report.py", line 12, in <module>
    average = total / len(values)
TypeError: unsupported operand type(s) for /: 'str' and 'int'

From this block alone, the AI can already guess the cause: numbers read from a CSV were never converted from text to actual numbers. Compare that with a one-line report like my code is broken, please help — that forces the AI to guess, and its guesses are often wildly off.

3. Include the Code, the Expectation, and the Environment

An error is only a symptom. A diagnosis needs three more ingredients: the code that triggered it, the result you expected, and the environment (operating system, language version, library versions — check with python --version or pip list). The following template works for almost any small bug and can be reused forever:

My Python script fails. Please help me fix it.

Context:
- Goal: sum the 'sales' column in data.csv and compute the average
- Environment: Windows 11, Python 3.12
- Expected: the script prints the total and the average
- Actual: it stops with the error below

Full error message:
[paste the whole traceback here]

Code (lines 1-20):
[paste your code here]

Pay attention to the expected-versus-actual pair. Many bugs are not technical failures at all but wrong expectations: the code does exactly what it says, just not what you meant. The AI can only catch this class of bug if you spell out both sides explicitly.

4. Ask for the Root Cause Before Asking for the Fix

The biggest temptation is to copy the corrected code immediately. Hold on, and ask for an explanation first:

Before giving me the fixed code, explain in plain language:
1. What does this error message mean?
2. What is the root cause?
3. How can I prevent similar errors in the future?

Two things come out of this. First, the explanation often reveals that the real fix belongs somewhere other than the failing line: in the example above, the actual problem happens while reading the CSV, not while dividing. Second, you learn — the next debugging session, you will diagnose half the problem before even opening the AI. Think of it as a rubber duck that finally talks back.

5. Apply Fixes One at a Time, and Keep the Old Version

The AI sometimes hands you three solutions at once. Do not paste them all. The rule: one change, rerun the exact same command, compare the before-and-after errors, and only then move to the next suggestion. Before editing, keep the old version — duplicate the file as report_backup.py, or use git if you are comfortable with it. The reason is practical: AI fixes sometimes just reshape the error rather than remove it. If the message gets longer after two attempts, it is easy to roll back to a clean state and try another path. One more thing: if a suggested fix uses a function you do not recognize, check its documentation — AI tools occasionally name functions that do not exist or are long deprecated.

6. Stuck? Shrink It Down to a Minimal Example

If the script is hundreds of lines and the AI keeps guessing wrong, stop pasting everything repeatedly. Build a minimal reproducible example: the shortest possible version (usually 5-15 lines) that still produces the exact same error. The shrinking process itself often reveals the bug, because you are forced to separate the problematic part from everything else. The AI can help with this too:

My script is 80 lines long and fails; it is hard to summarize.
Help me build a minimal example: the shortest version that
still reproduces the same error. Ask me first for any
information you are missing.

Bonus: the conversation stays short, so you do not burn through the context window — which matters in long debugging sessions.

7. Two Safety Rules: Test the Result, Protect Your Data

First, AI-written code still has to be tested. Run it, check the output against one or two numbers you calculated by hand, and only then use it for real work. For scripts that touch important files, add a test mode first: process only the first 10 rows before letting it loose on the full data set. Second, never paste secrets into a chat: API keys, passwords, customer data, or the contents of your .env file. If the bug involves sensitive data, replace it with fake data of the same shape first — the AI only needs the pattern, not the real values. A logging request like this is usually enough:

Add logging at the key points of this script so I can see
the data type of every variable before the failing line.
Do not change the program logic, and do not use real
data in your example output.

Debugging with AI is not about finding a magic prompt; it is about building the habit of reporting cleanly: full errors, complete code and context, expectation versus actual, fixes tested one at a time, and secrets kept at home. After five to ten sessions, the pattern becomes reflex — and the wall of red text that once looked terrifying turns into a simple set of directions. Your script stays in control; the AI is only a brighter flashlight.

References

Partner Spotlight Explore today’s curated technology offers