FastOpenAPI: Generate OpenAPI Schemas for Any Python Framework

Developing APIs is a crucial part of modern software projects, and OpenAPI is the de facto standard for documenting APIs. While FastAPI seamlessly integrates OpenAPI schema generation and documentation, what if you prefer using frameworks like Flask, Falcon, Sanic, or others? FastOpenAPI addresses exactly this need.

What is FastOpenAPI?

FastOpenAPI is a Python library inspired by FastAPI and built upon Pydantic v2, designed to simplify the generation and integration of OpenAPI schemas with virtually any popular Python framework. Importantly, FastOpenAPI uses “proxy routing”: you write routes in a FastAPI-like style, and FastOpenAPI automatically registers them with your chosen framework.

Key Features:

  • Support for Falcon, Flask, Quart, Sanic, Starlette, Tornado.
  • Proxy routing with automatic registration.
  • Convenient FastAPI-style API.
  • Automatic documentation generation (Swagger UI, ReDoc).
  • Strong data validation and typing via Pydantic v2.

How it Works

FastOpenAPI employs a system of adapters (routers), each integrating with a specific framework:

  • BaseRouter: Contains shared logic for schema and documentation generation.
  • Specialized routers (FlaskRouter, SanicRouter, etc.) extend BaseRouter.

Minimal Flask API Example:

from flask import Flask
from pydantic import BaseModel

from fastopenapi.routers import FlaskRouter

app = Flask(__name__)
router = FlaskRouter(app)

class UserResponse(BaseModel):
    username: str

@router.get("/user", tags=["User"], response_model=UserResponse)
def get_user(username: str):
    return UserResponse(username=username)

if __name__ == "__main__":
    app.run(port=8000)

Enter fullscreen mode Exit fullscreen mode

Go to http://localhost:8000/docs to see swagger for example.

You can try it:

Router Composition

FastOpenAPI supports convenient API structuring through router composition, similar to APIRouter in FastAPI. For example, creating versioned APIs:

from flask import Flask
from pydantic import BaseModel

from fastopenapi.routers import FlaskRouter


app = Flask(__name__)
main_router = FlaskRouter(app)

api_v1 = FlaskRouter()
api_v2 = FlaskRouter()

class UserResponse(BaseModel):
    username: str
    version: str

class UserResponseV2(BaseModel):
    login: str
    version: str

@api_v1.get("/items", tags=["Users", "v1"], response_model=UserResponse)
def items_v1(name: str):
    return UserResponse(username=name, version="1.0")

@api_v2.get("/items", tags=["Users", "v2"], response_model=UserResponseV2)
def items_v2(name):
    return UserResponseV2(login=name, version="2.0")

main_router.include_router(api_v1, prefix="/api/v1")
main_router.include_router(api_v2, prefix="/api/v2")

if __name__ == "__main__":
    app.run(port=8000)

Enter fullscreen mode Exit fullscreen mode

Go to http://localhost:8000/docs:

图片[1]-FastOpenAPI: Generate OpenAPI Schemas for Any Python Framework - 拾光赋-拾光赋
If you prefer ReDoc, you can go to http://localhost:8000/redoc:

Generated Documentation

Once the server is running, documentation is automatically available:

Performance Considerations

The project’s repository includes basic benchmarks showing that FastOpenAPI introduces only a modest overhead (approximately 2-5%), which is a small price to pay for robust data validation and automatic documentation.

Who Should Use FastOpenAPI?

  • Developers wanting OpenAPI support without migrating from their preferred framework.
  • Teams using multiple frameworks seeking standardized API documentation.

Installation and Usage

# Choose installation based on your preferred framework
pip install fastopenapi[falcon]
pip install fastopenapi[flask]
pip install fastopenapi[sanic]
pip install fastopenapi[starlette]
pip install fastopenapi[tornado]

Conclusion

FastOpenAPI is an open-source library that simplifies the addition of OpenAPI specifications to existing Python projects across various frameworks, offering a balance of convenience, flexibility, and performance.

Project Repository:

mr-fatalyst / fastopenapi

FastOpenAPI is a library for generating and integrating OpenAPI schemas using Pydantic v2 and various frameworks (Falcon, Flask, Sanic, Starlette, Tornado).

FastOpenAPI is a library for generating and integrating OpenAPI schemas using Pydantic and various frameworks

This project was inspired by FastAPI and aims to provide a similar developer-friendly experience


Installation

Install only FastOpenAPI:

pip install fastopenapi

Enter fullscreen mode Exit fullscreen mode

Install FastOpenAPI with a specific framework:

pip install fastopenapi[falcon]

Enter fullscreen mode Exit fullscreen mode

pip install fastopenapi[flask]

Enter fullscreen mode Exit fullscreen mode

pip install fastopenapi[sanic]

Enter fullscreen mode Exit fullscreen mode

pip install fastopenapi[starlette]

Enter fullscreen mode Exit fullscreen mode

pip install fastopenapi[tornado]

Enter fullscreen mode Exit fullscreen mode


️ Quick Start

Step 1. Create an application

  • Create the main.py file
  • Copy the code from an example
  • For some examples uvicorn is required (pip install uvicorn)

Examples:

  • Click to expand the Falcon Example

    import falcon.asgi
    import uvicorn
    from pydantic import BaseModel
    from fastopenapi.routers import FalconRouter
    
    app = falcon.asgi.App()
    router = FalconRouter(app=app)
    
    
    class HelloResponse(BaseModel):
        message: str
    
    
    @router.get("/hello", tags=["Hello"], status_code=200, 

    … Enter fullscreen mode Exit fullscreen mode

View on GitHub

The project is still evolving, and I’d love any feedback or testing from the community!

原文链接:FastOpenAPI: Generate OpenAPI Schemas for Any Python Framework

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容