{"id":3797,"library":"sanic-ext","title":"Sanic Extensions","description":"Sanic-Ext is a powerful extension package for the Sanic web framework, providing core functionality like OpenAPI documentation (Swagger UI, Redoc), dependency injection, request data validation, background tasks, and CORS handling. It aligns its major version numbers with Sanic releases (e.g., v25.12.0 for Sanic 23.12/24.12/25.12). The project releases new major versions annually, typically in December, with patch and minor updates in between.","status":"active","version":"25.12.0","language":"en","source_language":"en","source_url":"https://github.com/sanic-org/sanic-ext/","tags":["web-framework","sanic","extension","openapi","swagger","redoc","dependency-injection","validation","cors"],"install":[{"cmd":"pip install sanic-ext","lang":"bash","label":"Install latest version"},{"cmd":"pip install sanic-ext[msgspec]","lang":"bash","label":"Install with msgspec support"},{"cmd":"pip install sanic-ext[pydantic]","lang":"bash","label":"Install with Pydantic support"}],"dependencies":[{"reason":"Core web framework Sanic-Ext extends. Compatibility is tied to Sanic's major version.","package":"sanic","optional":false},{"reason":"Optional dependency for high-performance data validation and serialization/deserialization. Enabled via sanic-ext[msgspec].","package":"msgspec","optional":true},{"reason":"Optional dependency for data validation, enabled via sanic-ext[pydantic].","package":"pydantic","optional":true}],"imports":[{"note":"The primary way to initialize Sanic-Ext is through `from sanic_ext import Extend`.","wrong":"from sanic_ext.extension import Extend","symbol":"Extend","correct":"from sanic_ext import Extend"},{"note":"Sanic-Ext expects the Sanic application object, which is imported from the main `sanic` library.","symbol":"Sanic","correct":"from sanic import Sanic"}],"quickstart":{"code":"from sanic import Sanic\nfrom sanic_ext import Extend\nfrom typing import Optional\n\napp = Sanic(\"My App with Sanic-Ext\")\nExtend(app)\n\n@app.get(\"/\")\nasync def hello_world(name: str = \"World\"):\n    # Sanic-Ext automatically injects query parameters based on type hints\n    return {\"hello\": name}\n\n@app.post(\"/items\")\nasync def create_item(item_id: int, description: Optional[str] = None):\n    # This demonstrates basic type-hinted injection for path/query parameters\n    return {\"item_id\": item_id, \"description\": description, \"status\": \"created\"}\n\nif __name__ == \"__main__\":\n    # Run the Sanic application\n    # Sanic-Ext provides features like OpenAPI docs at /docs or /swagger\n    app.run(host=\"0.0.0.0\", port=8000, debug=True)","lang":"python","description":"This quickstart demonstrates how to initialize Sanic-Ext with a Sanic application and use its dependency injection feature for route handlers. By type-hinting parameters in a route function, Sanic-Ext automatically extracts them from the request (e.g., query parameters, path parameters, or request body) and performs basic validation. After running, you can typically access OpenAPI documentation at `/docs` or `/swagger` in your browser."},"warnings":[{"fix":"Always install `sanic-ext` with a major version that matches or is compatible with your `sanic` installation. Refer to Sanic-Ext's documentation for specific version compatibility matrices.","message":"Sanic-Ext versions are tightly coupled with Sanic framework versions. For example, `sanic-ext==25.12.0` is intended for use with `sanic>=23.12,<26.0`. Using a significantly older or newer Sanic version with an incompatible `sanic-ext` version can lead to runtime errors or unexpected behavior.","severity":"breaking","affected_versions":"<25.12.0 (and future versions if mismatch occurs)"},{"fix":"If migrating from older versions, thoroughly test your API endpoints and review generated OpenAPI documentation. Consider explicitly installing `sanic-ext[msgspec]` or `sanic-ext[pydantic]` to leverage specific validation libraries.","message":"The behavior and capabilities of request data validation and OpenAPI schema generation have evolved across versions, especially with the introduction of `msgspec` support in `v23.12.0` and refinements to `pydantic`/`attrs` integration. Default type coercions or schema representations might differ slightly.","severity":"gotcha","affected_versions":"<23.12.0"},{"fix":"Upgrade to `sanic-ext==25.12.0` or newer to benefit from fixes addressing `KeyErrors` in query param validation and handler list mutation stability. If upgrading is not immediately possible, avoid dynamic modification of handler lists after `Extend(app)` is called.","message":"Prior to `v25.12.0`, Sanic-Ext could experience `KeyErrors` in query parameter validation under certain conditions. Additionally, issues related to handler list mutation (when dynamically adding/removing handlers) were present.","severity":"gotcha","affected_versions":"<25.12.0"},{"fix":"Upgrade to `sanic-ext==25.12.0` or newer to resolve issues with the background logger failing to remove unpickleable items. Ensure that objects passed to background tasks are pickleable where necessary.","message":"In versions prior to `v25.12.0`, the background logger might fail to remove unpickleable items, potentially leading to resource leaks or unexpected behavior in long-running applications that utilize background logging.","severity":"gotcha","affected_versions":"<25.12.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}