Dealing with AI Hallucinations in Code
AI coding assistants sometimes "hallucinate"—generating code that uses non-existent APIs, deprecated methods, or imaginary libraries. Here's how to identify and fix these issues.
What Are Code Hallucinations?
Hallucinations occur when AI confidently generates code that:
- References APIs that don't exist
- Uses deprecated syntax as if it's current
- Invents library methods or parameters
- Mixes concepts from different frameworks
- Creates plausible-looking but non-functional code
Real Examples
Invented API Methods
// AI hallucination: This method doesn't exist
const user = await supabase.auth.getUserByEmail(email);
// Actual Supabase API
const { data } = await supabase
.from('users')
.select('*')
.eq('email', email)
.single();
Mixed Framework Syntax
// AI mixing Next.js App Router and Pages Router
// This won't work in App Router
export async function getServerSideProps() {
// ...
}
// Correct App Router approach
async function Page() {
const data = await fetchData(); // Direct async in component
return <div>{data}</div>;
}
Deprecated Methods
// AI using deprecated React patterns
componentWillMount() { // Deprecated
this.fetchData();
}
// Modern approach
useEffect(() => {
fetchData();
}, []);
How to Spot Hallucinations
1. TypeScript Errors
TypeScript is your first line of defense:
Property 'getUserByEmail' does not exist on type 'AuthClient'
2. Runtime Errors
TypeError: supabase.auth.getUserByEmail is not a function
3. Import Failures
Module '"@supabase/supabase-js"' has no exported member 'createClient2'
4. Documentation Mismatch
If you can't find the method in official docs, it's likely hallucinated.
Prevention Strategies
1. Specify Versions
"Using Next.js 14.1 with App Router and Supabase JS v2.39,
create a function to fetch user data."
2. Ask for Documentation Links
"Create a Supabase query and include a link to the
relevant documentation for each method used."
3. Request Explanations
"Create this function and add comments explaining
why each method is used and what it does."
4. Verify Immediately
Run the code right after generation. Don't accumulate unverified code.
5. Cross-Reference
Check multiple sources:
- Official documentation
- GitHub repository
- Stack Overflow
- Another AI tool
Fixing Hallucinations
Step 1: Identify the Correct API
Search the official docs for similar functionality:
site:supabase.com/docs get user by email
Step 2: Ask AI to Correct
"The method 'getUserByEmail' doesn't exist in Supabase.
Looking at the docs, I should use a query on the users table.
Please rewrite using the correct Supabase v2 API."
Step 3: Test Thoroughly
After fixing, test all edge cases:
- Valid input
- Invalid input
- Empty results
- Error conditions
Common Hallucination Patterns
| Pattern | Example | Reality |
|---------|---------|---------|
| Invented methods | array.unique() | Use [...new Set(array)] |
| Wrong parameters | fetch(url, { mode: 'no-cors' }) | May not work as expected |
| Deprecated APIs | componentWillMount | Use useEffect |
| Mixed frameworks | Next.js Pages + App Router | Choose one |
| Fake libraries | import { magic } from 'utils' | Verify package exists |
Found a hallucination you can't fix? Post it on CoderVibez and get expert help!