Expert prompts for AI-powered code generation — Python, JavaScript, SQL, APIs, debugging, refactoring, and test writing.
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.
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.
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.
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.
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
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.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.
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
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
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.
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.
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.
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.
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.
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 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.
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.
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)
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
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]
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 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.
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
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
Always include in your prompt:
Review generated code for:
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.
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.