Skip to content

Python Workflow Recipes

Python backend patterns for AI coding assistants span building production backends with FastAPI and Django, adding typed Pydantic validation, SQLAlchemy 2.0 database access, Celery background tasks, and pytest test suites. Each recipe pairs a concrete prompt with the async-first code it produces, so Cursor, Claude Code, and Codex generate consistent, testable Python across routing, background work, and Docker deployment.

Python is the glue language of the industry — from web APIs to ML pipelines to automation scripts. These recipes produce typed, async-first Python code that uses modern patterns: Pydantic for validation, SQLAlchemy 2.0 for databases, and pytest for testing. No more untyped dictionaries passed between functions.

What You’ll Walk Away With From These Python Patterns

Section titled “What You’ll Walk Away With From These Python Patterns”
  • FastAPI and Django REST framework recipes with proper typing
  • Database integration with SQLAlchemy 2.0 async and Alembic migrations
  • Background task patterns with Celery and async queues
  • Testing recipes with pytest, fixtures, and mocking

Recipe 1: FastAPI Project with Dependency Injection

Section titled “Recipe 1: FastAPI Project with Dependency Injection”

Scenario: You need a new API service that is testable, typed, and follows Python best practices.

Expected output: Project structure, config, dependencies, middleware, exception handlers, and tests.


Recipe 2: Pydantic Schemas with Complex Validation

Section titled “Recipe 2: Pydantic Schemas with Complex Validation”

Scenario: Your API accepts nested JSON with cross-field validation rules that Pydantic v2 can handle but you keep writing raw dictionaries.

Expected output: Pydantic schemas with validators, response models, and comprehensive validation tests.


Recipe 3: SQLAlchemy 2.0 Async Models and Queries

Section titled “Recipe 3: SQLAlchemy 2.0 Async Models and Queries”

Scenario: Your database code uses raw SQL strings with no type safety, no migration support, and synchronous calls blocking the event loop.

Expected output: Base model, 4 domain models, async database setup, repository pattern, Alembic config, and tests.


Scenario: Your API generates PDF reports synchronously, taking 30 seconds per request. Users are frustrated.

Expected output: Celery config, 3 task definitions, status API, Beat schedule, and eager-mode tests.


Recipe 5: Comprehensive API Testing with pytest

Section titled “Recipe 5: Comprehensive API Testing with pytest”

Scenario: Your FastAPI project has zero tests. You need to test routes, services, and database interactions.

Expected output: conftest with fixtures, API tests, service tests, repository tests, and coverage config.


Recipe 6: Django REST Framework CRUD with Permissions

Section titled “Recipe 6: Django REST Framework CRUD with Permissions”

Scenario: You are building a multi-tenant SaaS with Django and need role-based access control on every endpoint.

Expected output: Models, serializers, viewsets, permissions, filters, and permission tests.


Recipe 7: Async HTTP Client with Retry and Circuit Breaker

Section titled “Recipe 7: Async HTTP Client with Retry and Circuit Breaker”

Scenario: Your service calls three external APIs and cascading failures bring everything down.

Expected output: Base client with retry/circuit breaker, 3 typed API clients, health integration, and tests.


Scenario: Your team runs manual database scripts and deployment tasks. You need a typed CLI tool.

Expected output: CLI app with 4 command groups, formatted output, dry-run support, and CliRunner tests.


Scenario: You need a real-time chat feature in your FastAPI application.

Expected output: WebSocket endpoint, connection manager, Redis pub/sub, message persistence, and tests.


Recipe 10: Data Pipeline with Async Generators

Section titled “Recipe 10: Data Pipeline with Async Generators”

Scenario: You need to process a 10 GB CSV file without loading it into memory.

Expected output: Reader, transformer, loader modules, pipeline composition, checkpointing, and tests.


Recipe 11: Configuration Management with Pydantic Settings

Section titled “Recipe 11: Configuration Management with Pydantic Settings”

Scenario: Your app reads environment variables with os.getenv scattered everywhere, no validation, and defaults that differ between files.

Expected output: Settings class, cached getter, test overrides, startup validation, and tests.


Recipe 12: Dockerized Development and Production Setup

Section titled “Recipe 12: Dockerized Development and Production Setup”

Scenario: “It works on my machine” is your team’s most-used phrase. Everyone has different Python versions and system dependencies.

Expected output: Multi-stage Dockerfile, compose files, .dockerignore, Makefile, and build tests.


When These Python Patterns Break in Production

Section titled “When These Python Patterns Break in Production”

Where to Go Next From These Python Patterns

Section titled “Where to Go Next From These Python Patterns”