Prompt patterns for code generation, review, and tests — include language, framework, requirements, and constraints.
Prompt Engineering for Code
# Code generation — be specific about all requirements
Language: Python 3.12
Framework: FastAPI
Task: Write a user authentication endpoint
Requirements:
- POST /auth/login
- Accept JSON: {email: string, password: string}
- Validate with Pydantic model
- Compare hash with bcrypt
- Return JWT on success (use python-jose)
- Return 401 with message on invalid credentials
- Include type hints on all parameters
- Add docstring
- Handle database connection errors with 500
Do NOT include: database model (already exists as User)
# Code review prompt
Review this code for:
1. Security vulnerabilities (SQL injection, XSS, auth issues)
2. Performance issues (N+1 queries, missing indexes)
3. Missing error handling
4. Python best practice violations
Format: Issue | Severity (High/Med/Low) | Line | Suggested Fix
# Test generation
Write pytest tests for the function below.
Include: happy path, edge cases (empty, None, boundary), error cases.
Mock all external dependencies.
Use pytest fixtures for repeated setup.