Flask Pydantic Spec is a Python library that simplifies the generation of OpenAPI documentation and the validation of Flask request/response payloads using Pydantic models and Python annotations. It provides decorators to integrate Pydantic validation directly into Flask route functions, enhancing API reliability and maintainability. The library is actively maintained, with frequent minor releases, and the current version is 0.8.7.
pip install flask-pydantic-specVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a Flask application with `flask-pydantic-spec` to validate incoming query parameters and serialize outgoing JSON responses. It defines Pydantic models for the request's query and the response body. The `@spec.validate` decorator applies the validation rules, and validated data is accessible via `spec.query` or `spec.body` within the route function.
Either downgrade `flask-pydantic-spec` to <0.7.0, upgrade your Pydantic models to v2.x syntax, or upgrade `flask-pydantic-spec` to v0.8.0 or newer which re-introduced support for Pydantic v1 models alongside v2.
Upgrade to `flask-pydantic-spec` v0.8.6 or newer to utilize blueprint integration. For older versions, you'd need to manually register routes or configure the spec object globally.
Ensure `@app.route()` or `@blueprint.route()` is the first decorator applied to your route function, followed by `@spec.validate()`.
Upgrade to `flask-pydantic-spec` v0.8.2 or newer to ensure correct OpenAPI 3.1.0 schema generation. Also, ensure your Swagger UI/Redoc versions are compatible with OpenAPI 3.1.0 if using that spec version.