Registry / web-framework / flask-pydantic-spec

flask-pydantic-spec

JSON →
library0.8.7pypypiunverified

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-spec
INSTALL
IMPORT
SIG · FLASK-PYDANTIC-SPE
F
flask-pydantic-spec
web-frameworkpythonv0.8.7
Install
3.2s avg
Import
—
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.8.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 28.1MB
glibc
py 3.10–3.910 runs
installs and imports cleanly · install 3.2s · import 0.000s · 28MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

FlaskPydanticSpec
✓ from flask_pydantic_spec import FlaskPydanticSpec
✗ from flask_pydantic_spec import FlaskPydanticSpec

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.

from flask import Flask from flask_pydantic_spec import FlaskPydanticSpec, Request, Response, Query from pydantic import BaseModel, Field # Define Pydantic models for request and response class UserRequest(Request): user_id: int = Field(Query, description="ID of the user") class UserResponse(BaseModel): id: int name: str email: str app = Flask(__name__) spec = FlaskPydanticSpec('flask-pydantic-spec', app=app) spec.register(app) @app.route('/user/<int:user_id>') @spec.validate( query=UserRequest, resp=Response(HTTP_200=UserResponse) ) def get_user(user_id): # Access validated query parameters via spec.query # Note: user_id from path param will override if name conflict if spec.query.user_id != user_id: print(f"Warning: Path ID {user_id} and Query ID {spec.query.user_id} mismatch.") # In a real app, fetch user from DB user_data = {"id": user_id, "name": f"User_{user_id}", "email": f"user{user_id}@example.com"} return UserResponse(**user_data).model_dump() # Example of OpenAPI documentation endpoint (auto-generated) # Accessible at /swagger or /redoc by default if __name__ == '__main__': app.run(debug=True)
Debug
Known issues
breakingPydantic v1.x support was removed in `flask-pydantic-spec` v0.7.0, making it strictly Pydantic v2.x compatible. If you relied on Pydantic v1.x models, your application would break upon upgrading to v0.7.x.
fix
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.
affects: 0.7.x
gotchaBlueprint support was added in `flask-pydantic-spec` v0.8.6. Prior versions do not natively support attaching `FlaskPydanticSpec` instances directly to Flask Blueprints.
fix
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.
affects: <0.8.6
gotchaThe order of decorators matters. Flask's `@app.route()` or `@blueprint.route()` decorator should typically be placed *before* `flask-pydantic-spec`'s `@spec.validate()` decorator.
fix
Ensure `@app.route()` or `@blueprint.route()` is the first decorator applied to your route function, followed by `@spec.validate()`.
affects: All versions
deprecatedOpenAPI Specification 3.1.0 changed the schema keyword from 'definitions' to '$defs'. While `flask-pydantic-spec` v0.8.2 fixed this internally for its generated specs, older versions (specifically for 'v1 specs') might generate schemas that are not fully compliant with newer OpenAPI 3.1.0 tools or Swagger UI versions.
fix
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.
affects: <0.8.2
Upgrade
Version history
0.8.7latest on PyPI · released Nov 25, 2025
Audit
Dependencies
FlaskrequiredCore web framework integration.
PydanticrequiredRequired for defining request/response schemas and validation logic. Supports both Pydantic v1 and v2 since v0.8.0.
Agent activity
14 hits · last 30 days
node
14
Resources