{"id":8997,"library":"flask-pydantic-spec","title":"Flask Pydantic Spec","description":"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.","status":"active","version":"0.8.7","language":"python","source_language":"en","source_url":"https://github.com/turner-townsend/flask-pydantic-spec","tags":["Flask","Pydantic","OpenAPI","Swagger","API validation","REST","Documentation"],"install":[{"cmd":"pip install flask-pydantic-spec","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core web framework integration.","package":"Flask","optional":false},{"reason":"Required for defining request/response schemas and validation logic. Supports both Pydantic v1 and v2 since v0.8.0.","package":"Pydantic","optional":false}],"imports":[{"wrong":"from flask_pydantic_spec import FlaskPydanticSpec","symbol":"FlaskPydanticSpec","correct":"from flask_pydantic_spec import FlaskPydanticSpec"}],"quickstart":{"code":"from flask import Flask\nfrom flask_pydantic_spec import FlaskPydanticSpec, Request, Response, Query\nfrom pydantic import BaseModel, Field\n\n# Define Pydantic models for request and response\nclass UserRequest(Request):\n    user_id: int = Field(Query, description=\"ID of the user\")\n\nclass UserResponse(BaseModel):\n    id: int\n    name: str\n    email: str\n\napp = Flask(__name__)\nspec = FlaskPydanticSpec('flask-pydantic-spec', app=app)\nspec.register(app)\n\n@app.route('/user/<int:user_id>')\n@spec.validate(\n    query=UserRequest,\n    resp=Response(HTTP_200=UserResponse)\n)\ndef get_user(user_id):\n    # Access validated query parameters via spec.query\n    # Note: user_id from path param will override if name conflict\n    if spec.query.user_id != user_id:\n        print(f\"Warning: Path ID {user_id} and Query ID {spec.query.user_id} mismatch.\")\n\n    # In a real app, fetch user from DB\n    user_data = {\"id\": user_id, \"name\": f\"User_{user_id}\", \"email\": f\"user{user_id}@example.com\"}\n    return UserResponse(**user_data).model_dump()\n\n# Example of OpenAPI documentation endpoint (auto-generated)\n# Accessible at /swagger or /redoc by default\n\nif __name__ == '__main__':\n    app.run(debug=True)","lang":"python","description":"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."},"warnings":[{"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.","message":"Pydantic 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.","severity":"breaking","affected_versions":"0.7.x"},{"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.","message":"Blueprint support was added in `flask-pydantic-spec` v0.8.6. Prior versions do not natively support attaching `FlaskPydanticSpec` instances directly to Flask Blueprints.","severity":"gotcha","affected_versions":"<0.8.6"},{"fix":"Ensure `@app.route()` or `@blueprint.route()` is the first decorator applied to your route function, followed by `@spec.validate()`.","message":"The order of decorators matters. Flask's `@app.route()` or `@blueprint.route()` decorator should typically be placed *before* `flask-pydantic-spec`'s `@spec.validate()` decorator.","severity":"gotcha","affected_versions":"All 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.","message":"OpenAPI 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.","severity":"deprecated","affected_versions":"<0.8.2"}],"env_vars":null,"search_vec":"'0.8.7':62 'activ':51 'annot':30 'api':44,67 'current':59 'decor':33 'direct':38 'document':17,70 'enhanc':43 'flask':1,4,22,40,63 'frequent':54 'function':42 'generat':14 'integr':35 'librari':10,49 'maintain':47,52 'minor':55 'model':27 'openapi':16,65 'payload':24 'provid':32 'pydant':2,5,26,36,64 'python':9,29 'releas':56 'reliabl':45 'request/response':23 'rest':69 'rout':41 'simplifi':12 'spec':3,6 'swagger':66 'use':25 'valid':20,37,68 'version':60","created_at":"2026-04-16T18:47:56.510268+00:00","updated_at":"2026-04-16T18:47:56.510268+00:00","problems":{"verify_error":"Traceback (most recent call last):\n  File \"<string>\", line 1, in <module>\n  File \"/tmp/tmp2u2e42n0/venv/lib/python3.12/site-packages/flask_pydantic_spec/__init__.py\", line 4, in <module>\n    from .spec import FlaskPydanticSpec\n  File \"/tmp/tmp2u2e42n0/venv/lib/python3.12/site-packages/flask_pydantic"},"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.8.7","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/turner-townsend/flask-pydantic-spec","docs":null,"changelog":null,"pypi":"https://pypi.org/project/flask-pydantic-spec/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework","serialization","http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"import_fail","verified_at":"2026-07-03","last_verified":"2026-07-03","next_check":"2026-07-10","install_tag":null}}