📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Prompt Engineering Prompts for Code Generation

Prompts for Code Generation

5 min read Quiz at the end
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.
Topic Quiz · 1 questions

Test your understanding before moving on

1. Why should you specify language and framework when prompting for code?
💡 Specifying Python 3.12 + FastAPI gets precise, version-appropriate code instead of generic examples.