---
name: python-rest-api-refactoring-tool
description: "Turns a creaking Flask, Django, or FastAPI service into a sprint-sized refactoring plan: numbered steps with the files they touch, code samples, a test strategy, and an effort estimate in person-days. Use when a Flask, Django, or FastAPI REST API has grown messy or slow and needs a prioritised refactoring plan the team can run as sprint tickets. Do not use for designing a new API from scratch or reviewing a single endpoint's code. Related topics: coding, python."
---

# Python REST API Refactoring Tool

Turns a creaking Flask, Django, or FastAPI service into a sprint-sized refactoring plan: numbered steps with the files they touch, code samples, a test strategy, and an effort estimate in person-days.

## When to use

Use this skill when a Flask, Django, or FastAPI REST API has grown messy or slow and needs a prioritised refactoring plan the team can run as sprint tickets. Do not use it for designing a new API from scratch or reviewing a single endpoint's code.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Map each pain point and bottleneck to a root cause in the codebase, so every planned step traces back to a stated problem.
3. Order the refactoring steps so each one leaves the service deployable with tests passing, and mark the files each step touches.
4. Choose the patterns that fit this codebase (dependency injection, schema validation, pagination, async) and justify each in one line.
5. Write before-and-after code samples for the two or three highest-impact changes, runnable as shown.
6. Lay out the testing approach that reaches the coverage target, naming the modules to cover first.
7. Estimate effort per step in person-days and fill the before-and-after metrics table with the figures the plan expects to move.

## Inputs

- **Current framework** (choose one: Flask, Django, FastAPI, Falcon)
- **Main pain points** (longer free text)
- **Target Python version** (choose one: 3.9, 3.10, 3.11, 3.12, 3.13)
- **Test coverage increase wanted (%)** (number from 0 to 100)
- **Known performance bottlenecks** (longer free text)
- **Refactor depth** (choose one: Minor cleanup, Moderate restructuring, Full redesign)
- **Deployment environment** (choose one: Docker, Kubernetes, Bare metal, Serverless)
- **Style guide** (choose one: PEP8, Google, Black)

## Output

A markdown document with Overview, numbered Refactoring Steps (rationale and files), Code Samples, Testing Recommendations, Estimated Effort per step in person-days, a before-and-after metrics table, and a list of new libraries with reasons.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Python REST API Refactoring Plan

Plan the refactor of my Python REST API.

- Current framework: {{Current framework}}
- Main pain points: {{Main pain points}}
- Target Python version: {{Target Python version}}
- Test coverage increase wanted (%): {{Test coverage increase wanted (%)}}
- Known performance bottlenecks: {{Known performance bottlenecks}}
- Refactor depth: {{Refactor depth}}
- Deployment environment: {{Deployment environment}}
- Style guide: {{Style guide}}

Start from the pain points and bottlenecks, not from a generic checklist: every step must trace back to one of them. Order the steps so each one leaves the service deployable with tests green, so the work can ship in slices. Apply the patterns that earn their place in this codebase: dependency injection for the pieces that are hard to test, schema validation at the boundary, pagination on any list endpoint, async handling only where the framework and the I/O profile justify it. Fold security into the same steps: input sanitisation, the authentication flow, secrets handling. Keep the depth the user chose; a minor cleanup does not become a rewrite.

Return a markdown document with these sections in order: Overview, Refactoring Steps (numbered, each with a rationale and the affected files), Code Samples (before and after for the two or three highest-impact changes), Testing Recommendations (how to reach the coverage target), Estimated Effort in person-days per step, a before-and-after metrics table, and any new third-party libraries with a one-line reason each.

## Pitfalls

- Turning a minor cleanup into a rewrite because the plan follows a generic best-practices list instead of the stated pain points.
- Adding async handlers to a service whose database driver is synchronous, which blocks the event loop and makes latency worse.
- Ordering steps so the service cannot be deployed between them, which forces one risky big-bang release.
