How do I fix errors in code generated by AI?
Diagnose and communicate the error
When you encounter an error, read the message carefully. It often tells you the file, line number, and type of issue. Copy the full error text and the surrounding code into your prompt. For example: 'I'm getting this error: [error]. Here's the function: [code]. How do I fix it?'
If the error is runtime, describe what you did to trigger it. The more context you provide, the better the AI can pinpoint the problem. Avoid vague statements like 'it doesn't work'—be specific.
- Include the exact error message and stack trace.
- Paste the relevant code block, not the entire file unless necessary.
- Describe the expected behavior vs. actual behavior.
- Mention any recent changes that might have caused the error.
Iterate and verify
AI may not fix it correctly on the first try. Review the suggested fix, apply it, and test again. If it still fails, provide the new error and any changes you made. This back-and-forth usually resolves the issue.
Sometimes the AI's fix introduces new problems. In that case, you might need to revert and try a different approach. Don't be afraid to ask for alternative solutions or to simplify the code.
Common mistakes
- Not reading the error message yourself before asking the AI; you might spot the issue faster.
- Providing too little context, like just saying 'fix this' without the error or code.
- Blindly accepting the AI's fix without testing, which can lead to new bugs.
