ASGI2 Compatibility
Relevant source files
The following files were used as context for generating this wiki page:
ASGI2 Compatibility in Uvicorn refers to the server's ability to host applications written against the older ASGI 2.0 specification while utilizing a modern ASGI 3.0 server implementation. This is achieved through a specialized middleware layer that bridges the differences between the double-callable (ASGI2) and single-callable (ASGI3) interfaces.
The compatibility layer ensures that legacy applications can still benefit from Uvicorn's high-performance protocols, such as httptools and h11, without requiring code changes to the application's entry point.
Architecture and Implementation
Uvicorn detects the ASGI version of an application during the configuration loading phase. If an application is identified as following the ASGI 2.0 pattern, Uvicorn automatically wraps it in compatibility logic.
ASGI Interface Differences
The primary distinction between the versions is the invocation pattern:
- ASGI 2.0: Uses a "double-callable" pattern. The application is called with a
scope, returning an awaitable that is then called withreceiveandsend. - ASGI 3.0: Uses a "single-callable" pattern. The application is called once with
scope,receive, andsendsimultaneously.
Sources: uvicorn/_types.py:186-193, uvicorn/middleware/asgi2.py:10-15
The Compatibility Bridge
The ASGI2Middleware class acts as the translator between these two patterns. It implements the ASGI 3.0 interface (__call__(scope, receive, send)) and internally executes the two-step ASGI 2.0 process.
class ASGI2Middleware:
def __init__(self, app: "ASGI2Application"):
self.app = app
async def __call__(self, scope: "Scope", receive: "ASGIReceiveCallable", send: "ASGISendCallable") -> None:
instance = self.app(scope)
await instance(receive, send)
Sources: uvicorn/middleware/asgi2.py:10-15
Component Relationship
The following diagram illustrates how the server interacts with an ASGI2 application through the compatibility middleware:
The server treats the middleware as a standard ASGI3 application, while the middleware manages the legacy lifecycle.
Sources: uvicorn/middleware/asgi2.py:14-15, uvicorn/_types.py:186-193
Version Detection and Configuration
Uvicorn's Config class is responsible for inspecting the provided application and determining which interface version it utilizes.
Detection Logic
Uvicorn differentiates between versions based on the signature of the application callable. During the load() process, the server inspects the application to set the asgi_version property.
| App Signature | Detected Version | Interface Version String |
|---|---|---|
app(scope) | ASGI 2.0 | "2.0" |
app(scope, receive, send) | ASGI 3.0 | "3.0" |
Sources: tests/test_config.py:255-260, uvicorn/protocols/http/httptools_impl.py:228-232
Version Reporting in Scopes
When a request is processed, the protocol implementation populates the asgi dictionary within the scope. Even when running an ASGI2 application, the spec_version usually reflects the capabilities of the current server protocols (e.g., "2.3" for HTTP).
Protocol Integration
Compatibility is maintained across both HTTP and WebSocket protocols. Protocol implementations like HttpToolsProtocol and WebSocketsSansIOProtocol generate scopes that include the versioning information required by the middleware and application.
HTTP Scope Versioning
In the HttpToolsProtocol, the scope is initialized with version information derived from the server configuration.
Sources: uvicorn/protocols/http/httptools_impl.py:228-232
WebSocket Scope Versioning
In the WebSocketsSansIOProtocol, the scope specifically includes the asgi versioning and the WebSocket spec version.
Sources: uvicorn/protocols/websockets/websockets_sansio_impl.py:171-173
Summary of Types
The following table summarizes the technical definitions involved in the compatibility layer:
| Type | Definition | Source |
|---|---|---|
ASGI2Application | type[ASGI2Protocol] (Double-callable) | uvicorn/_types.py:192 |
ASGI3Application | Callable[[Scope, receive, send], Awaitable[None]] | uvicorn/_types.py:193 |
ASGIVersions | TypedDict containing spec_version and version | uvicorn/_types.py:53-56 |
Scope | HTTPScope, WebSocketScope, or LifespanScope | uvicorn/_types.py:97 |
Conclusion
ASGI2 Compatibility in Uvicorn is a transparent shim provided by ASGI2Middleware. By wrapping legacy double-callable applications, Uvicorn allows older frameworks to operate on a modern, high-performance ASGI3 server without modification, while correctly reporting the versioning context through the standard ASGI scope.