How to review code you copied from an AI assistant or a forum

Copied code solves someone else's problem in someone else's project, often for a different library version. AI assistants add two more risks: they can call functions that do not exist, and they often rewrite more than you asked for.

The example

A Python function fetches exchange rates. It sometimes fails with timeouts and certificate errors, so the developer asks an AI assistant to fix it. The original:

import requests

def fetch_rates(base_url, api_key):
    response = requests.get(
        f"{base_url}/rates",
        headers={"Authorization": f"Bearer {api_key}"},
        timeout=10,
    )
    response.raise_for_status()
    return response.json()

The suggested fix:

import requests

def fetch_rates(base_url, api_key):
    try:
        response = requests.get(
            f"{base_url}/rates",
            headers={"Authorization": f"Bearer {api_key}"},
            verify=False,
        )
        return response.json()
    except:
        return {}

It runs and the errors stop. That is exactly why it is dangerous.

Step 1: diff it against your version

Do not paste the suggestion over your file. Put both versions in Compare Codes. With default settings it looks like a full rewrite, because the new try block indented every line:

@@ -1,10 +1,12 @@ import requests  def fetch_rates(base_url, api_key):-    response = requests.get(-        f"{base_url}/rates",-        headers={"Authorization": f"Bearer {api_key}"},-        timeout=10,-    )-    response.raise_for_status()-    return response.json()+    try:+        response = requests.get(+            f"{base_url}/rates",+            headers={"Authorization": f"Bearer {api_key}"},+            verify=False,+        )+        return response.json()+    except:+        return {}

Turn on Ignore whitespace and only five rows remain: try: added, timeout=10, changed to verify=False,, raise_for_status() deleted, and except: and return {} added. Python uses indentation as structure, so turn the option off once more before accepting anything. Those five rows hold four problems:

  1. verify=False turns off certificate checking. The request, API key included, can now be read and changed by anyone who can intercept it. The error is gone because the check is gone.
  2. timeout=10 was removed. requests has no default timeout, so the call can now wait forever.
  3. raise_for_status() was removed. A 401 or 500 error page is now parsed as if it were rates.
  4. The bare except: catches everything, even a typo or Ctrl+C. The caller cannot tell a failure from a day with no rates.

None of this is visible when you only read the suggestion. All of it is visible in the diff.

Step 2: check that every function and package exists

Step 3: search for risky patterns

Quick fixes often use these because they make an error message go away:

RiskSearch forSafer approach
Certificate checks offverify=False, rejectUnauthorized: false, CURLOPT_SSL_VERIFYPEER false, curl -kFix the certificate chain, or point the client at the right CA bundle.
SQL built from strings"... WHERE id = " + id, f-strings or $"..." in a queryParameterized queries, such as $pdo->prepare("... WHERE id = ?").
Errors swallowedexcept: pass, catch (Exception) { }, .catch(() => {})Catch the specific exceptions you expect and log or raise the rest.
Text run as codeeval(, exec(, shell=True, shell_exec(Pass arguments as a list and never build commands from user input.
Unsafe data loadingpickle.loads, yaml.load(, PHP unserialize(json or yaml.safe_load.
Weak random secretsMath.random(), Python random for tokenscrypto.getRandomValues(), the Python secrets module.
Weak password storagemd5( or sha1( on passwordspassword_hash() in PHP, bcrypt or Argon2 elsewhere.
Hardcoded secretsapi_key =, password =, Bearer with a literal valueEnvironment variables or a secrets manager.
Access opened too widechmod 777, AllowAnyOrigin()List the exact origins, users or paths that need access.

Step 4: test it with your own inputs

Include an empty value, a missing value and a very long value; accented and non Latin text such as Zoƫ or Arabic; zero, one and duplicate items; a server that times out or returns an error; and dates near midnight in your users' time zones.

Step 5: check the license and keep a source note

Stack Overflow code is shared under CC BY-SA, which requires attribution. GitHub code carries its repository's license. For AI generated code, follow your organization's policy. A one line # Adapted from https://... helps the next reader, so keep it when you clean up other comments.

Step 6: write the version you will keep

For the example, a real fix keeps the timeout and the status check, retries only errors worth retrying, and tells the caller clearly when it gives up:

import requests


class RatesUnavailable(Exception):
    pass


def fetch_rates(base_url, api_key, retries=2):
    last_error = None
    for attempt in range(retries + 1):
        try:
            response = requests.get(
                f"{base_url}/rates",
                headers={"Authorization": f"Bearer {api_key}"},
                timeout=10,
            )
            response.raise_for_status()
            return response.json()
        except (requests.Timeout, requests.ConnectionError) as error:
            last_error = error
    raise RatesUnavailable(f"Rates API failed after {retries + 1} attempts") from last_error

requests raises SSLError as a kind of ConnectionError, so certificate errors are retried too. If they keep happening, the certificate chain is the real problem to fix. Before committing, remove comments that only repeat the code and write a commit message that says what changed and why.