How-to guide

How to write a ChatGPT prompt for code

A good coding prompt gives ChatGPT four things: the code itself, what it should do, what it's doing instead, and what you want back. Most bad answers come from leaving one of those out, so the model fills the gap with a guess. Here's how to write one, the mistakes that waste a round trip, and a template you can copy.

What to put in the prompt

  1. 1.

    Paste the real code

    The actual function, query or error, in a code block. Not a description of it. The model can't debug code it can't see, and paraphrased code tends to lose the bug.

  2. 2.

    Say what it should do

    One sentence on the intended behaviour, like “this should return the user's last 10 orders, newest first”. That's what the model checks its answer against.

  3. 3.

    Say what happens instead

    The exact error message, the wrong output, or how slow it is. “It takes 4 seconds on 200k rows” is something the model can work with. “It's broken” isn't.

  4. 4.

    Give the setup

    Language, version, framework, database. “Python 3.12, Django 5, Postgres” takes five seconds to type and rules out a whole class of wrong answers.

  5. 5.

    Say what must not change

    The function signature, the output format, a library you can't swap out. Without this you'll often get a rewrite of the parts that were fine.

  6. 6.

    Ask for a specific answer

    Just the fixed code, the fix plus why it broke, or a diff. Say which. If you want to understand it, ask for the reason first and the code second.

Common mistakes

A template you can copy

Goal:
[What the code should do, in one sentence.]

Context:
[Language, version, framework.]
Here's the code:
```
[paste the code]
```
What happens instead: [the exact error or the wrong output]

Requirements:
- Keep [the function signature / the output format / this library] the same.
- [Anything else it must or must not do.]

Output format:
[The fixed code only / the reason it fails, then the fix / a diff.]

Fill in the brackets and delete the lines you don't need. A short prompt with the code and the error beats a long one without them.

A real before and after

From our test runs. A one-line question, and the prompt Polido rewrote it into. The code in backticks comes through exactly as typed.

In a coding assistant, from our test runs

You type

why does this fail `const [a,b] = obj` obj is {a:1,b:2}

Polido sends

Goal: Explain why destructuring assignment fails when trying to extract properties from an object using array destructuring syntax. Context: The code is `const [a,b] = obj` where `obj` is `{a:1,b:2}`. I want to understand why this specific syntax does not work, and what the correct syntax would be. Requirements: - Explain what array destructuring does and why it fails on an object. - Show the correct syntax for destructuring object properties. - Explain the difference between array destructuring and object destructuring. - If relevant, mention why the syntax looks similar but behaves differently. Constraints: - Keep the explanation focused on this specific case, not a general destructuring guide. - Use the exact code from the question in your explanation. Output format: Start with a one-sentence explanation of why it fails, then explain the difference between the two syntaxes with side-by-side examples, then show the correct way to write it. What good looks like: Clear, direct, and answers the "why" not just the "how". A reader should understand both why the code fails and why the fix works.

Polido writes this structure for you. Type the quick version in ChatGPT, Claude or any AI chat, double-tap right ⌘ (right Ctrl on Windows), and it's rewritten in place, with your code left exactly as you typed it.

Download Polido

Free: 100 rewrites in your first two weeks, then 20 a month. Pro is $10 a month for 500 ($8 billed annually).

Related: Coding prompts for Claude and ChatGPT, Better ChatGPT prompts

Questions

Should I tell ChatGPT to act as a senior developer?
It rarely hurts, but it isn't what makes the answer good. The code, the error and what you want back do far more than a persona line.
How much code should I paste?
Enough to reproduce the problem: the function that fails, the code it calls, and the error. For a big file, paste the relevant part and say what's around it.
Does this work for Claude and Gemini too?
Yes. The same structure works in any AI chat, because the problem it solves, the model guessing what you meant, is the same everywhere.
Why does ChatGPT keep changing code I didn't ask it to touch?
Because nothing in the prompt said to leave it alone. Add a line like “keep the function signature and don't change anything outside this function”.

Get updates

Polido, the character, holding up a newspaper

The Polido newsletter: new features and the odd writing tip, only when there is something worth saying. No spam, and unsubscribe in one click.