refactor-fastapi_skill

This skill refactors FastAPI/Python code to cleaner, maintainable patterns using Pydantic v2, SOLID principles, and modular service design.

6

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill snakeo/claude-debug-and-refactor-skills-plugin --skill refactor-fastapi

  • SKILL.md17.4 KB

Overview

This skill refactors FastAPI/Python code to improve maintainability, readability, and adherence to best practices. It transforms working code into idiomatic FastAPI, applying Pydantic v2 patterns, SOLID principles, and clear async/sync boundaries. The result is skinny route handlers, testable services, and consistent project structure.

How this skill works

I inspect route handlers, Pydantic models, dependency usage, and I/O patterns to locate smells: business logic in routes, blocking calls inside async functions, duplicated code, and Pydantic v1 patterns. I then extract services, repositories, and dependencies; convert models to Pydantic v2 idioms; add precise type hints; and reorganize routers by domain. Changes aim to be incremental and safe for existing behavior.

When to use it

  • Routes contain business logic or long handlers (>25 lines).
  • Async endpoints call blocking libraries (time.sleep, blocking DB drivers).
  • Code uses Pydantic v1 patterns or mixed v1/v2 styles.
  • Project shows duplicated logic across handlers or modules.
  • Missing dependency injection, hard-to-test functions, or monolithic routers.

Best practices

  • Keep routes thin: validate input, call services, return responses.
  • Use async only with async I/O libraries; prefer def for blocking operations.
  • Adopt Pydantic v2: model_config, Annotated Fields, field_validator/model_validator.
  • Extract repositories and services; follow SRP and DI via Depends.
  • Organize code by domain (router, service, repository, schemas, dependencies).

Example use cases

  • Convert a route with embedded DB queries into a route that Depends on a repository and service.
  • Replace time.sleep in an async endpoint with await asyncio.sleep or make the handler sync.
  • Migrate models from Pydantic v1 Config class to v2 model_config and Annotated fields.
  • Introduce background tasks for fire-and-forget email sending after user creation.
  • Add a lifespan context to initialize and close database and redis connections.

FAQ

Refactors are designed to preserve behavior while improving structure. I prioritize minimal, testable changes and recommend running the existing test suite and adding targeted tests.

When should I keep a function sync instead of async?

Keep a function sync when it uses blocking libraries without async alternatives. FastAPI runs sync functions in a threadpool, avoiding event loop blocking.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
refactor-fastapi skill by snakeo/claude-debug-and-refactor-skills-plugin | VeilStrat