OpenAI Codex Prompts

Expert prompts for AI-powered code generation — Python, JavaScript, SQL, APIs, debugging, refactoring, and test writing.

PythonJavaScriptSQLDebugging

Python & Data Science

Python

Data processing pipeline

Write a Python function that:
- Reads a CSV file at [filepath] using pandas
- Cleans the data: drops rows with >50% null values, fills remaining nulls with column median for numerics and "Unknown" for strings
- Normalises numeric columns to 0-1 range (min-max scaling)
- Returns a cleaned DataFrame and a summary dict with: original row count, dropped rows, columns normalised
Include type hints, docstring, and handle FileNotFoundError gracefully.
Python

Async API client with retry logic

Write an async Python function that fetches data from [API endpoint] with:
- httpx async client
- Retry logic: 3 attempts with exponential backoff (1s, 2s, 4s)
- Timeout of 10 seconds per request
- Rate limiting: max 10 requests per second
- Returns parsed JSON response or raises a custom APIError with status code and message
Include type hints and a simple usage example.
Python

Class with context manager and logging

Create a Python class DatabaseConnection that:
- Connects to PostgreSQL using psycopg2 (connection params from environment variables)
- Implements __enter__ and __exit__ for context manager usage
- Logs all queries at DEBUG level with execution time
- Logs errors at ERROR level with full stack trace
- Automatically rolls back on exception, commits on clean exit
Include an example showing a SELECT query and an INSERT with error handling.
Python

CLI tool with argparse

Write a Python CLI tool that processes [task] with these arguments:
- Required: --input (file path)
- Optional: --output (file path, defaults to stdout)
- Optional: --format (choices: json, csv, table; default: table)
- Optional: --verbose flag for detailed logging
- --version flag showing "1.0.0"
Use argparse with helpful descriptions. Include input validation with clear error messages. Add a shebang line and make it executable.

JavaScript & TypeScript

TypeScript

Type-safe API service class

Create a TypeScript service class for the [API name] REST API:
- Constructor accepts baseURL and apiKey
- Generic fetch method: fetchData<T>(endpoint: string, options?: RequestInit): Promise<T>
- Specific methods for: getUser(id: string), listItems(params: QueryParams), createItem(data: CreateItemDTO), updateItem(id: string, data: UpdateItemDTO)
- All methods return typed responses using interface definitions you write
- Error handling: throws typed APIError class with status, message, and response body
- Include JSDoc comments for each method
React

Custom hook with caching

Write a React custom hook useData<T> that:
- Fetches data from a URL parameter
- Caches results in a Map (keyed by URL) to avoid re-fetching
- Returns: { data: T | null, loading: boolean, error: Error | null, refetch: () => void }
- Accepts optional refreshInterval (ms) for automatic re-fetching
- Cleans up intervals and cancels in-flight requests on unmount
- TypeScript with generics
Include a usage example with a typed interface.
Node.js

Express middleware for auth + rate limiting

Write Express.js middleware that:
1. authenticateJWT: verifies Bearer token from Authorization header, attaches decoded payload to req.user, returns 401 for invalid/expired tokens
2. rateLimit: allows 100 requests per IP per 15 minutes using an in-memory Map, returns 429 with Retry-After header when exceeded
3. requestLogger: logs method, path, status code, and duration in ms

Show how to chain them on a router: router.use(rateLimit, authenticateJWT, requestLogger)
TypeScript. No external auth libraries — use jsonwebtoken only.
TypeScript

Generic data transformation pipeline

Write a TypeScript pipeline utility that chains transformation functions:
- type TransformFn<T, U> = (input: T) => U
- function pipe<A, B, C, D>(...fns): (input: A) => D (overloads for 2-4 stages)
- function pipeAsync<A, B>(...fns): (input: A) => Promise<B> for async transforms
- Includes error boundary: if any step throws, wrap in PipelineError with step index and original error
Show an example: parse JSON → validate schema → transform shape → format output

Debugging & Code Review

Debugging

Root cause analysis from error

I'm getting this error in my [language/framework] application:
Error: [paste full error message and stack trace]

Relevant code:
[paste the function/module where the error occurs]

Context: [what were you trying to do? what did you expect to happen?]

Please:
1. Identify the root cause (not just the symptom)
2. Explain why this error occurs conceptually
3. Provide a specific fix with the corrected code
4. Suggest a test that would catch this regression in future
5. Note any related issues in the code worth fixing
Code Review

Security-focused code review

Review this code for security vulnerabilities:
[paste code]

Focus on:
1. Injection risks (SQL, command, LDAP) — are user inputs sanitised?
2. Authentication/authorisation — are endpoints properly protected?
3. Sensitive data handling — credentials, PII, tokens
4. Dependency risks — are there known-vulnerable packages?
5. Error messages — do they leak implementation details?

Rate each issue Critical/High/Medium/Low. For Critical and High issues, provide a specific fix.
Refactoring

Refactor for readability and performance

Refactor this code to improve readability and performance:
[paste code]

Priorities:
1. Extract magic numbers and strings into named constants
2. Break functions longer than 20 lines into smaller, single-purpose functions
3. Replace nested if-else with early returns or strategy pattern where appropriate
4. Identify any O(n²) or worse complexity that could be optimised
5. Improve variable/function names to be self-documenting

Show the refactored version with a brief explanation of each change made.
Documentation

Generate code documentation

Generate comprehensive documentation for this code:
[paste code]

Include:
- Module/file overview: what does this code do and why does it exist?
- For each function/method: purpose, parameters (name, type, description), return value, side effects, exceptions thrown
- Usage examples for the main public API (2-3 examples)
- Any non-obvious design decisions and why they were made
Format as [JSDoc / Python docstrings / Markdown] appropriate for this language.

Test Writing

Unit Tests

Comprehensive unit test suite

Write unit tests for this function using [pytest / Jest / RSpec]:
[paste the function]

Cover:
1. Happy path: expected inputs produce expected outputs
2. Edge cases: empty input, null/undefined, boundary values, max/min
3. Error cases: invalid types, missing required fields, values out of range
4. Side effects: verify any database calls, API calls, or file writes are made correctly

Use descriptive test names in the format "it should [expected behaviour] when [condition]".
Mock any external dependencies. Aim for >90% branch coverage.
Integration Tests

API endpoint integration tests

Write integration tests for these API endpoints:
[list endpoints: method, path, expected behaviour]

For each endpoint test:
- 200/201 success case with valid payload
- 400 validation error cases (missing fields, wrong types)
- 401 unauthorised (no token / invalid token)
- 404 not found (for endpoints with ID params)
- 500 handling: mock the database to throw and verify error response format

Use [Supertest / FastAPI TestClient / RSpec request spec]. Include test database setup and teardown.
Mocking

Mock strategy for external dependencies

I need to test [module] which depends on [list dependencies: database, external API, file system, etc.].
Write the mocking strategy:
1. For each dependency, show how to mock it in [testing framework]
2. Identify what the mock should verify vs. just stub
3. Show how to test both success and failure scenarios for each dependency
4. Explain how to avoid over-mocking (testing mocks instead of real behaviour)
5. Provide the test setup/teardown code for managing mock lifecycle
TDD

TDD workflow for a new feature

I want to build: [describe the feature]
Walk me through building it test-first (TDD):
1. Write the failing test for the simplest case
2. Write the minimum code to make it pass
3. Refactor without breaking the test
4. Write the next failing test for the next requirement
5. Repeat

Continue this cycle for: [list 3-4 key requirements of the feature]
Use [language] with [testing framework]. Keep each step minimal — this is the red-green-refactor loop.

SQL & Data Queries

SQL

Complex analytical query

Write a SQL query for [database: PostgreSQL / MySQL / BigQuery] that:
- Calculates [metric] from table [table name] with columns [list relevant columns]
- Groups by [dimensions]
- Filters to [date range / conditions]
- Ranks results using window functions
- Handles nulls appropriately
- Is optimised for large datasets (hint on index usage if relevant)

Include a brief explanation of each CTE or complex clause.
SQL

Database schema with indexes

Design a PostgreSQL schema for [use case — e.g., "a SaaS subscription billing system"].
Requirements:
- [List 3-5 entities and their key attributes]
- [List key relationships: one-to-many, many-to-many]
- [Performance requirement: e.g., "fast lookup by user_id and date range"]

Include:
- CREATE TABLE statements with appropriate data types and constraints
- Foreign key relationships
- Indexes for expected query patterns
- A brief explanation of design decisions (why UUID vs serial, why this index)
SQL

Query optimisation and explain plan

This query is running slowly (>5 seconds on ~2M rows):
[paste slow query]

Table structure: [paste CREATE TABLE or describe columns and approximate row counts]
Existing indexes: [list them]

Please:
1. Identify the likely performance bottleneck(s)
2. Suggest specific index additions or changes
3. Rewrite the query if there's a more efficient approach
4. Explain what to look for in the EXPLAIN ANALYZE output to confirm the fix works
SQL

Data migration script

Write a SQL migration script to [describe the schema change — e.g., "split the full_name column into first_name and last_name", or "add a status enum column with a default value"].

The migration should:
- Be idempotent (safe to run twice)
- Handle existing data (backfill the new column/table)
- Include a rollback script
- Add a comment explaining what the migration does and why
- Work for [PostgreSQL / MySQL] version [X]

API Integration & Architecture

REST API

REST API client with auth and pagination

Build a [language] REST API client for [service] that handles:
- Authentication: [API key header / OAuth2 bearer token / basic auth]
- Pagination: automatically fetches all pages when results exceed one page
- Rate limiting: respects the [X] requests/minute limit using a token bucket approach
- Retry: 3 retries with exponential backoff for 429 and 5xx responses
- Response parsing: typed response models for each endpoint

Endpoints needed:
- GET /[resource] (list with filters)
- GET /[resource]/{id} (get single)
- POST /[resource] (create)
- PATCH /[resource]/{id} (update)

Include type definitions/interfaces and a usage example.
WebSocket

WebSocket client with reconnection

Write a [language] WebSocket client that:
- Connects to [endpoint] with [authentication method]
- Automatically reconnects on disconnect with exponential backoff (max 5 retries)
- Maintains a message queue and replays unacknowledged messages after reconnect
- Dispatches incoming messages by type to registered handlers
- Exposes: connect(), disconnect(), send(type, payload), on(type, handler), off(type, handler)
- Logs connection lifecycle events (connected, disconnected, reconnecting, error) at appropriate log levels
Include TypeScript types if using JS/TS.
Microservices

Service-to-service communication pattern

Design the communication pattern between these microservices: [list your services]
Requirements:
- Synchronous for [list operations that need immediate response]
- Asynchronous for [list operations that can be eventual]
- Service A needs to know when Service B completes [specific operation]

Provide:
1. The recommended pattern for each communication type (REST, gRPC, message queue, event bus)
2. The message/event schema for async communications
3. How to handle partial failures and rollbacks
4. The retry and circuit breaker strategy
5. How to trace a request across all services for debugging
Database

Repository pattern implementation

Implement the Repository pattern for [entity — e.g., User, Order] in [language/framework]:
- IUserRepository interface with: findById, findByEmail, findAll(filters, pagination), create, update, delete
- PostgreSQLUserRepository implementing the interface using [ORM or raw SQL]
- InMemoryUserRepository for testing
- Factory function that returns the right implementation based on environment

The implementation should:
- Use transactions where data integrity requires it
- Log all queries in development
- Never expose raw DB errors to callers (wrap in domain errors)
- Handle optimistic locking for concurrent updates to the same record

Frequently Asked Questions

Quick Reference: Code Generation Best Practices

Always include in your prompt:

  • Programming language and version
  • Framework or library context
  • Input/output types and examples
  • Edge cases to handle explicitly
  • Error handling requirements
  • Performance or style constraints

Review generated code for:

  • Security vulnerabilities (injection, auth)
  • Missing error handling paths
  • Hardcoded credentials or values
  • Off-by-one errors and boundary cases
  • Compatibility with your language version
  • Test coverage of edge cases

Language and Framework Support

OpenAI Codex and GPT-4 code models support all major programming languages. Prompt quality matters more than language choice — the more context you provide, the better the output.

Python
JavaScript
TypeScript
Go
Rust
Java
C#
C++
Ruby
PHP
Swift
Kotlin
SQL
Bash
R
Scala

For best results with any language: specify the version (e.g. Python 3.11, Node 20, Go 1.22), mention the relevant framework, and paste real code rather than describing it abstractly.

Strongest for

Python, JavaScript, TypeScript — largest training data, most reliable output for these languages.

Works well for

Go, Rust, Java, C# — good results with clear context, but verify output carefully for idiomatic patterns.

Use with care for

Domain-specific languages, niche frameworks, or proprietary codebases — always review outputs against your own standards.

Security reminder: Never paste API keys, passwords, connection strings, or sensitive credentials into code generation prompts. Use placeholder names like YOUR_API_KEY or environment variable references, and replace them in your actual codebase. Generated code should always be reviewed before committing to production.
On hallucinated packages: Code models occasionally reference libraries or methods that do not exist. Always verify that any package name, version, or API method in generated code actually exists in the official documentation before adding it to your project.
Context window tip: For large files, paste only the relevant function or class, not the entire codebase. If you need the model to understand how multiple parts interact, describe the relationship in plain language rather than pasting everything. Targeted context produces more accurate, focused output.
Iterative refinement: Treat the first generated output as a draft, not a finished product. Follow up with: "Now add error handling for X", "Refactor this part to be more readable", or "Add a test for the edge case where the input is empty". Iteration almost always produces better results than a single long prompt.

Codex Prompt Patterns by Task Type

Generate a function
"Write a [language] function that [does X]. Accept [inputs] and return [output]. Handle [edge case] by [behaviour]."
Refactor code
"Refactor the following code to [goal — e.g. reduce complexity, use async/await]. Keep the same behaviour: [paste code]"
Add error handling
"Add error handling to this function. Catch [errors], log them with context, and return [fallback]. Do not change the happy path: [code]"
Write tests
"Write unit tests using [framework]. Cover: the happy path, empty inputs, null values, boundary conditions, and error states: [code]"
Explain code
"Explain what this code does in plain English. Then describe what happens if [edge case]. Highlight any potential bugs: [code]"
Convert languages
"Translate this [source language] code to [target language]. Preserve all logic. Use idiomatic [target language] patterns: [code]"
Write a SQL query
"Write a [dialect] query to [goal]. Tables: [schema]. Filter by [conditions]. Return [columns]. Optimise for [concern]."
Debug an error
"I'm getting this error: [message]. Here is the code: [code]. Here is the context: [what I was doing]. What is the cause and fix?"
Document code
"Add JSDoc / docstrings to every function in this file. Include: what it does, each parameter's type and purpose, what it returns, and any thrown exceptions: [code]"
Review for security
"Review this code for security vulnerabilities. Focus on: input validation, SQL injection risks, authentication logic, and any data exposed to the client: [code]"
Optimise performance
"This function is slow for large inputs. Profile it and suggest optimisations. Preserve correctness. Explain the time complexity before and after: [code]"
Migrate framework
"Migrate this [old framework] component to [new framework]. Maintain identical behaviour. Flag anything that has no direct equivalent and suggest alternatives: [code]"
Generate boilerplate
"Generate a [language] project structure for a [type of app — e.g. REST API, CLI tool]. Include: folder layout, dependency list, config files, and a working hello-world entry point."
Write a regex
"Write a regex in [language] that matches [description]. Provide: the regex, an explanation of each part, 3 examples that should match, and 3 that should not."
Code review prep
"Review this PR diff and identify: potential bugs, missing edge cases, performance concerns, style issues, and anything that should have a test: [paste diff]"
Generate mock data
"Generate 10 realistic mock records for a [entity] with these fields: [list fields and types]. Use realistic values, not Lorem ipsum. Output as JSON."
Add logging
"Add structured logging to this function using [library — e.g. pino, winston, zap]. Log: entry, exit, errors, and key decision points. Do not log sensitive values: [code]"
Design a schema
"Design a [SQL/MongoDB/Firestore] schema for a [type of application]. Include: entities, relationships, indexes for likely queries, and an explanation of your key design decisions."
Write a CLI tool
"Write a [language] CLI tool that [does X]. Include: argument parsing with --help text, sensible defaults, input validation, and human-readable error messages on failure."
Add types
"Add TypeScript types to this JavaScript file. Infer types from usage where possible. Use strict types — avoid any. Mark nullable fields explicitly: [code]"
Write a webhook handler
"Write a [language] webhook handler for [service — e.g. Stripe, GitHub]. Verify the signature, parse the payload, handle [event types], and return the correct response codes for success and failure."
Containerise an app
"Write a Dockerfile for this [language] application. Use a minimal base image, implement multi-stage builds to keep the final image small, and follow container security best practices."
Seed a database
"Write a database seed script in [language] that creates realistic test data for [describe schema]. Include variety in the data — different statuses, edge case values, and relationships between records."
Write a cron job
"Write a [language] cron job that [does X] on a [schedule — e.g. daily at midnight]. Include: idempotency so it is safe to run twice, logging, and alerting on failure."

Related Prompts