ChatGPT Prompt for Code Generation: 12 Spec-First Prompts
Use these ChatGPT prompts for code generation to turn specs into tested code: restate the task, write tests first, set constraints, and review the output.

A ChatGPT prompt for code generation works best when it reads like a small spec: what the code must do, the language version, the limits, and real examples. The 12 prompts below get you from idea to tested code. Fill in each [variable], and run the tests yourself before you trust the result.
Safety note: Never paste API keys, passwords, or private customer data into a prompt. Treat generated code as a draft: run the tests, check for security issues such as injection and unsafe input handling, and have a person review it before it merges.
Write the spec first
1. Spec restatement
Before writing code, restate this task [task] in bullets, list your assumptions, and ask up to two questions that would change the design.
2. Examples to tests
Turn these input and output examples [examples] into a [test framework] test file. Add edge cases the spec implies.
3. Interface only
Propose the function signature and types for [task] in [language and version], matching this existing code [paste]. No implementation yet.
Generate the code
4. Implementation with constraints
Implement [signature] so these tests pass [paste tests]. Use only the standard library and [allowed packages]. Throw [error type] on invalid input.
5. Fit the codebase
Here is a file from my project [paste]. Write [new function] in the same style, naming, and error handling.
6. Data structure choice
For [task] with about [data size], compare two data structures and pick one, with the trade-off in one line each.
7. Script with safety checks
Write a [language] script that [task]. Add a dry-run flag that prints what it would do, and stop on the first error.
Check the output
8. Self review
Review the code you just wrote for bugs, missing edge cases, and functions that may not exist in [version]. List issues before fixing anything.
9. Failing test fix
This test fails with [error output]. Explain the cause, then change only the code needed to fix it.
10. Security pass
Check this code [paste] for injection, unsafe input handling, and secrets in code. Give each issue a line number and a fix.
Ship it
11. Docs and usage
Write a short doc comment and a usage example for [function], including what it throws.
12. Port to another language
Port this function and its tests [paste] from [language A] to [language B and version], keeping the same behavior and test cases.
Use the full spec-first prompt
The flagship Code Generation Prompt: Spec, Constraints, and Tests First takes your task, language version, existing code, constraints, examples, and test framework. It returns a spec restatement, an interface, tests, the implementation, a run command, and risks. Its example builds a TypeScript duration parser with Vitest tests.
Tips for better generated code
Always give versions, since APIs change between releases. Include at least one invalid input in your examples. Run the tests yourself and read every line before merging, because generated code can call functions that do not exist.
Related prompts
To understand code you did not write, use the ChatGPT prompt for code explanation. To clean up what you generated, see the ChatGPT prompt for code refactoring, and for automated reviews, the prompt for a code review agent.
Find more in the Coding category, look through Browse, or post your own on Share.