How Do You Get ChatGPT to Write the Function You Actually Want?
Give ChatGPT the exact signature and a few real input and output examples, and ask only for the body. That replaces a paragraph of description with something the code either matches or doesn't.
Write the body of this function. Keep the signature exactly as it is. ```[language] [paste your signature with types, or a stub with a docstring] ``` It must produce these results: - [input 1] -> [output 1] - [input 2] -> [output 2] - [an edge case, such as empty input] -> [what should happen] - [an invalid input] -> [the error it should raise] Runs on [language version and runtime]. Use only [standard library / the libraries you allow]. Don't change other code. Give me the function first. Then turn the examples above into [test framework] tests. Then list any assumption you made that my examples don't settle.
- Normal case"Hello, World!" gives "hello-world"
- Edge case"Crème brûlée" gives "creme-brulee"
- Invalid caseAn empty string raises ValueError
Try it now
- 1Pick a small function you need, and write its signature plus three examples, including one that should fail.
- 2Paste the prompt into New chat, or into Codex in your IDE, where you can add the open file or selected code to the composer.
- 3Run the tests it returns. It worked if they pass unedited and the "assumptions" list contains nothing you disagree with.
Use it when
Pure functions, parsers, validators, formatters, and data transforms: anything where you can write down what goes in and what comes out. It's especially useful when the codebase is typed, since the types already state half the contract.
Skip it when
Exploratory work where you don't yet know the shape of the answer. Ask for options first. It also suits poorly when the function's job depends on lots of nearby code, such as a request handler that touches auth and sessions. Let Codex read the repo there, since an isolated signature would hide what matters.
Why it works
Examples cut out ambiguity that prose leaves in. OpenAI's prompt engineering guide says a handful of input and output examples can steer a model toward a new task, and advises you to "show a diverse range of possible inputs with the desired outputs." A signature also fixes the names and types, so the function drops into your code without changes.
The same guide says GPT models need explicit, precise instructions, while reasoning models do better with high-level goals. So explicit contracts help most on a faster setting. OpenAI's Model selection page lists Luna at Low effort for well-scoped problem-solving, and its September notes suggest Luna High for focused, repeatable coding and Sol Medium for everyday and complex coding.
Your examples are also ready-made tests, which means you can check the result in seconds.
Example
You paste def slugify(title: str, max_len: int = 60) -> str: with these examples: "Hello, World!" becomes "hello-world", "Crème brûlée" becomes "creme-brulee", an empty string raises ValueError, and a 200-character title gets cut at a word boundary. Accented letters and word-boundary cuts are the decisions a one-line request would leave to chance. Now they're settled before any code appears.
Going further
Reverse the process in Codex. Write the failing test yourself, then give it a clear boundary: "Make this test pass. Don't edit the test." OpenAI's Prompting guide says a useful Codex prompt names the desired behavior, points to the relevant code or reproduction steps, keeps your constraints, and says how to verify the change. In the composer, @ inserts file paths from the workspace, so you can point to the test file exactly. Also, make your examples varied, not similar. Three happy-path inputs teach far less than one normal, one edge and one invalid case.
Common mistakes
- Giving only happy-path examples. The code will look right until the first empty string.
- Saying "or similar" about types or names. Paste the real signature, because guessed names won't compile against your code.
- Skipping the runtime. A function using a newer language feature fails on your older version, so state the version.
- Letting it pull in a new dependency for a five-line job. Say which libraries are allowed.
Sources
- OpenAI API docs: Prompt engineering
- ChatGPT Learn: Model selection
- ChatGPT Learn: What's new
- ChatGPT Learn: Prompting
- ChatGPT Learn: Codex IDE extension
Checked 6 November 2026. Products change often, so confirm details with the vendor.