Magic Tools
AI TutorialsBy CooconApril 22, 2026219 views4 min read

How to Use Gemini for Code Debugging (2025)

Debugging is the worst part of writing code. You stare at the same error for an hour, and it turns out to be a missing semicolon. Gemini can cut that time down significantly — here's how to actually use it.


Scenario 1: You Got an Error and Have No Idea What It Means

Paste the full error into Gemini. Don't summarize it. Copy the whole thing.

Say you're working in Python and you see this:

Traceback (most recent call last):
  File "app.py", line 14, in <module>
    result = process(data)
  File "app.py", line 8, in process
    return data["user"]["name"]
KeyError: 'user'

Just paste that into Gemini and say:

"I got this error in Python. What does it mean and how do I fix it?"

Gemini will tell you that data doesn't have a key called "user" — either it's None, the key name is wrong, or the API response changed. It'll also suggest using .get() with a fallback or adding a check before accessing the key.

This works for JavaScript, TypeScript, Go, Rust — any language.


Scenario 2: Your Code Runs But the Output Is Wrong

No error, just wrong behavior. Give Gemini the expected output and the actual output — that context is everything.

function applyDiscount(price, discountPercent) {
  return price - discountPercent / 100;
}

console.log(applyDiscount(100, 20)); // Expected: 80, Got: 99.8

Ask:

"This function should apply a percentage discount. Input is 100 and 20%, but I'm getting 99.8 instead of 80. What's wrong?"

Gemini catches it immediately: you're dividing discountPercent by 100 first (getting 0.2), then subtracting from price. The fix is return price - (price * discountPercent / 100).


Scenario 3: You Have Two Versions of Code and Don't Know What Changed

Paste both into Gemini:

"Here's version A and version B. What are the differences? Could any of those differences cause a bug?"

Version A:

def get_user(user_id):
    user = db.query(User).filter(User.id == user_id).first()
    return user.to_dict()

Version B:

def get_user(user_id):
    user = db.query(User).filter(User.id == str(user_id)).first()
    if user:
        return user.to_dict()
    return None

Gemini will spot the str() cast and null check, and tell you which change is more likely to introduce a regression.

For visual diffs, use MagicTools Diff Checker — paste both versions and see differences highlighted side by side before asking Gemini.


Scenario 4: Let Gemini Write Test Cases to Expose the Bug

Give Gemini your function and say: "Write unit tests. Try edge cases that might expose bugs."

function slugify(text) {
  return text.toLowerCase().replace(/ /g, '-');
}

Gemini will write tests revealing the function doesn't handle punctuation or multiple spaces — exposing multiple bugs at once.


Prompt Templates

Explain an error:

I got this error in [language]:
[paste full error]
What does this mean and what's the most likely fix?

Logic bug:

This function should [describe goal].
Input: [input]
Expected output: [expected]
Actual output: [actual]
Code: [paste code]
What's wrong with the logic?

Write tests to find bugs:

Write unit tests for this function. Focus on edge cases that might expose bugs.
[paste code]

FAQ

Is Gemini accurate when debugging code?

Most of the time, yes — especially for common errors. It's best at explaining error messages, spotting logic mistakes in small functions, and suggesting edge cases. Treat it like a smart colleague, not an oracle.

What if Gemini gives me a fix that doesn't work?

Tell it. Say "that fix didn't work, here's what happened" and paste the new error. Debugging with Gemini is a conversation, not a one-shot query.

Should I paste my entire codebase?

No. Paste only the relevant function or file. Too much context makes the response vague. If the bug spans multiple files, explain the connection in plain text and paste only the key parts.

Does Gemini work better with some languages than others?

It's strongest with Python, JavaScript, TypeScript, and Go. It still handles Rust, C++, and others reasonably well, but for niche languages you may need to provide more context.

Can I use Gemini in my editor?

Yes. Gemini Code Assist is available in VS Code and JetBrains IDEs. You can highlight code and ask questions directly in the sidebar — saves a lot of copy-paste time.


Before asking Gemini about a diff, use MagicTools Diff Checker to visualize the two versions first. It highlights additions, deletions, and changes line by line — making it easier to write a focused question.

Related Articles

Blocking rm -rf in Claude Code: A Deny Rule Stopped 14 Deletes in Two Weeks, 7 Other Spellings Still Get Through, and a Hook That Closes the Gap

To keep Claude Code from deleting things by accident, I added a deny rule, Bash(rm -rf *), to my global settings. Across my sessions from October 7 to 21 it blocked 14 deletes. After each denial the model picked a new directory name 8 times and dropped the delete 6 times. It never tried another spelling. Then I replayed 22 delete commands verbatim against Claude Code 2.1.285 through a stub. rm -fr, rm -r -f, rm -Rf and rm -r deleted the directory in manual, auto and bypass mode alike. /bin/rm, find -delete and bash -c deleted it in manual and bypass mode. In auto mode, rm -fr inside the working directory skips the classifier because it 'would be allowed in acceptEdits mode'. A second, built-in dangerous-rm check fires on targets like $D/*, even in bypass mode, but loses track of a variable assignment when a heredoc comes earlier in the same command. The post ends with a PreToolUse hook that blocks every recursive spelling in all three modes and tells the model what to do instead.

claude-codehooks+5
pitfallsOct 21, 20268 min
315

The Self-Reinforcing Flywheel Is Harder Than It Looks: 40 Days of an LLM Auto-Tuning Loop

We ran an 'LLM proposes, code decides' auto-tuning loop on a daily WeChat newsletter for 40 days: 18 versions, 8 rollbacks. Three of those rollbacks overturned decisions a human had made, and one version whose knobs were identical to its parent still got rolled back. Using our real numbers, telling apart a 20% difference needs about 25,000 reads per version. At 60 reads a day, that is more than 400 days. This postmortem covers five reasons the flywheel never turned, and what actually moved the numbers.

wechatpostmortem+4
pitfallsOct 21, 20268 min
211

Dev Breakfast · 2026-10-21

Today's headline: A single "bonsoir, Elliot" makes the model leak its .env: a backdoor for under $50. Plus 4 more: DeepSeek open-sources its inference engine: V4.1 Flash only, and KV cache still missing; TorchTPU goes open source: PyTorch models can run directly on TPU; and more.

daily-intelOct 21, 20268 min
536

Dev Breakfast · 2026-10-20

Today's headline: Should You Merge AI Code You Don't Understand: Settle the Abstraction Layer Yourself First. Plus 4 more: tmux as a Remote Control: Multi-Session Claude Code and send-keys; headroom Goes Open Source: 95% Token Savings on JSON, Only 20% for Coding Agents; and more.

daily-intelOct 20, 20265 min
492

Published by Magic Tools

How to Use Gemini for Code Debugging (2025) | Magic Tools