Prompt to Fix AI Code: Catch Hallucinated APIs
Forces an inventory of every API the code uses, flags invented symbols and wrong signatures, and gives a verification plan before you run anything.
The code below does not work, or I do not trust it. Your job is not to run it — it is to find the invented parts. LANGUAGE / VERSION: [LANGUAGE] LIBRARY / VERSION: [LIBRARY] ERROR MESSAGE: [ERROR] CODE: [CODE] STEP 1 — API INVENTORY List every external function, class, method and parameter the code uses. Three columns: | Symbol | Where it comes from (library/version) | Certain? (YES / NO / DON'T KNOW) | STEP 2 — FABRICATION SCAN Flag these patterns separately: - A standard function that does not exist (plausible name, absent from the library) - Correct name, wrong parameter order, or a missing required argument - Removed or deprecated API - A constant or enum value that does not exist - Wrong return type (docs return an object, the code expects a string) - Logic that runs silently wrong (no exception, wrong result) — most dangerous group, give it its own heading STEP 3 — MINIMAL FIX Per error: line number, problem, corrected line. Do not rewrite the file — give only the broken lines. STEP 4 — VERIFICATION PLAN Tell me the three checks to run before executing: (a) which documentation page, (b) which test input, (c) which output to expect. RULE Do not guess about an API you do not know. Write "DON'T KNOW" — that is information, not a failure.
The "DON'T KNOW" option is the heart of this prompt. Getting the model to admit uncertainty is worth more than getting a confident answer.
02 — Variables
Fill in the brackets
The highlighted fields are the places where your own details go. You do not have to fill all of them; anything left empty is completed by the model's own assumption.
[LANGUAGE]
[LIBRARY]
[ERROR]
[CODE]
03 — How to use
How to use it, step by step
- 01
Paste the error message verbatim. Summarising it sends the model in the wrong direction.
- 02
State the version. The model's most common mistake is using an API from an older release; naming the version removes most of it.
- 03
Take the silently-wrong-logic heading seriously. That group throws no exception and corrupts data in production.
- 04
Actually do the three checks in step 4. The model's fix can be hallucinated too.
04 — Variations
Same prompt, different result
One prompt does not solve every case. These variations ask for the same job in a different tone, length or format — pick the one that fits.
Dependency check
List every external package this code uses and its version. For each: (1) does this API exist in that version, (2) was there a breaking change in the last 12 months, (3) is our code affected. Write "VERIFY" on any line you are unsure about and say which doc page to read. Code: [CODE]
To get ahead of a broken upgrade.
Generate tests
Write tests for the function below: normal case, boundary case, empty/null input, wrong type. Before running anything, state in one line what each test proves. Function: [CODE]. Test framework: [LIBRARY].
To prove the fix.
05 — Tips
Notes that take the result up a level
- Hallucinated code often compiles and then fails — so "it ran" is not proof. Read the step 1 inventory.
- Name the library version when you first ask for code. Fixing it afterwards costs more.
- IDE assistants (Copilot, Cursor) invent less in autocomplete because they see your context; chat models invent more because they do not see the project.
06 — FAQ
Frequently asked about AI Errors
Why does AI invent functions?
It follows the pattern of what should logically exist in that library rather than looking at the actual API surface. Names like `session.fetch_all()` look right and are not there. The most common fabrication is a correct library plus a plausible but non-existent method name.
Which model hallucinates least in code?
Claude and the top GPT tiers are more consistent on large codebases; Gemini is strong at analysing long files. None is free of it. What separates them is whether they notice when they have invented something. Building the API inventory works better than switching models.
How risky is shipping AI code unchecked?
High from a security standpoint. AI code usually works and usually lacks input validation, authorisation checks and error handling. At minimum verify three things: input sanitisation, permission boundaries and secret leakage. Those are precisely what AI skips.
Next
You may also find these useful
Prompt That Stops AI Hallucinations (Claim Labelling)
Splits the answer into three layers: verifiable fact, inference and guess. The model learns to say "I don't know", and you see which sentences you can trust.
Intermediate ChatGPTClaudeChatGPT Refuses to Answer? A Prompt That Opens the Refusal
Separates the cause of a refusal (safety filter, over-caution, vague request) and builds the right frame for each. It gets your work done without wandering.
Intermediate ChatGPTClaudePrompt for When the Model Forgets Your Instructions Mid-Chat
The model writes your rules back in its own words, then checks every reply against that list. Still working 40 messages later.
Intermediate ChatGPTClaudePrompt That Turns a Generic AI Answer Into Real Analysis
Bans empty lines like "it is important" and demands a claim, a counter-claim and the missing side for each point. Real analysis in three rounds.
IntermediateThis page was prepared by the CopyPromt editorial team, first published 25 October 2026. Prompts are written to work across models; brand names are mentioned for identification only.