AI & Tools / Article
AI-Generated Code Not Working? How to Debug and Fix It
Photo by Bernd 📷 Dittrich on Unsplash

AI-Generated Code Not Working? How to Debug and Fix It

When code from ChatGPT, Claude, Copilot, Gemini, or another assistant fails, the problem is usually one of a few things: a version mismatch, a missing dependency, a function that doesn't exist, or a misunderstanding of what you asked for. This guide walks you through a practical order of checks so you can find the cause instead of repeatedly pasting "it doesn't work" into the chat.

AI tools write code that looks plausible, and they can be confidently wrong. They don't run your code in your environment unless the tool specifically has an execution feature. Treat the output as a draft written by someone who has never seen your computer, project, or library versions.

Start by reading the actual error

Before changing anything, find the error message and read it from the bottom up. In Python, the last line of a traceback names the error type (for example ModuleNotFoundError or AttributeError). The lines above it show where it happened. In JavaScript, check the browser developer console (usually F12) or your terminal output.

Note three things: the error type, the file and line number, and whether the failure happens at startup, on install, or only when you perform a specific action. That tells you which category below to check first.

Match the symptom to the likely cause

  • "Module not found," "cannot find package," or "command not found": a dependency isn't installed, or you're running a different Python/Node environment than the one you installed into.
  • "has no attribute," "is not a function," or "unexpected keyword argument": the AI may have invented a function or used an older or newer version of a library than yours.
  • Syntax errors: often caused by copy-paste problems, such as mixed tabs and spaces in Python, smart quotes, missing brackets, or a truncated response.
  • No error, but wrong output: a logic problem, usually from an ambiguous request or unstated assumptions about your data.
  • Works in the chat, fails on your machine: differences in operating system, file paths, language version, or permissions.

Fix it step by step

1. Check that you copied the complete code

Long responses sometimes get cut off, and chat formatting can alter indentation. Compare the end of the code to make sure functions and brackets are closed. In Python, make sure indentation is consistent throughout. If you've combined several snippets, confirm that each imported name is defined once and nothing is duplicated.

2. Confirm your language and library versions

Version mismatch is one of the most common reasons AI code fails, because assistants often mix syntax from different releases of the same library. Check what you actually have installed:

  • Python version: python --version (or python3 --version on macOS/Linux)
  • Installed Python package version: pip show package-name
  • Node.js version: node --version
  • Installed npm package versions: npm list package-name

Then compare against the library's official documentation for that version. If a function in the AI's code isn't in the docs for your version, you've found the cause. Either update the library or ask the AI to rewrite the code for your specific version.

3. Verify that every function and method exists

AI tools sometimes invent function names, parameters, or even entire packages. Search the official documentation (or the package's page on PyPI or npm) for each unfamiliar call. Be cautious with any package name you don't recognize: confirm it's a real, established project before installing it, since mistyped or lookalike package names are a known way malicious code spreads.

4. Use the right environment

If you installed a package but still get "module not found," you may have several Python or Node installations. In Python, run python -m pip install package-name instead of plain pip so the package installs for the same interpreter that runs your script. For projects, a virtual environment (python -m venv .venv) keeps dependencies separate and avoids conflicts.

5. Isolate the failing part

If the code is long, shrink the problem. Comment out sections or run one function at a time, and add temporary print() or console.log() lines to see actual values. The goal is to find the smallest piece that fails. A bug in 10 lines is far easier to explain, search for, and fix than one buried in 200.

6. Test with simple known input

When there's no error but the result is wrong, run the code with a tiny example where you already know the correct answer. Compare the output step by step against what you expected. This often reveals wrong assumptions, such as a different date format, empty values, or a column named differently from what the AI guessed.

Ask the AI again, but give it what it needs

Vague follow-ups produce vague fixes. Include the context the AI couldn't see:

I'm using Python 3.11 on Windows 11 with pandas 2.2. This code should [goal]. When I run it, I get this exact error: [full error message and traceback]. Here is the relevant code: [code]. Please explain the cause first, then give the smallest change that fixes it. Only use functions that exist in these versions.

Asking for the cause first helps you judge whether the explanation makes sense. If the first fix fails, paste the new error rather than the old one, and start a fresh conversation with a summary if the chat has become long and confused. Repeated patches can pile up contradictory changes.

If it still doesn't work

  • Search the exact error message (without your file paths) in the library's documentation, GitHub issues, or Stack Overflow. Real-world reports often identify version-specific bugs.
  • Try a second tool or a fresh prompt. A different assistant may take another approach, but still verify its answer against the documentation.
  • Use a minimal working example from the official docs and build up from it. This confirms your setup works before you add AI-written logic.
  • Review code that touches files, databases, or accounts before running it. Look for commands that delete, overwrite, or send data, and test on copies or sample data first.
  • Ask for help. Post a minimal example, your versions, the full error, and what you've tried to a community forum or ask a developer. Remove API keys, passwords, and personal data first.

How to confirm it's really fixed

Run the code from a clean start, not just in the session where it last worked. Test it with at least one normal input and one edge case, such as an empty file or missing value. If it's part of a larger project, run any existing tests and check that nothing else broke. Once it works, save the working versions of your dependencies (for example in requirements.txt or package.json) so you can reproduce the setup later.

Next step

Next time code fails, resist the urge to ask for a rewrite immediately. Read the error, check your versions, and confirm the functions exist. Those three checks resolve most AI code problems, and they give you the details needed to get a useful answer if you do go back to the assistant. If the code handles anything sensitive or important, have an experienced developer review it before relying on it.