Skip to content

AIExplore

Debug With ChatGPT Using the Error First

Paste the exact error message and a small failing example before you ask for a fix so ChatGPT starts from the real symptom.

When you debug with a teammate, the first thing you share is the error. Not the feature spec, not the full file, not your theory about what went wrong. You show them the red text and say this is what I see. ChatGPT works the same way. Lead with the error and the conversation stays focused from the start.

The mistake most people make is burying the error at the bottom of a long message or leaving it out entirely. They describe the feature, paste the code, and then mention that something went wrong. By that point ChatGPT has already started forming a response around the code, not the failure. Flip the order and the answer gets sharper.

What error first debugging means

You put the exact error message at the top of your prompt. Below it, you add the smallest piece of code that triggers the error. Below that, you add any context that changes the diagnosis, such as the runtime version or the operating system. The error is the headline. Everything else is supporting evidence.

When to lead with the error

  • You have a stack trace or error message you can copy
  • The error is specific enough to narrow the search
  • You have tried reading the message yourself and it did not click
  • The same error appears in multiple places and you want a single root cause
  • You are under time pressure and need the fastest path to a fix

Prompt for a TypeError in JavaScript

Error:
TypeError: Cannot read properties of undefined (reading 'map')
  at renderList (components/List.js:8:22)

Code that triggers it:
function renderList(data) {
  return data.items.map(item => <li>{item.name}</li>);
}

// Called with: renderList({ })

Task: explain what causes this TypeError and suggest the smallest fix.

Why this prompt works

The error comes first. ChatGPT immediately knows the issue is about accessing a property on undefined. The code confirms that items is not present in the object. The fix will be a guard check or a default value. Without the error at the top, ChatGPT might review the whole function for style issues you did not ask about.

Prompt for a SQL syntax error

Error:
ERROR 1064 (42000): You have an error in your SQL syntax near 'LIMIT 10 OFFSET'

Query:
SELECT name, email FROM users
WHERE active = 1
ORDER BY created_at
LIMIT 10 OFFSET;

Task: identify the syntax mistake and show the corrected query.

Prompt for a build failure

Error (from CI log):
Module not found: Can't resolve './utils/helpers'
  at /app/src/index.ts:3:1

Directory listing:
src/
  index.ts
  utils/
    Helpers.ts

Task: explain why the import fails and how to fix it.
Note: the build runs on Linux.

What to include in an error first prompt

  • The exact error message, copied from the terminal or log
  • The smallest code snippet that triggers the error
  • The runtime, language version, or operating system if it could matter
  • What you expected to happen instead of the error
  • A note about what you already tried if anything

How to refine when the first fix does not work

Paste the new error or the same error with the updated code. Say I applied your suggestion but now I see this. Keeping the conversation in a chain lets ChatGPT adjust without losing the original context. Avoid starting a new chat unless the problem has changed completely.

Common mistakes

  • Describing the error in your own words instead of pasting the exact text
  • Including hundreds of lines of code when only five are needed
  • Burying the error at the end of the message after a long explanation
  • Asking for a fix without sharing the code that triggers the error
  • Ignoring the stack trace, which often points directly to the line that breaks

How to confirm the fix works

Apply the change in your code, run the same action that caused the error, and check that the error is gone. If a new error appears, share it in the same thread. If the error is gone but the behavior is still wrong, that is a different problem. Start a new prompt with the new symptom.

Takeaway

The error message is the most useful thing you have when debugging. Put it first, add just enough code to show the trigger, and let ChatGPT trace the cause from the symptom.

Related articles