Skip to main content

fastapi-canon

PyPI Python CI Ruff Checked with mypy License

FastAPI for streams, WebSockets and AI apps, with typed clients.

FastAPI is great at JSON endpoints. WebSockets, streams and their errors are left to you, and none of it shows up in OpenAPI. Canon makes them typed contracts and generates Python and TypeScript clients.

Plain FastAPI: your own protocol, dispatch and error format, no schema.

@app.websocket("/ws")
async def ws(websocket: WebSocket) -> None:
    await websocket.accept()
    while True:
        message = await websocket.receive_json()
        if message.get("method") != "tasks.complete":
            await websocket.send_json({"id": message.get("id"), "error": "unknown"})
            continue
        try:
            task = await service.complete(int(message["params"]["task_id"]))
            await websocket.send_json(
                {"id": message["id"], "result": task.model_dump()}
            )
        except TaskMissing:
            await websocket.send_json({"id": message["id"], "error": "not found"})

Canon: a typed method with a declared error.

tasks = RpcChannel(namespace="tasks")


@tasks.method(raises=[task_missing])
async def complete(task_id: int, service: Inject[TaskService]) -> Task:
    return await service.complete(task_id)


router.include_channel(tasks, path="/ws")

The generated client is one call: task = await client.tasks.complete(task_id=1).

What Canon adds

  • Errors, declared once. Raise plain exceptions; get the response, OpenAPI entry and client error from one Error.
  • WebSocket RPC. Typed methods, streams and events, no request-ID plumbing.
  • Typed SSE. Clients iterate typed events and know when a stream failed.
  • Everything in the contract. HTTP, streams, sockets and WebRTC, checked in CI.
  • One app model. Features, Dishka dependencies and lifecycles for every transport.

Installation

uv add fastapi-canon

Python 3.12–3.14. Extras: sse, websockets, webrtc, mcp, codegen. Upgrading? See the changelog and upgrade guide.

Learn Canon

The docs are a learning path; start with the quickstart.

Step Guide You learn
1 Quickstart HTTP, SSE and RPC with one error, plus generated clients
2 Errors Public errors, fields and headers
3 Streaming Typed SSE, POST streams, event unions
4 WebSockets Methods, streams and events
5 Composition Features, Dishka services, policies, tests
6 Client generation Generating clients and catching drift
7 Error checking Finding undeclared errors before runtime
8–10 Files, database, observability Production concerns
11–12 WebRTC, MCP Media sessions and agent tools

Runnable apps are in examples/. Coding agents can install the bundled agent skill with uv run fastapi-canon skill install.

Metadata

Release files for fastapi-canon 0.30.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-canon 0.30.0
File Size Uploaded Provenance Actions
fastapi_canon-0.30.0.tar.gz 486.2 kB With provenance Details for fastapi_canon-0.30.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-canon 0.30.0
File Interpreter ABI Platform Actions
fastapi_canon-0.30.0-py3-none-any.whl Python 3 none any Details for fastapi_canon-0.30.0-py3-none-any.whl

Total release size: 1.1 MB

Release files / fastapi_canon-0.30.0.tar.gz

Download URL fastapi_canon-0.30.0.tar.gz
Size 486.2 kB
Tags Source
SHA-256 checksum
How to use checksums
81f1db7ecc5748185fa21bf875d9316b529dd65db97014c1f128b13ca9962669
BLAKE2b-256 checksum
How to use checksums
8e2a71a7ebe898fb1d649d5c119b703d1bc1af715edf8abeff3f2c6d66e266af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / fastapi_canon-0.30.0-py3-none-any.whl

Download URL fastapi_canon-0.30.0-py3-none-any.whl
Size 565.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ea34e3fd9e48722a0eef525cf5d6f7053e8d2696f655c8da0f81fceb5659c77
BLAKE2b-256 checksum
How to use checksums
de94c9d8d5a9fee18fc7777ac44c62152e2cba1ad3d14d81f7b40756144a3337
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for fastapi-canon 0.30.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastapi_canon-0.30.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.30.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page