DEV Community

Emmanuel Buah-Kwofie
Emmanuel Buah-Kwofie

Posted on

Flask Nova 0.2.x: Async Tasks, Pydantic Field Validation & OpenAPI 3.0 for Flask

Flask remains one of the most flexible WSGI frameworks in Python. However, building enterprise-grade APIs often requires manual integration of OpenAPI spec generators, task offloading mechanisms, and strict parameter validation.
The v0.2.x release of Flask Nova addresses these gaps directly.

Key Features in Flask Nova 0.2.x:

  • Async Task Offloading: Run blocking I/O with to_thread() and CPU tasks with to_process().
  • Pydantic Field Support: Enforce ge, le, min_length, and regex on path/query parameters.
  • OpenAPI 3.0 Enhancements: Auto-group Blueprints and add status_code & externalDocs to route decorators.
  • Observability: Native RFC 7807 problem details, ANSI-colored JSON logs, and trace_id injection.

Asynchronous Task Offloading in Flask: to_thread and to_process

Running asynchronous code inside Flask requires careful handling of blocking operations. Flask Nova introduces two task offloaders:

from flask_nova import to_thread, to_process

@app.get("/analytics")
async def get_analytics():
    # Run blocking database I/O in a worker thread
    data = await to_thread(fetch_heavy_db_records)

    # Run CPU-bound processing in a separate process pool
    processed = await to_process(compute_complex_metrics, data)

    return {"status": "success", "data": processed}
Enter fullscreen mode Exit fullscreen mode

Pydantic Field Validation for Path and Query Parameters

In v0.2.x, path and query parameters accept Pydantic Field definitions for granular type constraints:

from pydantic import Field
from flask_nova import FlaskNova

app = FlaskNova()

@app.get("/users/<int:user_id>")
def get_user(
    user_id: int = Field(..., ge=1, description="Unique positive user ID"),
    page: int = Field(1, ge=1, le=100, description="Page number"),
    search: str | None = Field(None, min_length=3, max_length=50)
):
    return {"user_id": user_id, "page": page, "search": search}
Enter fullscreen mode Exit fullscreen mode

Automatic OpenAPI 3.0 Docs and Route Metadata

  • Blueprint Tagging: Blueprint names automatically append to OpenAPI tags for organized documentation.
  • Route Metadata: Decorators accept status_code, additionalOperations, externalDocs, servers, and deprecated.
  • Explicit File Requests: Updated File binder with multi-file support and explicit content_type checks.
from flask_nova import File

@app.post("/upload",
    status_code=201,
    externalDocs={"description": "Upload specs", "url": "https://docs.example.com/upload"}
)
def upload_file(
    avatar: File = File(name="avatar", content_type="image/png")
):
    avatar.save(f"/uploads/{avatar.filename}")
    return {"message": "File uploaded successfully"}
Enter fullscreen mode Exit fullscreen mode

Observability & Error Handling

  • RFC 7807 Logging: Set ANSI_COLOR_JSON_LOG: bool to switch Flask's logger to colorful JSON formatted logs.
  • Traceability: All HTTPException instances inject traceparent headers and trace_id by default into response contexts.
  • Template Serialization: Standard render_template calls support response serialization out of the box.

Installation

pip install --upgrade flask-nova
Enter fullscreen mode Exit fullscreen mode

Check out the full CHANGELOG.md on GitHub!

Top comments (0)